As pointed out in arieljuod's comment above, the behavior of <input>
radio elements aligns with your description. By assigning the same name
attribute to each radio button within a group, you can create a radio group.
If you're open to alternatives beyond using <button>
, class
, and :focus
, consider employing
<input type="radio">
,
name
, and
:checked
, coupled with
<label>
for stylized button-like effects.
Note the usage of the adjacent sibling combinator +
, which targets <span>
only when it directly follows a checked input.
.radio-button input {
display: none;
}
.radio-button span {
padding: 0.1em 0.6em;
background-color: rgb(239, 239, 239);
border: 2px outset rgb(118, 118, 118);
text-align: center;
}
.radio-button input[name="a"]:checked + span {
background-color: green;
}
.radio-button input[name="b"]:checked + span {
background-color: blue;
}
.radio-button input[name="c"]:checked + span {
background-color: red;
}
<label class="radio-button">
<input type="radio" name="a">
<span>a1</span>
</label>
<label class="radio-button">
<input type="radio" name="a">
<span>a2</span>
</label>
<label class="radio-button">
<input type="radio" name="b">
<span>b1</span>
</label>
<label class="radio-button">
<input type="radio" name="b">
<span>b2</span>
</label>
<label class="radio-button">
<input type="radio" name="b">
<span>b3</span>
</label>
<label class="radio-button">
<input type="radio" name="c">
<span>c1</span>
</label>
<label class="radio-button">
<input type="radio" name="c">
<span>c2</span>
</label>
For more information, check out:
How to Style a Selected Radio Buttons Label?