Is it possible to check the default checkbox in the stylesheet and not in the HTML attribute?

As in the title: can the checkbox be checked by default in the stylesheet, and not in the embedded HTML attribute?

Example from w3schools.com, the β€œcar” checkbox is checked:

<form action="demo_form.asp"> <input type="checkbox" name="vehicle" value="Bike"> I have a bike<br> <input type="checkbox" name="vehicle" value="Car" checked> I have a car<br> <input type="submit" value="Submit"> </form> 

I check the box "I agree to the Terms of Use", and because of the clumsy website I am doing this, I can not create inline CSS. Instead, I can assign this field to the css class and edit the class in a larger stylesheet.

If this simplifies, this will be the only checkbox on the page.

+5
source share
2 answers

Unfortunately, the checkbox is not set in CSS. It relies on the checked attribute of the input element, and the attributes cannot be changed using CSS.

Alternatively, you could study the JavaScript solution, but of course, the best way would be to directly edit the HTML.

+8
source

First of all, this is not css, but an attribute of the html element.

Another way to test this with javascript, and with css you can only select it like this:

 input[type=checkbox]:checked /* select checked checkbox */ input[type=checkbox] /* select any checkbox */ 
+2
source

Source: https://habr.com/ru/post/1232558/


All Articles