CSS Nesting: Grouping Related Styles

CSS nesting groups related styles like folders for files. Instead of repeating parent selectors, you write child rules inside the parent. This is great for component-based styling, but over-nesting creates specificity headaches that are hard to override.
Why it exists
Before native nesting, styling complex components meant repeating parent selectors over and over (e.g., .card .title, .card .button). This was verbose, error-prone, and made stylesheets hard to read. CSS Nesting was created to provide the organizational benefits of preprocessors like Sass directly in the browser.
The mental model
Think of CSS nesting as creating a visual hierarchy in your stylesheet that mirrors your HTML structure. A .card rule can contain the styles for its .title and .button children directly inside its curly braces. This co-locates related styles, making it easier to find, understand, and maintain the styling for a complete component in one place.
How it works
You write a style rule for a parent element. Inside its declaration block ({ ... }), you can then write new style rules for its descendants. The browser automatically combines the parent selector with the nested child selector. For example, a td rule nested inside a table rule is interpreted by the browser as table td. To refer to the parent selector itself, such as for adding a pseudo-class, you use the ampersand (&) symbol, as in &:hover.
When to use it
Nesting is ideal for self-contained components like cards, modals, or navigation bars where styles are tightly coupled to the HTML structure. It dramatically improves readability and maintainability for these modules. It's also perfect for applying state-based styles with pseudo-classes (&:hover, &:focus) or modifier classes (&.is-active).
When not to use it
Avoid nesting too deeply. A good rule of thumb is to go no more than three levels deep. Each level of nesting increases selector specificity, which can make your CSS difficult to override later, leading to "specificity wars" and the temptation to use !important. Only nest rules that are truly contextually dependent on the parent.
One canonical example
Without nesting, styling a card's title and hover state requires separate rules: .card { ... }, .card h2 { ... }, and .card:hover { ... }. With nesting, you can write this more concisely: .card { background: white; border-radius: 8px; h2 { font-size: 1.5rem; } &:hover { box-shadow: 0 4px 8px rgba(0,0,0,0.1); } } . Notice how the h2 is directly inside .card, and & is used for the :hover state.
Interview question
Which of the following is the primary benefit of using CSS nesting for styling components?
- a.It enhances stylesheet readability and maintainability by co-locating all related styles for a specific component.Correct
- b.It automatically generates unique class names for nested elements to prevent global style clashes.
- c.It allows developers to define custom CSS properties and functions directly within a component's scope.
- d.It significantly reduces the final compiled CSS file size by optimizing selector declarations.
Why? this is the answer
The card states that nesting "co-locates related styles, making it easier to find, understand, and maintain the styling for a complete component in one place." While it reduces authoring verbosity, its primary benefit is not file size reduction (option D) as the compiled CSS still contains full selectors.
Just read this? Test yourself on what you have been reading.
Read the original → developer.mozilla.org
Put your scrolling time to good use
Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on css — each one lists the topics its interview covers.
See open roles