【问题标题】:radio input styling not working for firefox无线电输入样式不适用于 Firefox
【发布时间】:2015-10-03 16:47:28
【问题描述】:

所以我使用下面的 css 设置了我的收音机输入样式。但是在 Firefox 中,出现了一个完全不同的收音机,我根本无法设置它的样式。

它在 safari 和 chrome 中完美运行,但我似乎无法弄清楚为什么它在 Firefox 中不起作用。

 input[type=checkbox] {
   border: 0px;
   cursor: pointer;
   height: 20px;
   margin: 0px 10px 0px 0px;
   position: relative;
   overflow: hidden;
   vertical-align: -5px;
   width: 20px;
 }
 input[type=checkbox]:before {
   background-color: transparent;
   border: 2px solid #E0E0E0;
   content: '';
   display: block;
   height: 20px;
   left: 0px;
   opacity: 1;
   position: absolute;
   transition: opacity .15s linear;
   top: 0px;
   width: 20px;
 }
 input[type=checkbox]:after {
   border: 2px solid #EC407A;
   content: '';
   display: block;
   height: 23px;
   left: -5px;
   opacity: 0;
   position: absolute;
   top: -9px;
   transform: rotate(-45deg);
   -webkit-transform: rotate(-45deg);
   transition: opacity .15s linear;
   width: 27px;
 }
 input[type=checkbox]:checked:before {
   opacity: 0;
 }
 input[type=checkbox]:checked:after {
   opacity: 1;
 }
 input[type=checkbox]:focus {
   box-shadow: none;
   outline: 0px;
 }
 .radio-wrapper {
   position: relative;
 }
 input[type='radio'] {
   -webkit-appearance: none;
   -moz-appearance: none;
   -ms-appearance: none;
   -o-appearance: none;
   appearance: none;
   position: relative;
   right: 0;
   bottom: 0;
   left: 0;
   height: 20px;
   width: 20px;
   -webkit-transition: all 0.15s ease-out 0s;
   -moz-transition: all 0.15s ease-out 0s;
   transition: all 0.15s ease-out 0s;
   background: #cbd1d8;
   border: none;
   color: #fff;
   cursor: pointer;
   display: inline-block;
   outline: none;
   position: relative;
   border-radius: 50%;
 }
 @-webkit-keyframes click-wave {
   0% {
     height: 40px;
     width: 40px;
     opacity: 0.35;
     position: relative;
   }
   100% {
     height: 200px;
     width: 200px;
     margin-left: -80px;
     margin-top: -80px;
     opacity: 0.0;
   }
 }
 @-moz-keyframes click-wave {
   0% {
     height: 40px;
     width: 40px;
     opacity: 0.35;
     position: relative;
   }
   100% {
     height: 200px;
     width: 200px;
     margin-left: -80px;
     margin-top: -80px;
     opacity: 0.0;
   }
 }
 @keyframes click-wave {
   0% {
     height: 40px;
     width: 40px;
     opacity: 0.35;
     position: relative;
   }
   100% {
     height: 200px;
     width: 200px;
     margin-left: -80px;
     margin-top: -80px;
     opacity: 0.0;
   }
 }
 .option-input {
   -webkit-transition: all 0.15s ease-out 0s;
   -moz-transition: all 0.15s ease-out 0s;
   transition: all 0.15s ease-out 0s;
   background: #7CE1C9;
   border: none;
   color: #fff;
   cursor: pointer;
   display: inline-block;
   outline: none;
   position: relative;
   margin-right: 0.5rem;
 }
 .option-input:hover {
   background: #9faab7;
 }
 .option-input:checked {
   background: #7CE1C9;
 }
 .option-input:checked::before {
   height: 20px;
   width: 20px;
   position: relative;
   content: '\2716';
   display: inline-block;
   font-size: 14.66667px;
   text-align: center;
   line-height: 20px;
 }
 .option-input:checked::after {
   -webkit-animation: click-wave 0.65s;
   -moz-animation: click-wave 0.65s;
   animation: click-wave 0.65s;
   background: #7CE1C9;
   content: '';
   display: block;
   position: relative;
 }
 .option-input.radio {
   border-radius: 50%;
 }
 .option-input.radio::after {
   border-radius: 50%;
 }
 input[type='radio'] + label {
   user-select: none;
   cursor: pointer;
   padding-left: 30px;
 }
 input[type='radio'] + label:before,
 input[type='radio'] + label:after {
   background: #fff;
   background-clip: padding-box;
   border: 2px solid #EC407A;
   border-radius: 50%;
   bottom: 3px;
   content: '';
   display: block;
   height: 16px;
   left: 3px;
   margin-top: -8px;
   position: absolute;
   top: 50%;
   transition: all 0.3s ease;
   width: 16px;
 }
 input[type='radio'] + label:after {
   background: #7CE1C9;
   border: 2px solid #fff;
   transform: scale(0);
   -wekbit-transform: scale(0);
   transition: all 0.3s ease;
 }
 input[type='radio']:focus + label:before {
   box-shadow: 0 0 5px #EC407A;
 }
 input[type='radio']:checked + label:before {
   border-color: #EC407A;
 }
 input[type='radio']:checked + label:after {
   transform: scale(0.75, 0.75);
   -webkit-transform: scale(0.75, 0.75);
 }
 `
<form>
  <li class='initialForm'><span class='optionText'>$option1</span><strong><input type='radio' class='option-input radio option1' id='$id' value='1' name='option'></strong>
  </li>
  <li class='initialForm'><span class='optionText'>$option2</span><strong><input type='radio' class='option-input radio option2' id='$id' value='2' name='option'></strong>
  </li>
</form>

【问题讨论】:

  • 您完美地添加了 CSS 和 HTML。你介意使用code snippets,以便更容易看到你在说什么吗?
  • 你有这个input[type='radio']:checked + label:after,但你的label呢?

标签: html css firefox


【解决方案1】:

我在网上找到了这篇博文,它解决了您遇到的问题:https://clarknikdelpowell.com/blog/styling-radio-buttons-select-boxes-in-firefox/

编辑: 一般来说,直接为单选按钮设置样式被认为是不好的做法。您遇到问题的原因是因为 -webkit-appearance, -moz-appearance 实际上不是 CSS。由于您无法直接设置单选按钮的样式,因此您必须使用一些变通方法。

  1. 将无线电输入设置为显示:无。
  2. 按照您希望为单选按钮设置样式的方式设置与单选按钮相邻的跨度。
  3. 使用 :checked 属性切换其外观。

我使用您指定的一些代码在 jsFiddle 上绘制了一个基本示例:https://jsfiddle.net/o3nurghw/2/

HTML:

<form>
  <li class='initialForm'>
      <span class='optionText'>$option1</span>
      <input type='radio' class='option-input radio option1' id='$id' value='1' name='option' />
      <span class="overlay"></span> <!-- the adjacent span -->
  </li>
  <li class='initialForm'>
    <span class='optionText'>$option2</span>
    <input type='radio' class='option-input radio option2' id='$id' value='2' name='option' />
    <span class="overlay"></span> <!-- the adjacent span -->
  </li>
</form>

CSS:

.initialForm {
    margin-bottom: .5em;
    display: block;
    cursor: pointer;    
    font-size: 16px;
    line-height: 1em;
}
.overlay {
    position: relative;
    right: 0;
    bottom: 0;
    left: 0;
    height: 20px;
    width: 20px;
    -webkit-transition: all 0.15s ease-out 0s;
    -moz-transition: all 0.15s ease-out 0s;
    transition: all 0.15s ease-out 0s;
    background: #cbd1d8;
    border: none;
    color: #fff;
    cursor: pointer;
    display: inline-block;
    outline: none;
    position: relative;
    border-radius: 50%;
}
.radio {
    display: none;
}
.radio:checked + span:before {
    width: .75em;
    height: .75em;
    margin: .125em;
    display: block;
    content: " ";
    background-color: #666;
    border-radius: 100%;
}

【讨论】:

    【解决方案2】:

    来源:https://clarknikdelpowell.com/blog/styling-radio-buttons-select-boxes-in-firefox/

    尽管它是 Chrome、Safari 和 Firefox 的一部分(-webkit-appearance、-moz-appearance),但实际上并不是 CSS。很多人使用 -webkit-appearance:none 来重置按钮、输入等所有内容的外观。但是,有几个 Firefox 错误使 -moz-appearance:none 成为一个危险的提议。

    由于外观不能完全在复选框和单选按钮上起作用。您仍然可以设置单选按钮的样式,您只需要使用一些技巧才能将其关闭。以下是你的做法。

    1. 将无线电输入设置为显示:无。

    2. 按照您希望为单选按钮设置样式的方式设置与单选按钮相邻的跨度。

    3. 使用 :checked 属性切换其外观。


    有关浏览器兼容性和更多信息,请参阅源代码 使用 :checked 属性切换其外观。

    【讨论】:

    • display 设置为none 时,您打算如何切换收音机(并使用:checked 属性)?
    【解决方案3】:

    您正在使用-webkit CSS 前缀,它专门用于 Chrome 和 Safari(或基于 webkit 的浏览器)。

    您还应该使用-moz CSS 前缀,特别是对于 Firefox。当然,如果你想支持 IE,它们可能对某些方法有额外或不同的样式。

    特别是对于您的 transform 方法,在 Firefox 中它被认为是实验性的,也许 -moz-transform 更合适。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-10
      • 2012-08-01
      • 1970-01-01
      相关资源
      最近更新 更多