为单个输入 ID 定义 CSS 样式,而不是 input[type=checkbox]

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18036544/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 23:47:26  来源:igfitidea点击:

Define CSS style for single input ID, instead of input[type=checkbox]

cssinputcheckbox

提问by user2635574

This is my check box

这是我的复选框

<input type="checkbox" name="MyName[]" value="A1" id="A1"/>
<label for="Filter"></label>
<input type="checkbox" name="MyName[]" value="A2" id="A2"/>
<label for="Filter"></label>

This is my style

这是我的风格

input[type=checkbox] {..}
input[type=checkbox] + label {background:'image-1'}
input[type=checkbox]:checked + label {background:'image-2'}

I need to style EACH checkbox with a different checked and unchecked image.

我需要使用不同的选中和未选中的图像来设置每个复选框的样式。

I was thinking to refer to ID instead of input[type=checkbox]in my CSS.

我想input[type=checkbox]在我的 CSS 中引用 ID 而不是引用。

How to do that?

怎么做?

回答by Bram Vanroy

input#A1
input#A2
input#A1:checked
input#A2:checked

But you don't need to specify the tag name (input) if you don't want. But it can be useful though! (Especially with classes)

但是,如果您不想,则无需指定标记名称 ( input)。但它可能很有用!(特别是上课)

Read more about (ID-)selectors in the docs.

文档中阅读有关 (ID-) 选择器的更多信息。

回答by Ritabrata Gautam

for

为了

<input type="checkbox" name="MyName[]" value="A1" id="A1"/>

it will be

这将是

input[name=MyName[]] {..}

to satisfy your need

满足您的需要

回答by theftprevention

With the id selector:#

使用id 选择器:#

input#A1 {background:url('image-1.jpg');}
input#A1:checked {background:url('image-1-checked.jpg');}
input#A2 {background:url('image-2.jpg');}