【问题标题】:Styling Radio Inputs样式化无线电输入
【发布时间】: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


【解决方案1】:

如 cmets 中所述,:after:before 伪元素插入到应用它们的元素内部。 :after:before 不应与输入一起使用,因为 &lt;input&gt; 元素不能有子元素:(

解决方案是将图像和:after 伪元素应用于无线电输入标签。从语义的角度来看,给标签一个值也很好。

  • 无线电输入用display: none 隐藏,它们是通过附有匹配forid 属性的相应标签来选择的。

  • input + label 仅针对每个输入之后的标签元素。

  • input + label:afterinput:checked + label:after 提供选定的不透明叠加和过渡动画

Have an example!

HTML

<div id="welcome-gender"> 
    <input class="male" name="gender" type="radio" value="male" id="male-one">
    <label for="male-one">Male One</label>          

    <input class="female" name="gender" type="radio" value="female" id="female-one">
    <label for="female-one">Female One</label>
</div>

CSS

input[type="radio"]{
    display: none;
}
label {
    position: relative;
    width: 170px;
    height: 300px;
    display: inline-block;
    color: white;
    font-size: 0;
    border-radius: 4px;
}
.male + label {
    background: url(http://socialavatarnetworkscript.com/media/img/male_avatar.png);    
}
.female + label {
    background: url(http://socialavatarnetworkscript.com/media/img/female_avatar.png)
}

input + label:after {
    background: #FFF;
    content: '';    
}
input:checked + label: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;
}

【讨论】:

  • 你救了我的培根,你真的救了。非常感谢。
猜你喜欢
  • 2015-10-03
  • 2021-12-27
  • 1970-01-01
  • 2021-09-23
  • 2013-08-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多