Sub topic 13
Keeping nested CSS maintainable
This chapter assumes you know how the & selector works, how specificity is calculated, and how conditional at-rules nest inside style rules. It covers how to decide how much to nest.
Nesting adds specificity
Every level of nesting adds a selector to the compiled result. Three levels of class nesting produce a three-class selector:
.card {
.body {
.title {
a {
color: #06c;
}
}
}
}
/* Equivalent to: .card :is(.body :is(.title a))
Specificity: (0,3,1) */That rule is hard to override later. Any override must also be at least (0,3,1), or it has to come later in the source. It also breaks if you move the markup, because the selector now describes four levels of HTML.
A working limit
A common guideline is to nest no more than two or three levels. Past that, flatten the rule:
.card {
padding: 1rem;
.card-title {
font-weight: 600;
&:hover {
text-decoration: underline;
}
}
}Here .card-title is a class that describes the element itself. It does not depend on where the element sits. The rule still reads as part of .card, but its specificity stays at (0,2,0), and &:hover adds only a pseudo-class.
What is worth nesting
Some rules gain the most from nesting:
- States and modifiers such as
&:hover,&:focus-visible, and&.is-open. They belong to the parent by definition. - Conditional at-rules such as
@media,@supports, and@container. Keepingpadding: 2remnext to the basepadding: 1remshows every variation of a property in one place. - Direct parts of a component that are only meaningful inside it.
Avoid nesting that only saves typing. If you write .a { .b { .c { } } } because the parent class is short, you are paying for a few characters with extra specificity.
Keep specificity low on purpose
If you need the nested structure but not the weight, wrap the parent part in :where(). It always contributes zero specificity:
:where(.card) {
.title {
font-size: 1.25rem;
}
}
/* Equivalent to: :where(.card) .title
Specificity: (0,1,0) */Also watch selector lists. If any item in the parent list is an ID, every nested rule takes the ID's weight, as in #main, .note { & p { } }. Do not mix IDs and classes in a nested parent list.
Sass habits that do not carry over
Native nesting cannot build names by string concatenation. This works in Sass but is invalid in CSS:
.card {
&__title {
font-weight: 600;
}
}Write .card__title as its own rule, or nest a class selector inside the parent. Also keep declarations before nested rules when you can. This is not required. Newer browsers handle declarations placed after nested rules, but mixing them makes the source harder to scan.
Fallbacks for older browsers
A browser that does not support nesting ignores the nested rules and applies the parent's own declarations. Check your browser support targets. If you have to support an older browser, run a build step that flattens nesting, such as a PostCSS plugin, instead of hand-writing duplicate rules.
Review checklist
- Can you state each nested selector's full flat form and specificity?
- Is the nesting depth three levels or less?
- Are
@mediaand@supportsplaced inside the rule they change? - Does any nested rule use
&to append a suffix, as in&__x? - Could a descendant selector be replaced by a class on the element?
Exercise
A stylesheet has .page { .content { .list { li { margin: 0; } } } }. What is the specificity of the resulting selector, and how would you reduce it?
Show answer
The flat selector is .page .content .list li, with specificity (0,3,1). Give the items their own class, such as .list-item, and nest it one level inside .list, or wrap the outer part in :where(), as in :where(.page .content) .list li. Either way, the selector stops depending on the full markup structure.