火狐浏览器无法正常显示单选框输入样式

3

我已经使用下面的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。您介意使用代码片段,以便更容易地看到您所说的内容吗? - BillyNate
你有这个 input[type='radio']:checked + label:after,但是你的 label 在哪里呢? - dippas
3个回答

0

您正在使用 -webkit CSS 前缀,这是专门为 Chrome 和 Safari(或基于 Webkit 的浏览器)设计的。

您还应该使用 -moz CSS 前缀,专门为 Firefox 设计。当然,如果您想支持 IE,则可能需要针对某些方法具有额外或不同的样式。

特别针对您的 transform 方法,在 Firefox 中被视为实验性的,也许更适合使用 -moz-transform


0

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

尽管-webkit-appearance,-moz-appearance是Chrome、Safari和Firefox的一部分,但它实际上并不是CSS。许多人使用-webkit-appearance:none来重置按钮、输入框等所有元素的外观。然而,有几个Firefox的bug使-moz-appearance:none成为一个危险的选择。

由于appearance在复选框和单选按钮上不能完全工作。你仍然可以样式化单选按钮,只是你需要使用一些技巧才能做到。以下是如何实现:

  1. 将单选输入设置为display:none。

  2. 样式化与单选按钮相邻的span,就像你想要样式化单选按钮一样。

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


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


1
display设置为none时,您打算如何切换收音机(并使用:checked属性)? - BillyNate

-1

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

编辑: 通常情况下,直接样式化单选按钮被认为是不好的做法。你遇到问题的原因是因为-webkit-appearance, -moz-appearance实际上不是CSS。由于你不能直接样式化单选按钮,所以必须使用一些变通方法。

  1. 将单选输入设置为display:none。
  2. 样式化与单选按钮相邻的span元素,就像你想要样式化单选按钮一样。
  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%;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接