Home >Web Front-end >JS Tutorial >Can I Use a Number as the First Character in an HTML Element's ID?
Dividing an element's ID with a number may seem like an unusual practice, but it's entirely possible in HTML. However, it's essential to note that while valid, using numeric IDs can introduce challenges when selecting and styling elements with CSS.
HTML permits ID values composed solely of digits; any character but a space is considered acceptable. Browsers have historically supported this practice, leading to the relaxation of stricter restrictions in the HTML5 specification.
While using numeric IDs in HTML is technically feasible, exercising caution when using them in CSS selectors is prudent. CSS id selectors do not allow IDs beginning with digits, necessitating their escape. For instance, #12 is invalid as a CSS selector and must be specified as #3132.
For those intending to use numeric IDs with CSS selectors, starting with a letter is recommended for simplicity. Nonetheless, below is an example demonstrating the use of a div with the ID "12" and various methods of manipulating it:
Please note that support for document.querySelector may vary across browsers. Browsers that support this function will be able to apply style changes using the latter method.
The above is the detailed content of Can I Use a Number as the First Character in an HTML Element's ID?. For more information, please follow other related articles on the PHP Chinese website!