Course contentsShow
← Web Development
Lesson 1352 of 2,98224. CSS Architecture and MaintainabilityFree lesson

Writing Low-Specificity Selectors

Master techniques for keeping selectors simple and avoiding unnecessary nesting and qualification.

Writing Low-Specificity Selectors

You'll learn how to write simple, maintainable selectors that avoid the specificity war problem.

The Core Principle

After understanding the specificity war problem, the solution is straightforward: keep your selectors as simple as possible. Low-specificity selectors are easier to override when needed, make your CSS more predictable, and prevent the need for increasingly complex selectors down the line.

Think of specificity like elevation on a map. If you start building everything at sea level (single classes), you can easily add height later when needed. But if you start building at 5,000 feet (deeply nested selectors with multiple IDs), you've left yourself nowhere to go but even higher—and that's exhausting.

Best Practices

Prefer single classes over complex combinations:

/* Good: Low specificity (10) */
.button { }

/* Avoid: High specificity (20) */
.header .nav .button { }

Avoid unnecessary qualification:

/* Good: Just the class (10) */
.alert { }

/* Avoid: Element + class (11) */
div.alert { }

Don't nest when you don't need to:

/* Good: Specific enough with one class */
.card-title { }

/* Avoid: Unnecessary context */
.card .card-header .card-title { }

Use naming conventions (like BEM) to create specificity through names, not selectors:

.card__title { }          /* Clear, low specificity */
.card__title--large { }   /* Easy to override */

Key Takeaway: Keep selectors flat and simple—use single classes whenever possible, avoid unnecessary nesting, and rely on clear naming conventions rather than selector complexity to convey relationships.