简体   繁体   English

CSS选择器不起作用-输入[type = radio]:已选中

[英]CSS selector not working — input[type=radio]:checked

I am making a custom look for my radio buttons. 我正在为我的单选按钮自定义外观。 However I stumbled onto a problem when checking with CSS if the button is selected. 但是我在使用CSS检查是否选择了按钮时偶然发现了一个问题。

<ul class="list">
    <li><label for="radio_sex_male"> <i class="icon fa fa-mars" aria-hidden="true"></i> <span class="text">Male</span> </label> <input type="radio" id="radio_sex_male" name="sex" value="M"></li>
    <li><label for="radio_sex_female"> <i class="icon fa fa-venus" aria-hidden="true"></i> <span class="text">Female</span> </label> <input type="radio" id="radio_sex_female" name="sex" value="F"></li>
    <li><label for="radio_sex_other"> <i class="icon fa fa-genderless" aria-hidden="true"></i> <span class="text">Other</span> </label> <input type="radio" id="radio_sex_other" name="sex" value="O"></li>
</ul>

html body #body_cont form#new_user ul.list li input[type=radio] { display: none; visibility: collapse; }

html body #body_cont form#new_user ul.list li label {   
    position: relative; float: left; clear: none; display: inline-block;
    width: auto; height: 50px; margin: 0; padding: 0 20px;

    outline: 0; cursor: pointer;

    background-color: red;
}   
html body #body_cont form#new_user ul.list li label i.icon { float: left; font-size: 30px; margin: 0 8px 0 0; line-height: 50px; }
html body #body_cont form#new_user ul.list li label span.text { float: left; font-family: "Open Sans", sans-serif; font-weight: 600; font-size: 16px; line-height: 50px; }

That would be this line. 那就是这条线。 When the radio button is checked it needs to turn the background green. 选中单选按钮后,需要将背景变为绿色。

html body #body_cont form#new_user ul.list li input[type=radio]:checked + label { background-color: green; }

Am I missing something? 我想念什么吗?

div1 + div2 targets div2 that goes right after div1 . div1 + div2定位在div1之后的div2 So, in order to make your code work you have to redo your html part. 因此,为了使您的代码正常工作,您必须重做html部分。 Place <input> before <label> inside <li> s: <input>放在<li>内的<li> <label> <input>之前:

 html body #body_cont form#new_user ul.list li input[type=radio] { display: none; visibility: collapse; } html body #body_cont form#new_user ul.list li label { position: relative; float: left; clear: none; display: inline-block; width: auto; height: 50px; margin: 0; padding: 0 20px; outline: 0; cursor: pointer; background-color: red; } html body #body_cont form#new_user ul.list li label i.icon { float: left; font-size: 30px; margin: 0 8px 0 0; line-height: 50px; } html body #body_cont form#new_user ul.list li label span.text { float: left; font-family: "Open Sans", sans-serif; font-weight: 600; font-size: 16px; line-height: 50px; } html body #body_cont form#new_user ul.list li input[type=radio]:checked + label { background-color: green; } 
 <div id="body_cont"> <form id="new_user"> <ul class="list"> <li><input type="radio" id="radio_sex_male" name="sex" value="M"><label for="radio_sex_male"> <i class="icon fa fa-mars" aria-hidden="true"></i> <span class="text">Male</span> </label></li> <li><input type="radio" id="radio_sex_female" name="sex" value="F"><label for="radio_sex_female"> <i class="icon fa fa-venus" aria-hidden="true"></i> <span class="text">Female</span> </label> </li> <li><input type="radio" id="radio_sex_other" name="sex" value="O"><label for="radio_sex_other"> <i class="icon fa fa-genderless" aria-hidden="true"></i> <span class="text">Other</span> </label> </li> </ul> </form></div> 

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

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