Home >Web Front-end >CSS Tutorial >Can HTML IDs Start with Numbers?

Can HTML IDs Start with Numbers?

Patricia Arquette
Patricia ArquetteOriginal
2024-12-20 17:41:09507browse

Can HTML IDs Start with Numbers?

Can HTML Elements Have IDs Starting with Numbers?

Yes, it is valid to have an HTML element with an ID that starts with a number. IDs can consist of any non-whitespace character except space, including numbers.

However, Using IDs Starting with Numbers in CSS Selectors Can Be Problematic:

While valid in HTML, using IDs starting with numbers in CSS selectors can be challenging. To select an element with such an ID, the leading number must be escaped using the ASCII character code, which can be inconvenient and error-prone. For example, to select an element with the ID "12", the CSS selector would be #3132.

Recommendations:

If you plan to use CSS selectors to style or target elements with numeric IDs, it's generally recommended to start the IDs with letters to avoid the need for escaping. For example, #my-element-12 would be a more practical choice than #12.

The above is the detailed content of Can HTML IDs Start with Numbers?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn