简体   繁体   English

CSS3 单选按钮在 Firefox 中不起作用

[英]CSS3 radio button not working in Firefox

I'm trying to style a radio button using css3 pseudo elements.我正在尝试使用 css3 伪元素设置单选按钮的样式。 In works great in chrome but it jumps back to the default styling in firefox. In 在 chrome 中效果很好,但它会跳回到 firefox 中的默认样式。 Any idea?任何想法?

Here's a fiddle: JSFIDDLE这是一个小提琴: JSFIDDLE

The HTML: HTML:

<input class="fancy" name="cc1" type="radio" checked/>
<input class="fancy" name="cc1" type="radio"/>

The CSS: CSS:

input[type="radio"].fancy:before {
  content:"";
  width: 24px;
  height: 24px;
  background-color: #1f1f1f;
  display: block;
  border-radius: 50%;
  position: relative;
  top: -6px;
  left: -6px;
  box-shadow: 0 1px 2px rgba(255,255,255,.1),
              inset 0 2px 2px rgba(0,0,0,.8);
}

input[type="radio"].fancy:checked:before {
  visibility: visible;
  content:"";
  width: 24px;
  height: 24px;
  background-color: #1f1f1f;
  display: block;
  border-radius: 50%;
  position: relative;
  top: -6px;
  left: -6px;
  box-shadow: 0 1px 2px rgba(255,255,255,.2),
          inset 0 2px 2px rgba(0,0,0,.8);
}

input[type="radio"].fancy:checked:after {
  visibility: visible;
  content:"";
  width: 14px;
  height: 14px;
  display: block;
  border-radius: 50%;
  position: relative;
  top: -25px;
  left: -1px;
  background: yellowgreen;
}

I'm trying to avoid background-images我试图避免背景图像

Unfortunately, what you're trying to do is not valid -- ::before and ::after do not work on input elements (any input ; it's not just restricted to radio buttons).不幸的是,您尝试执行的操作无效- ::before::after不适用于input元素(任何input ;它不仅限于单选按钮)。

Proof available here: http://jsfiddle.net/LvaE2/1/ -- even in the most simple case, it doesn't work.此处提供证明: http://jsfiddle.net/LvaE2/1/——即使在最简单的情况下,它也不起作用。

It also doesn't work in IE or Opera.它在 IE 或 Opera 中也不起作用。 The fact that it does work in Chrome is because Chrome is going beyond the spec in allowing it.它在 Chrome 中工作的事实是因为 Chrome 超出了允许它的规范。

Your best bet is to do your styling on a label element that is linked to the actual radio button using the for attribute, and then set the radio button itself to display:none;最好的办法是在label元素上进行样式设置,该元素使用for属性链接到实际的单选按钮,然后将单选按钮本身设置为display:none; . . This is how everyone else does it.这就是其他人的做法。

Related question here: Which elements support the::before and::after pseudo-elements?相关问题: 哪些元素支持::before 和::after 伪元素?

Hope that helps.希望有帮助。

CSS3 Input radio will working in Firefox: version > 80 if added style appearance CSS3 输入无线电将在 Firefox 中工作:如果添加样式appearance ,版本 > 80

input[type=checkbox] {   appearance:initial; }

Please see the Snippet:请看片段:

 input[type=radio] { appearance:initial; } input[type=radio]:after{ content: "after content"; width: 100px; height: 100px; display:block; background: red; } input[type=radio]:checked:after{ content: "after content"; width: 100px; height: 100px; display:block; background: blue; }
 <input type="radio"> <input type="radio"> <input type="radio">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM