Which characters are valid in CSS class names/selectors?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
In the world of web development, Cascading Style Sheets (CSS) is a cornerstone technology used to design and format the presentation of web pages. One of the fundamental aspects of CSS usage involves class selectors which are extensively used to specify styles to HTML elements. Here, we'll delve into the rules and conventions concerning the characters that can be used in CSS class names.
Understanding CSS Syntax and Characters in Class Names
CSS class selectors start with a period (.) followed by the class name. The class name can include a variety of characters, but there are specific rules and practical considerations to keep in mind:
Basic Rules:
- Standard Characters: Initially, class names can consist of letters ([a-zA-Z]), digits ([0-9]), hyphens (
-), and underscores (_). However, if a class name begins with a digit, a hyphen followed by a digit, or contains special characters, it should be handled with care (details explained below). - Starting with a Hyphen: Classes can start with a hyphen, for example,
. -example, but this is uncommon in practice due to potential confusion and readability issues. - Unicode Characters: CSS class names can include Unicode characters, e.g.,
.button-主, which can be quite useful for non-English applications.
Escaping Special Characters:
In CSS, if you want to use special characters or start a class name with a digit, you must use escaping techniques. This ensures the browser parses the characters correctly.
- Example: To use a class name that starts with a digit, such as
1stElement, you would define it in CSS as.\31 stElement { color: blue;}. Here,\31is the Unicode code point for the digit 1.
Use of ASCII Characters:
- Class names can include ASCII characters from 33 to 126, except for the space character, which is ASCII 32. This includes characters such as
!,$,%, and@, but they must be escaped properly in CSS.
Practical Considerations and Best Practices:
While technically possible to use a wide range of characters, certain practices are advisable to maintain code readability, understandability, and minimize errors:
- Maintain Readability: Stick to alpha-numeric characters and simple separators like hyphens or underscores.
- Avoid Obscure Escapings: Too much reliance on escaped characters can make your CSS difficult to read and maintain.
- Naming Convention: Adopt a consistent naming convention like BEM (Block Element Modifier) which uses hyphens and underscores to define relationships and states, such as
.block__element--modifier.
Examples of Valid Class Names
Here are a few examples illustrating valid and invalid class names and their usage:
Summary Table of Character Usage in CSS Class Names
| Character Type | Examples | Validity | Notes |
| Alphanumeric | a-z, A-Z, 0-9 | Yes | Common practice |
| Hyphen | - | Yes | Can start with hyphen |
| Underscore | _ | Yes | |
| Unicode | ä, é, 主 | Yes | Must be valid Unicode |
| Special Escaped | # { } | Yes, with proper escaping | Rarely used, affects readability |
| Space | No | Space character is not allowed |
Conclusion
While you can exercise a lot of flexibility in naming CSS class selectors, adhering to simpler, universally accepted characters (alphanumeric, hyphens, underscores) and logical naming conventions enhances your stylesheet's readability and maintainability. The use of special or Unicode characters, although supported, should be approached with consideration of the broader implications on your code's clarity and portability.

