html - 自定义单选按钮,与其他label标签冲突

标签 html css checkbox radio-button

我有一些使用 CSS 的自定义单选按钮,它们隐藏了原始按钮,并使用 LABEL 标签来显示图像。问题是,它工作正常,但也与文本框上的标签冲突。

我有 100 个单选按钮,所以我不想为每个单选按钮添加一个类。有没有办法将 CSS 更改为仅适用于单选/复选框的 LABEL 标签?

编辑:我可以删除文本框的标签,因为它们不需要可点击,但很高兴知道。

JS FIDDLE

HTML:

<input type="radio" name="question10" id="radio10c" value="radio" />
<label class="black" for="radio10c">Mostly</label>
<br/>
<input type="text">
<label>Text box label with repeated pattern of radio button on the background</label>

CSS:

input[type=radio], input[type=checkbox] {
    display: none;
}
input[type=radio]+ label, input[type=checkbox] + label {
    padding-left: 35px;
    padding-top: 2px;
    height: 30px;
    display: inline-block;
    line-height: 30px;
    background-repeat: no-repeat;
    background-position: 0 0;
    font-size: 12px;
    vertical-align: middle;
    cursor: pointer;
}
input[type=radio]:checked + label, input[type=checkbox]:checked + label {
    background-position: 0 -30px;
}
label {
    padding-bottom:2px;
    background-image: url(http://cis.kitoit.com/layouts/images/radio.png);
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    -khtml-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
}

最佳答案

请替换此 css。这是你想要的吗?

input[type=radio], input[type=checkbox] {
    display: none;
}
input[type=radio]+ label, input[type=checkbox] + label {
    padding-left: 35px;
    padding-top: 2px;
    height: 30px;
    display: inline-block;
    line-height: 30px;
    background-repeat: no-repeat;
    background-position: 0 0;
    font-size: 12px;
    vertical-align: middle;
    cursor: pointer;
}
input[type=radio]:checked + label, input[type=checkbox]:checked + label {
    background-position: 0 -30px;
}
input[type=radio] + label {
    padding-bottom:2px;
    background-image: url(http://cis.kitoit.com/layouts/images/radio.png);
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    -khtml-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
}

关于html - 自定义单选按钮,与其他label标签冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21550052/

相关文章:

html - Bootstrap "jumbotron-narrow"示例。风格如何变化?

html - 试图使图像背景不透明

html - PhpStorm 新行增加空间

html - 网站的自动 Retina 图像

javascript - 剪贴板中的粘贴图像功能如何在 Gmail 和 Google Chrome 12+ 中工作?

javascript - HTML 中的嵌套类

jquery - 调整图库中图像的大小以适应行,但保持每个图像的纵横比

php jquery动态保存复选框

Silverlight 将一个控件数据绑定(bind)到另一个控件

android - 如何制作带有复选框和图像的ListView?