When to Nest Less Selectors and When to Flatten Them
Less nesting with the & parent selector keeps styles scoped, but deep nesting raises specificity and breaks overrides. Learn when to flatten.
16 Mar 2026, 21:56 UTC

You inherit a frontend codebase where every component is written in Less. The `.card` rule nests `.card:hover`, `.card--featured`, and `.card .title`. Six months later, a marketing site needs a one-off `.card-title` utility, but the compiled `.card .title` rule wins every time. The problem is not Less itself; it is how deep nesting compiles into high-specificity CSS that ordinary flat rules cannot override.
Less (Leaner Style Sheets) is a CSS preprocessor: you write `.less` files with nesting, variables, and mixins, and a compiler turns them into standard CSS that browsers understand. The `&` character is the parent selector, so `&:hover` inside `.card` compiles to `.card:hover`. Nesting keeps related styles together, but each level of nesting can add classes to the compiled selector, raising its specificity.
The useful rule of thumb: nest pseudo-states and component-scoped modifiers, but flatten structural children and anything another stylesheet might need to override.
What the & Parent Selector Compiles To
Inside a Less rule block, `&` refers to the parent selector. This example is representative of how Less transforms nested rules:
.card {
border: 1px solid #ddd;
padding: 1rem;
&:hover {
border-color: #0066cc;
}
&--featured {
background: #f4f8ff;
}
& .title {
font-size: 1.25rem;
font-weight: 600;
}
}
The compiler emits flat descendant selectors. The resulting CSS looks like this:
.card {
border: 1px solid #ddd;
padding: 1rem;
}
.card:hover {
border-color: #0066cc;
}
.card--featured {
background: #f4f8ff;
}
.card .title {
font-size: 1.25rem;
font-weight: 600;
}
Notice that `& .title` became `.card .title`. That two-class selector has higher specificity than a standalone `.title` rule, so a later flat utility such as `.title { font-weight: 900; }` will lose unless it uses `!important` or an even more specific selector.
Specificity Comparison
Specificity is the browser’s way of choosing which rule wins when multiple rules match the same element. More classes, IDs, and attribute selectors generally increase specificity; source order breaks ties.
| Less source | Compiled selector | Approximate specificity | Override risk |
|---|---|---|---|
| .card { &:hover { ... } } | .card:hover | 0,1,1 | Low; equal specificity, source order decides |
| .card { &--featured { ... } } | .card--featured | 0,1,0 | Low; a flat .featured rule loaded later wins by order |
| .card { & .title { ... } } | .card .title | 0,2,0 | High; a flat .title rule loses regardless of source order |
| .card { & .box & .tag { ... } } | .card .box .tag | 0,3,0 | Very high; difficult to override without !important |
Worked Example: Fixing a Nested Component Title
Suppose `button.less` contains this nested rule:
.button {
display: inline-flex;
align-items: center;
& .label {
font-weight: 600;
margin-right: 0.4rem;
}
}
Compile it from your project root with the Less CLI. Normal user permissions are sufficient; the command creates or overwrites `button.css`, so keep the `.less` file as your source of truth and do not edit compiled CSS directly.
# Run in your project root
npx lessc button.less button.css
Expected behavior: `button.css` contains `.button .label { font-weight: 600; margin-right: 0.4rem; }`. A risk is that `npx lessc` silently overwrites an existing `button.css`, so check the file timestamp after running.
Now a utility stylesheet loaded afterward defines `.strong-label { font-weight: 900; }`. Because `.button .label` has specificity 0,2,0 and `.strong-label` has 0,1,0, the utility never takes effect. The fix is to flatten the Less selector:
.button {
display: inline-flex;
align-items: center;
}
.button-label {
font-weight: 600;
margin-right: 0.4rem;
}
Then apply both classes in HTML: `Save`. The compiled `.button-label` rule has specificity 0,1,0, so `.strong-label` loaded later wins by source order.
Limitations and When to Stop Nesting
- Deep nesting increases compiled output size and makes selectors harder to read and debug.
- Less variables and mixins are compile-time only; they cannot change at runtime like CSS custom properties.
- Running Less.js in the browser is not recommended for production because of performance and flash-of-unstyled-content risks; compile during build instead.
- Less nesting syntax is not identical to the native CSS Nesting specification, so Less code will not run directly in browsers.
- Native CSS nesting now exists in modern browsers, so Less offers less exclusivity for brand-new greenfield projects.
A practical way to check your result: open the compiled CSS and search for selectors containing more than one class. If you see chains longer than two classes, consider flattening them. In the browser, use DevTools: inspect the element, open the Styles pane, and confirm which rule is actually applied when you expect a flat override to win.
Closing Checklist
- Nest `&:hover`, `&:focus`, and `&--modifier` when the variant belongs to one component.
- Flatten structural children such as `.card .title` into standalone classes like `.card-title`.
- Compile Less during your build step with `lessc`, not in the browser.
- Review compiled CSS for long descendant chains before deploying.
Nesting is a scoping tool, not a default. Use it where it keeps related styles together, and flatten where specificity would otherwise trap you.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.