Syntax
.parent {
& .child { }
&:hover { }
}Examples
Basic Nesting
Nesting descendant selectors and pseudo-classes inside a parent rule.
.card {
padding: 1rem;
background: white;
&:hover {
background: #f3f4f6;
}
& .title {
font-size: 1.25rem;
font-weight: bold;
}
}Nesting Media Queries and Modifiers
Keeping related responsive and state styles physically close to the base rule.
.button {
padding: 8px 16px;
&.primary {
background: blue;
}
@media (min-width: 768px) {
padding: 12px 24px;
}
}Parameters and values
- & (nesting selector): Explicitly references the parent selector within a nested rule
Best practices
- Use native nesting to keep a component's related styles - base, hover states, modifiers, responsive rules - physically grouped together
- Use & explicitly when nesting a compound selector (&.active) or pseudo-class (&:hover), since omitting it changes the meaning to a descendant selector instead
- Avoid nesting too many levels deep - beyond 2-3 levels, generated specificity and selector length both grow, hurting maintainability
- This is now broadly supported without a preprocessor, but check your specific browser support target if you need to support noticeably older browsers
At a glance
- Purpose
- Presentation and layout
- File extension
- .css
- Runs in
- Web browsers
- Usually used with
- HTML and JavaScript
Specifications & further reading
Related CSS documentation
Basic Selectors
CSS selectors target elements to style. Type selectors match by tag name, class selectors (.name) match elements with that class, ID selectors (#name) match a single unique element, and the universal selector (*) matches everything. Classes are the most commonly used selector for reusable styling.Combinators
Combinators define relationships between selectors. The descendant combinator (space) matches any nested element. The child combinator (>) matches only direct children. The adjacent sibling combinator (+) matches the very next sibling. The general sibling combinator (~) matches all following siblings.Attribute Selectors
Attribute selectors target elements based on the presence or value of an HTML attribute, using square bracket syntax. They support several matching modes beyond exact equality, including starts-with, ends-with, and contains, making them powerful for targeting elements without needing extra classes.:has() - The Parent Selector
:has() matches an element if any of the selectors passed to it match something within it - effectively letting you style a parent based on its children or descendants, something CSS could never do natively before. It has broad, mainstream browser support as of 2026 and is safe to use in production.
CSS selectors target elements to style. Type selectors match by tag name, class selectors (.name) match elements with that class, ID selectors (#name) match a single unique element, and the universal selector (*) matches everything. Classes are the most commonly used selector for reusable styling.Combinators
Combinators define relationships between selectors. The descendant combinator (space) matches any nested element. The child combinator (>) matches only direct children. The adjacent sibling combinator (+) matches the very next sibling. The general sibling combinator (~) matches all following siblings.Attribute Selectors
Attribute selectors target elements based on the presence or value of an HTML attribute, using square bracket syntax. They support several matching modes beyond exact equality, including starts-with, ends-with, and contains, making them powerful for targeting elements without needing extra classes.:has() - The Parent Selector
:has() matches an element if any of the selectors passed to it match something within it - effectively letting you style a parent based on its children or descendants, something CSS could never do natively before. It has broad, mainstream browser support as of 2026 and is safe to use in production.