Organize Less component styles with nesting and &
Less nesting with the & parent selector keeps component variants and states in one block. Learn when it helps and how to check compiled specificity.
24 Jun 2026, 23:04 UTC

The problem: repeating the same component selector
You open a stylesheet to adjust a button's hover color, but the rule is three screens away. The file already contains .button, .button:hover, .button:focus, .button--primary, .button--primary:hover, and .button--disabled. If you rename the component, every selector needs a find-and-replace. That repetition is the default cost of flat CSS.
What Less nesting and & actually do
Less lets you place rules inside other rules. The & character is a parent selector reference: inside a .button block, &:hover becomes .button:hover after compilation. A variant like &--primary compiles to .button--primary. Browsers never see the nesting; Less flattens everything into ordinary CSS during your build step.
Worked example: a button component in one block
Here is a Less source file that keeps the base button, its states, and two variants together:
.button {
display: inline-flex;
align-items: center;
padding: 0.5rem 1rem;
border: 1px solid transparent;
border-radius: 4px;
cursor: pointer;
&:hover {
border-color: #333;
}
&:focus-visible {
outline: 2px solid #0066cc;
outline-offset: 2px;
}
&--primary {
background: #0066cc;
color: #fff;
border-color: #0066cc;
&:hover {
background: #0055aa;
}
}
&--disabled {
opacity: 0.5;
cursor: not-allowed;
pointer-events: none;
}
}
Compiling that block produces flat selectors such as .button:hover, .button--primary, and .button--primary:hover. The relationship between the component and its variants stays visible in the source, while the browser receives standard CSS.
Less version note: this & resolution behavior is stable across recent Less 3.x and 4.x releases. If you maintain an older project, check the release notes for your installed compiler before relying on nested variant syntax.
Run the compiler from your project terminal:
lessc styles.less styles.css
- Where to run: a terminal in the project directory that contains
styles.less. - Permissions: normal user access; you need write permission for the output file or directory.
- Placeholders: replace
styles.lesswith your input file andstyles.csswith your desired output path. - Expected check: the command exits without errors and
styles.csscontains.button:hoverand.button--primary:hover. - Risk: if
styles.cssalready exists, it is overwritten. Use a different output name or back up the file first.
Trade-off: specificity and how deep to nest
Nesting is authoring convenience, not a free optimization. Each level can add to selector specificity, making rules harder to override later. A selector such as .card .title a:hover carries more weight than a flat class like .card-title-link. Less also compiles & literally against the full parent, so complex parents can produce compound selectors you did not intend.
A practical limit is three or four levels of nesting. If a block needs more, consider splitting it into separate components or flattening the selector manually.
To verify what you are actually shipping, open the compiled CSS in browser DevTools. Inspect an element styled by a nested rule and compare its specificity against a flat equivalent. If the nested rule wins unexpectedly, that is a sign to reduce nesting or adjust the cascade.
Check the result before you ship it
- Write a
.lessfile with one component block and nested states. - Compile it with
lessc input.less output.css. - Search the output for the expected selectors, for example
grep -E '\.button:|\.button--' output.css. - Open the page in DevTools, select a styled element, and confirm the applied rules match the compiled output.
Start with one component. Keep its states and variants inside a single block, compile to CSS, inspect the output, and stop nesting when selectors reach three or four levels. The .less file is your source; the compiled CSS is what users download.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.