如何在单选按钮选中时更改标签边框颜色?

huangapple go评论62阅读模式
英文:

How to change label border color when radio is checked?

问题

我需要标签的边框颜色在选择内部的收音机被选中时更改 不使用id

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    font-family: 'Roboto', sans-serif
}

body {
    height: 100vh;
    padding: 10px
}

.selectable {
    display: flex;
    margin-top: 10px;
    padding: 10px 12px;
    border-radius: 5px;
    cursor: pointer;
    border: 1px solid #ddd;
}

.selectable:active {
    border: 1px solid #0d6efd;
}

.selectable:hover {
    background: #20c997;
}

input[type='radio'] {
    width: 22px;
    height: 22px;
    margin-right: 10px;
}

input[type='radio']:before {
    content: '';
    display: block;
    width: 60%;
    height: 60%;
    margin: 20% auto;
    border-radius: 50%;
}

input[type="radio"]:checked:before {
    background: #20c997;
}

input[type="radio"]:checked + label {
    border-color: #20c997;
}
<label class="selectable">
    <input type="radio" name="selectable">
    <span>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Asperiores, impedit.</span>
</label>
<label class="selectable">
    <input type="radio" name="selectable">
    <span>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Asperiores, impedit.</span>
</label>
<label class="selectable">
    <input type="radio" name="selectable">
    <span>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Asperiores, impedit.</span>
</label>
<label class="selectable">
    <input type="radio" name="selectable">
    <span>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Asperiores, impedit.</span>
</label>

这是示例一个选中的收音机按钮应该是什么样子的。我不想使用id,如果可能的话只想通过css进行更改,或者通过添加类来保持最少的html更改。

我尝试了很多方法,包括:

input[type="radio"]:checked + label {
    border-color: #20c997;
}

.selectable:checked {
    border-color: #20c997;
}

但都不起作用。

英文:

I need the label's border color to change when the radio inside is selected without using ids.

<!-- begin snippet: js hide: false -->

<!-- language: lang-css -->

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    font-family: &#39;Roboto&#39;, sans-serif
}

body {
    height: 100vh;
    padding: 10px
}

.selectable {
    display: flex;
    margin-top: 10px;
    padding: 10px 12px;
    border-radius: 5px;
    cursor: pointer;
    border: 1px solid #ddd;
}

.selectable:active {
    border: 1px solid #0d6efd;
}

.selectable:hover {
    background: #20c997;
}

input[type=&#39;radio&#39;] {
    width: 22px;
    height: 22px;
    margin-right: 10px;
}

input[type=&#39;radio&#39;]:before {
    content: &#39;&#39;;
    display: block;
    width: 60%;
    height: 60%;
    margin: 20% auto;
    border-radius: 50%;
}

input[type=&quot;radio&quot;]:checked:before {
    background: #20c997;
}

input[type=&quot;radio&quot;]:checked {
    border-color: #20c997;
}

<!-- language: lang-html -->

&lt;label class=&quot;selectable&quot;&gt;
    &lt;input type=&quot;radio&quot; name=&quot;selectable&quot;&gt;
    &lt;span&gt;Lorem ipsum dolor sit amet, consectetur adipisicing elit. Asperiores, impedit.&lt;/span&gt;
&lt;/label&gt;
&lt;label class=&quot;selectable&quot;&gt;
    &lt;input type=&quot;radio&quot; name=&quot;selectable&quot;&gt;
    &lt;span&gt;Lorem ipsum dolor sit amet, consectetur adipisicing elit. Asperiores, impedit.&lt;/span&gt;
&lt;/label&gt;
&lt;label class=&quot;selectable&quot;&gt;
    &lt;input type=&quot;radio&quot; name=&quot;selectable&quot;&gt;
    &lt;span&gt;Lorem ipsum dolor sit amet, consectetur adipisicing elit. Asperiores, impedit.&lt;/span&gt;
&lt;/label&gt;
&lt;label class=&quot;selectable&quot;&gt;
    &lt;input type=&quot;radio&quot; name=&quot;selectable&quot;&gt;
    &lt;span&gt;Lorem ipsum dolor sit amet, consectetur adipisicing elit. Asperiores, impedit.&lt;/span&gt;
&lt;/label&gt;

<!-- end snippet -->

This is how a checked radio button should look like. I don't want to use an id, I want to make changes to css only if possible or keep html changes at minimum by adding classes, no ids **.

I tried many things including:

input[type=&quot;radio&quot;]:checked + label{
    border-color: #20c997;
}

and

.selectable:checked {
    border-color: #20c997;
}

and nothing is working

答案1

得分: 1

.selectable:has(input:checked){
  border: 2px solid green;
}
英文:

Try following selector:

.selectable:has(input:checked){
  border: 2px solid green;
}

huangapple
  • 本文由 发表于 2023年3月1日 11:57:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/75599450.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定