gpt4 book ai didi

html - CSS选择组的单选按钮在检查一个之前

转载 作者:太空宇宙 更新时间:2023-11-04 07:38:02 24 4
gpt4 key购买 nike

我想为一组中的一些单选按钮设置样式。我不能使用任何 JS,所以只能使用 css。

<input type="radio" name="attr_1" value="1">
<input type="radio" name="attr_1" value="2">
<input type="radio" name="attr_1" value="3">
<input type="radio" name="attr_1" value="4">
<input type="radio" name="attr_1" value="5">

使用 :checked 选择器,我可以更改所选输入的外观,但我希望能够选择所有单选按钮,其值低于我选中的单选按钮。

input[type='radio'] {
background-color: red
}

input[type='radio']:checked + input {
background-color: black
}

例如,如果选择值为 3 的单选按钮,它将具有红色背景,但我也希望 1 和 2 具有红色背景。使用 JS 我很容易做到这一点,但我只能使用 CSS。

有什么方法可以让我拥有所有后续输入,而不是仅在 ckecked 后面的 1 个输入?

最佳答案

感谢@mpro,我可以想出一种方法来使我的所有后续样式与选中元素之前的样式区分开来。

<input type="radio" name="attr_1" value="1">
<input type="radio" name="attr_1" value="2">
<input type="radio" name="attr_1" value="3">
<input type="radio" name="attr_1" value="4">
<input type="radio" name="attr_1" value="5">

input {
background-color: red;
}
input:checked ~ input {
background-color: black
}

这要求我至少选择一个 Radiobutton,但我还是做到了。感谢大家的帮助

关于html - CSS选择组的单选按钮在检查一个之前,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48776443/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com