Explain Codes LogoExplain Codes Logo

Are class names in CSS selectors case sensitive?

html
css-selectors
case-sensitivity
css-properties
Anton ShumikhinbyAnton Shumikhin·Jan 29, 2025
TLDR

Yes, CSS class selectors are case-sensitive. These two might look like siblings, but they get treated like complete strangers: .menuItem versus .menuitem.

Example:

<div class="menuItem">Well-dressed</div> <div class="menuitem">Naked</div>
.menuItem { color: red; /* Don't worry, .menuItem, you're not blushing. It's just CSS.*/ }

Getting to the crux of case sensitivity

HTML and CSS: A tale of two sensitivities

Strangely enough, HTML class names and CSS class selectors don't share the same viewpoint on cases. In CSS, class selectors are case-sensitive. But when it comes to HTML, class names are actually case-insensitive. Confused much? Let's go deeper.

The fuss about CSS properties

By default, CSS properties, values, pseudo-classes, and element names are case-insensitive. So whether you're shouting in all caps (e.g. DISPLAY: BLOCK;) or whispering in lowercase (e.g. display: block;), CSS hears you just fine.

.block { DISPLAY: BLOCK; /* CSS: 'Alright! Alright! I heard you the first time!' */ }

The Document Object Model and quirks mode

The DOM (Document Object Model) in browsers can influence the way your CSS behaves. Most modern browsers adhere to a standard mode, which treats selectors as case-sensitive. However, the quirks mode throws a wrench into the works by treating all selectors as case-insensitive.

The future of CSS and case insensitivity

There's a glimmer of hope for those seeking case insensitivity in CSS selectors. Selectors Level 4, which is still in draft, proposes the ability to make attribute searches case-insensitive using the "i" flag, like so: [class~="SelfCatering" i].

/* It's certainly nice to have options, right? */ input[value="BACON" i] { background-color: yellow; } /* CSS4 */ [class~="breakfast" i] { background-color: yellow; } /* CSS4 */

Case Sensitive CSS classes:

💂‍♀️ Maggie: Very specific, only responds to "Maggie". 👱‍♀️ maggie: Different entity, only responds to "maggie".

Case Insensitive:

👮‍♀️Charlie: Equally responds to both "Charlie" and "charlie".


## Developer's guide: navigating case sensitivity
### Consistency is king
Inconsistencies can make code maintenance a nightmare and ruin your weekend plans. Be that **mindful developer** who sticks to consistent class naming in both your HTML and CSS.

### Embrace modern doctypes
Don't let your HTML live in the past. Use a **modern HTML doctype**, because consistency across all browsers is what we're all about.

### Mind the language-specific rules
Are you a **polyglot** programmer dealing with languages like **XML** that are **case-sensitive?** Make sure to remember these language-specific rules and quirks. 

### Be future-proof
Get acquainted with the **Selectors Level 4** proposal, and be that developer who's always prepared for the future.

## Potential gotchas and how to tackle them
### Different browsers, different rules
Browsers can sometimes be that pesky kid who refuses to follow the rules. Make sure you **test thoroughly** across different browsers to ensure a consistent experience.

### Team chaos
When working in teams, maintaining a common **CSS naming convention** can save you from mixed-up class names and a lot of heartache.

### Dynamic fails
Handling **dynamically generated class names** can be tricky. Ensure that your JavaScript or server-side languages match the cases used in your CSS.