【问题标题】:Different colors for radio buttons单选按钮的不同颜色
【发布时间】:2014-11-27 23:44:31
【问题描述】:

我想用这种方式设置单选按钮的样式:

我现在的代码如下所示:

input[type=radio] {
display:none;
}
input[type=radio]:checked +label:before {
background:green;
width: 15px;
height: 15px;
border: 0;
border-radius: 50%;
}
label {
color:black;
font-family:arial;
font-size:12px;
position:relative;
padding-left:20px;
}
label:hover {
color:green;
}
label:hover:before {
border:1px solid green;
border-radius: 50%;
}
label:before {
content:'';
height:13px;
width:13px;
border:1px solid lightgray;
border-radius: 50%;
display:inline-block;
position:absolute;
top:0;
left:0;
}

和 HTML 代码:

<input id="kunstmuseum1" type="radio" name="kunstmuseum" onClick="animateKunst('voll')">    <label class="gruen" for="kunstmuseum1">Volle Subvention</label>
<input id="kunstmuseum2" type="radio" name="kunstmuseum" onClick="animateKunst('halb')"><label for="kunstmuseum2">Subventionen um die Hälfte kürzen</label>
<input id="kunstmuseum3" type="radio" name="kunstmuseum" checked="checked" onClick="animateKunst('null')"><label for="kunstmuseum3">Keine Subventionen mehr</label>

是否可以为 input[type=radio] 添加不同的类,如何将单选按钮仅填充一半?这是一个 jsfiddle:http://jsfiddle.net/x5d4tyq7/

【问题讨论】:

  • 当单选按钮填满一半时,它仍然可以选择吗?
  • 是的,它只是视觉效果,按钮之间也有差异,尽管我对视觉效果并不完全确定。我不知道如何分别为单选按钮设置不同的样式?例如:同一组单选按钮,但一个是绿色的,一个是橙色的,一个是红色的。

标签: javascript html css radio-button


【解决方案1】:

要为样式选择正确的单选按钮,请使用此 css:

input[type=radio]:checked:nth-of-type(1) +label:before{

这将选择第一个单选按钮(如果它处于活动状态)。将第 nth-of-type(1) 更改为任意数字以选择右侧单选按钮。

这会将单选按钮更改为您的示例:

条纹:

  background: -webkit-repeating-linear-gradient(315deg, black, white 10%);
  background: -o-repeating-linear-gradient(315deg, black, white 10%);
  background: -moz-repeating-linear-gradient(315deg, black, white 10%);
  background: repeating-linear-gradient(315deg, black, white 10%);

50/50 黑/白:

  background: -webkit-repeating-linear-gradient(90deg, black, black 49%, white 51%, white 100%);
  background: -o-repeating-linear-gradient(90deg, black, black 49%, white 51%, white 100%);
  background: -moz-repeating-linear-gradient(90deg, black, black 49%, white 51%, white 100%);
  background: repeating-linear-gradient(90deg, black, black 49%, white 51%, white 100%);

白色:

  background-color: white;
  border: solid 1px green;

Here is the JSfiddle with the working code.

【讨论】:

    【解决方案2】:

    你可以用线性渐变填充before

    input[type=radio] {
        display:none;
    }
    input[type=radio]:checked +label:before {
        background: linear-gradient(to right, green 0%,green 50%, rgba(255, 255, 255, 1) 51%, rgba(255, 255,255, 1) 100%);
        /* W3C */
        width: 15px;
        height: 15px;
        border: 0;
        border-radius: 50%;
    }
    label {
        color:black;
        font-family:arial;
        font-size:12px;
        position:relative;
        padding-left:20px;
    }
    label:hover {
        color:green;
    }
    label:hover:before {
        border:1px solid green;
        border-radius: 50%;
    }
    label:before {
        content:'';
        height:13px;
        width:13px;
        border:1px solid lightgray;
        border-radius: 50%;
        display:inline-block;
        position:absolute;
        top:0;
        left:0;
    }
    <input id="kunstmuseum1" type="radio" name="kunstmuseum" onClick="animateKunst('voll')">
    <label class="gruen" for="kunstmuseum1">Volle Subvention</label>
    <input id="kunstmuseum2" type="radio" name="kunstmuseum" onClick="animateKunst('halb')">
    <label for="kunstmuseum2">Subventionen um die Hälfte kürzen</label>
    <input id="kunstmuseum3" type="radio" name="kunstmuseum" checked="checked" onClick="animateKunst('null')">
    <label for="kunstmuseum3">Keine Subventionen mehr</label>

    【讨论】:

    • 谢谢,这对于半填充按钮来说已经很棒了,但是我怎样才能为三个按钮设置不同的样式呢?因此,如果单击一个按钮,则完全填充,如果单击另一个按钮,则将其填充一半,而对于第三个按钮,则只有边框变为绿色。
    猜你喜欢
    • 1970-01-01
    • 2015-06-27
    • 2021-11-20
    • 2019-02-03
    • 2011-04-19
    • 2019-05-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多