Home > Web Front-end > CSS Tutorial > Can CSS Classes Start with Numbers? Workarounds and Best Practices.

Can CSS Classes Start with Numbers? Workarounds and Best Practices.

DDD
Release: 2024-11-02 18:47:30
Original
999 people have browsed it

Can CSS Classes Start with Numbers? Workarounds and Best Practices.

Workarounds for CSS Classes Starting with Numbers

While the use of CSS classes starting with numbers is discouraged, there are workarounds to make them valid.

Validity of Class Names

According to HTML5, class names can start with letters (a-z or A-Z), underscore (_), or a hyphen (-). However, they cannot contain whitespace. Therefore, class names such as .000000-8 and .8FFFFFF are valid.

Invalidity of CSS Selectors

CSS syntax rules prevent CSS identifiers from starting with an unescaped digit. Therefore, selectors like .000000-8 are invalid.

Workaround: Escaping Digits

To make CSS selectors valid, the leading digit can be escaped using CSS escaping rules. This can be achieved in the following ways:

. 00000-8
Copy after login

or

.003000000-8 
Copy after login

These escaped selectors will match elements with class names 000000-8.

Recommendations

While these workarounds allow the use of CSS classes with leading digits, it is generally advisable to avoid such naming conventions. This helps ensure compatibility with all browsers and maintains code readability.

The above is the detailed content of Can CSS Classes Start with Numbers? Workarounds and Best Practices.. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template