使用CSS样式化输入单选框

25

我有一个包含[input type="radio"]的幻灯片:http://jsfiddle.net/Jtec5/23/
代码:CSS:

#slideselector {
    position: absolue;
    top:0;
    left:0;
    border: 2px solid black;
    padding-top: 1px;
}
.slidebutton {
    height: 21px;
    margin: 2px;
}

我试图使用CSS样式化输入单选按钮,使其看起来像这样:enter image description here 你有什么建议吗?

5个回答

39

像这样

演示

CSS

#slideselector {
    position: absolue;
    top:0;
    left:0;
    border: 2px solid black;
    padding-top: 1px;
}
.slidebutton {
    height: 21px;
    margin: 2px;
}
#slideshow { 
    margin: 50px auto; 
    position: relative; 
    width: 240px; 
    height: 240px; 
    padding: 10px; 
    box-shadow: 0 0 20px rgba(0,0,0,0.4); 
}

#slideshow > div { 
    position: absolute; 
    top: 10px; 
    left: 10px; 
    right: 10px; 
    bottom: 10px;
    overflow:hidden;
}

.imgLike {
    width:100%;
    height:100%;
}
/* Radio */

input[type="radio"] {
    background-color: #ddd;
    background-image: -webkit-linear-gradient(0deg, transparent 20%, hsla(0,0%,100%,.7), transparent 80%),
                      -webkit-linear-gradient(90deg, transparent 20%, hsla(0,0%,100%,.7), transparent 80%);
    border-radius: 10px;
    box-shadow: inset 0 1px 1px hsla(0,0%,100%,.8),
                0 0 0 1px hsla(0,0%,0%,.6),
                0 2px 3px hsla(0,0%,0%,.6),
                0 4px 3px hsla(0,0%,0%,.4),
                0 6px 6px hsla(0,0%,0%,.2),
                0 10px 6px hsla(0,0%,0%,.2);
    cursor: pointer;
    display: inline-block;
    height: 15px;
    margin-right: 15px;
    position: relative;
    width: 15px;
    -webkit-appearance: none;
}
input[type="radio"]:after {
    background-color: #444;
    border-radius: 25px;
    box-shadow: inset 0 0 0 1px hsla(0,0%,0%,.4),
                0 1px 1px hsla(0,0%,100%,.8);
    content: '';
    display: block;
    height: 7px;
    left: 4px;
    position: relative;
    top: 4px;
    width: 7px;
}
input[type="radio"]:checked:after {
    background-color: #f66;
    box-shadow: inset 0 0 0 1px hsla(0,0%,0%,.4),
                inset 0 2px 2px hsla(0,0%,100%,.4),
                0 1px 1px hsla(0,0%,100%,.8),
                0 0 2px 2px hsla(0,70%,70%,.4);
}

无法在我的代码上运行,您可以在此处查看:jsfiddle.net/Jtec5/23 - System-x32z
@system-x32z 我已经更新了我的答案,请现在检查。 - Falguni Panchal
1
似乎你的样式只在Chrome上有效(测试浏览器:IE10,Firefox和Chrome) - Bart Burg
1
在生产网站上使用外观并不是一个好主意:https://developer.mozilla.org/en/docs/Web/CSS/-moz-appearance - Stephan Weinhold

5

这里是一个简单的示例,展示如何实现该操作。

只需替换图像文件即可完成。

HTML 代码

<input type="radio" id="r1" name="rr" />
<label for="r1"><span></span>Radio Button 1</label>
<p>
<input type="radio" id="r2" name="rr" />
<label for="r2"><span></span>Radio Button 2</label>

CSS

input[type="radio"] {
    display:none;
}

input[type="radio"] + label {
    color:#f2f2f2;
    font-family:Arial, sans-serif;
    font-size:14px;
}

input[type="radio"] + label span {
    display:inline-block;
    width:19px;
    height:19px;
    margin:-1px 4px 0 0;
    vertical-align:middle;
    background:url(check_radio_sheet.png) -38px top no-repeat;
    cursor:pointer;
}

input[type="radio"]:checked + label span {
    background:url(check_radio_sheet.png) -57px top no-repeat;
}

Working DEMO


在您的代码中,创建 radio 后,您需要添加 <label for="r2"><span></span>Radio Button 2</label>,通过使用我的 CSS 代码,您可以实现这一点。 - Dipesh Parmar
@DipeshParmar 使用 input[type="radio"] { display:none; } 将会使单选按钮失去键盘访问功能。这将导致无法通过辅助功能测试。你知道有什么解决方法吗? - Suleman Mirza

0

你应该为你的单选按钮使用一些背景图片,并在更改时翻转它与另一张图片。

.radio {
    background: url(customButton.png) no-repeat;
}

无法在我的代码上运行,您可以在此处查看:jsfiddle.net/Jtec5/23 - System-x32z

0

请查看此Fiddle

<input type="radio" id="radio-2-1" name="radio-2-set" class="regular-radio" /><label for="radio-2-1"></label>
<input type="radio" id="radio-2-2" name="radio-2-set" class="regular-radio" /><label for="radio-2-2"></label>
<input type="radio" id="radio-2-3" name="radio-2-set" class="regular-radio" /><label for="radio-2-3"></label>

.regular-radio {
    display: none;
}

.regular-radio + label {
    -webkit-appearance: none;
    background-color: #e1e1e1;
    border: 4px solid #e1e1e1;
    border-radius: 10px;
    width: 100%;
    display: inline-block;
    position: relative;
    width: 10px;
    height: 10px;
}

.regular-radio:checked + label {
    background: grey;
    border: 4px solid #e1e1e1;
}

无法在我的代码上运行,您可以在此处查看:jsfiddle.net/Jtec5/23 - System-x32z

0

Trident 为复选框和单选按钮控件提供了 ::-ms-check 伪元素。例如:

<input type="checkbox">
<input type="radio">

::-ms-check {
    color: red;
    background: black;
    padding: 1em;
}

在 Windows 8 上的 IE10 中,它显示如下:

enter image description here


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