当我的广播按钮检查时,标签不会更改背景颜色
为什么在选中广播按钮时我的标签盒不更改颜色?我还尝试输入:检查+标签或〜标签{ 语法},但仍然没有效果:
.ans{
display: none;
}
.answer input[type="radio"]:checked+label{
background-color: #FFA500;
}
.ans-list{
border-style:solid;
border-width:thin;
margin:20% auto;
width:50%;
}
.ans-list:hover{
background-color:#D6D5A8;
color:black;
}
<div class="answer an1">
<label class="ans-list" for="ans1">1</label><input type="radio" name="q1" class="ans false" id="ans1"><br>
<label class="ans-list" for="ans2">4</label><input type="radio" name="q1" class="ans true" id="ans2"><br>
<label class="ans-list" for="ans3">3</label><input type="radio" name="q1" class="ans false" id="ans3"><br>
<label class="ans-list" for="ans4">2</label><input type="radio" name="q1" class="ans false" id="ans4"><br>
<button type="submit" name="Submit" class="submit">Submit</button>
<button type="button" class="next hide" name="next">Next</button>
</div>
( codepen )
我也尝试了:
label{
display:block;
margin: 1rem auto;
border-style:solid;
width:50%;
}
input[type="radio"]:checked+label{
background-color:red;
color:red;
}
<label for="choice1"> <input type="radio" id="choice1" class="ans" name="op" checked="checked">male</label>
<label for="choice2"> <input type="radio" id="choice2" class="ans" name="op">male</label>
<label for="choice3"> <input type="radio" id="choice3" class="ans" name="op">male</label>
( codepen )
Why my label box not change the color when the radio button checked? I also try input:checked+label or ~label{
syntax} but still no effect :
.ans{
display: none;
}
.answer input[type="radio"]:checked+label{
background-color: #FFA500;
}
.ans-list{
border-style:solid;
border-width:thin;
margin:20% auto;
width:50%;
}
.ans-list:hover{
background-color:#D6D5A8;
color:black;
}
<div class="answer an1">
<label class="ans-list" for="ans1">1</label><input type="radio" name="q1" class="ans false" id="ans1"><br>
<label class="ans-list" for="ans2">4</label><input type="radio" name="q1" class="ans true" id="ans2"><br>
<label class="ans-list" for="ans3">3</label><input type="radio" name="q1" class="ans false" id="ans3"><br>
<label class="ans-list" for="ans4">2</label><input type="radio" name="q1" class="ans false" id="ans4"><br>
<button type="submit" name="Submit" class="submit">Submit</button>
<button type="button" class="next hide" name="next">Next</button>
</div>
(Codepen)
I also tryed :
label{
display:block;
margin: 1rem auto;
border-style:solid;
width:50%;
}
input[type="radio"]:checked+label{
background-color:red;
color:red;
}
<label for="choice1"> <input type="radio" id="choice1" class="ans" name="op" checked="checked">male</label>
<label for="choice2"> <input type="radio" id="choice2" class="ans" name="op">male</label>
<label for="choice3"> <input type="radio" id="choice3" class="ans" name="op">male</label>
(Codepen)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
检查此内容您需要更改结构1st
输入
标签
无法选择直接的先前兄弟姐妹,而您的代码(+)您正在选择下一个同胞。因此,您可以更改“标签”和“输入”的顺序,它将起作用Check this you need to change the structure 1st
input
afterlabel
there is no way to select direct previous siblings, with your code (+) you are selecting the next sibling. So you can just change the order of "label" and "input" and it will work使用CSS,无法选择直接的先前兄弟姐妹,
使用您的代码(+),您将选择下一个兄弟姐妹。
因此,您只需更改“标签”和“输入”的顺序,它将起作用,但它将更改页面上元素的顺序。
选项1
纯CSS解决方案以保持布局未触及的是:
在视觉上是相同的,但现在您可以应用CSS选择器
使用纯CSS进行此工作。
选项2 ,
但是,恕我直言,最好的选择是添加一个小JS,将类添加到正确的标签中。它较慢,但
CSS部分
JavaScript部分
With CSS there is no way to select direct previous siblings,
with your code (+) you are selecting the next sibling.
So you can just change the order of "label" and "input" and it will work, but it will change the order of the elements on the page.
Option 1
The pure CSS solution to keep the layout untouched would be:
Visually is the same, but now you can apply CSS selector
This work with pure CSS.
Option 2
But, imho, the best option is adding a small js that add a class to the right label. It's slower but
CSS part
Javascript part