CSS
Web Development
Programming
HTML
Cascade Style Sheets

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:

  1. 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).
  2. 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.
  3. 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, \31 is 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:

  1. Maintain Readability: Stick to alpha-numeric characters and simple separators like hyphens or underscores.
  2. Avoid Obscure Escapings: Too much reliance on escaped characters can make your CSS difficult to read and maintain.
  3. 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:

css
1.valid-class {}
2.\#hashTag { color: red; }  /* Using hash symbol but escaped */
3.\@atSign { font-weight: bold; } /* Using @ sign but escaped */
4.\--modifier { font-style: italic; }
5._underscore { text-decoration: underline; }
6.invalid class {} /* Invalid due to space */

Summary Table of Character Usage in CSS Class Names

Character TypeExamplesValidityNotes
Alphanumerica-z, A-Z, 0-9YesCommon practice
Hyphen-YesCan start with hyphen
Underscore_Yes
Unicodeä, é, 主YesMust be valid Unicode
Special Escaped# { }Yes, with proper escapingRarely used, affects readability
SpaceNoSpace 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.


Course illustration
Course illustration

All Rights Reserved.