【发布时间】:2014-10-10 08:07:06
【问题描述】:
所以我在设置单选按钮的样式时遇到了很大的麻烦,因此我设法让它在 Chrome 中完美运行,在 Firefox 中只有一点点,而在 IE 中则完全没有。
这是预期的外观(在 Chrome 中):
这是它在 Firefox 中的外观:
还有 IE...
我有一个 JSFiddle 供你们玩,但我似乎不知道如何让这个跨浏览器工作。有什么想法吗?
JSFiddle:http://jsfiddle.net/s5rpd97b/
强制粘贴代码,尽管它在 JSFiddle 上:
HTML:
<input style="background: url(http://socialavatarnetworkscript.com/media/img/male_avatar.png);margin-right: 240px;" class="gender" name="gender" type="radio" value="male">
<input style="background: url(http://socialavatarnetworkscript.com/media/img/female_avatar.png)" class="gender" name="gender" type="radio" value="female">
CSS:
input[type="radio"].gender::after{
background: rgba(0, 0, 0, 0.01);
position: absolute;
width: 170px;
height: 300px;
display: block;
content: '';
color: white;
font-size: 1px;
-webkit-transition: all 0.2s ease;
-moz-transition: all 0.2s ease;
-ms-transition: all 0.2s ease;
-o-transition: all 0.2s ease;
transition: all 0.2s ease;
}
input[type="radio"].gender:checked::after{
background: rgba(0, 0, 0, 0.8);
position: absolute;
width: 170px;
height: 300px;
display: block;
content: 'SELECTED';
border-radius: 4px;
color: white;
font-family: titillium, sans-serif;
font-weight: 500;
font-size: 22px;
text-align: center;
line-height: 300px;
-webkit-transition: all 0.2s ease;
-moz-transition: all 0.2s ease;
-ms-transition: all 0.2s ease;
-o-transition: all 0.2s ease;
transition: all 0.2s ease;
}
input[type="radio"]{
display: inline-block;
-webkit-appearance: none;
-moz-appearance: none;
-ms-appearance: none;
appearance: none;
border-radius: 4px !important;
outline: none !important;
border: none !important;
}
input[type="radio"].gender{
height: 300px;
width: 170px;
border-radius: 4px !important;
outline: none !important;
border: none !important;
}
【问题讨论】:
-
不要将
input之类的表单元素用于:after和:before。 -
::after应该被渲染为好像一个新的 child 元素被插入到它所应用的元素中——但input元素不能有子元素.在任何情况下,跨浏览器格式化单选按钮都很棘手。为了获得更可靠的结果,我建议您在每个输入之后使用一个额外的元素,并使用相邻的兄弟组合器+根据输入的状态来格式化它们。 -
我想用标签来做这个是最可行的选择?
-
将内联样式和外部样式混合在一起是一种非常糟糕的做法......因为它会让其他人检查您的代码......
标签: html css cross-browser radio-button