Toggle radio input using CSS only

前端 未结 6 1741
慢半拍i
慢半拍i 2020-12-09 04:00

The following code use a script to toggle/uncheck a radio when clicked a second time on the same.

My question is how do I do this using CSS only?

6条回答
  •  没有蜡笔的小新
    2020-12-09 04:40

    The answer is you can't unselect or uncheck a radio button in CSS only, as the radio button only becomes unchecked once you click on a different radio button. As only one radio button can be active at once, this will uncheck the previously checked radio button.

    input:checked + label {
        color: green;
    }
    
    input:not(:checked) + label {
        color: red;
    }
    

    So you'll have to stick with using the JS function you posted.

    Here are a couple of nice articles with further explanation :

    CSS Click Events

    How To Generate CSS Click Events

提交回复
热议问题