Sub topic 1
Writing a rule inside a rule
Prerequisites
This chapter assumes you already know:
- CSS selectors: class, type, attribute, and pseudo-class selectors.
- Combinators: descendant, child, and sibling.
- Basic cascade and specificity rules.
- Media queries.
You do not need Sass or any build tool. Native nesting runs in the browser.
What nesting does
Native CSS nesting lets you write a style rule inside another style rule. The inner rule applies only to elements that match the outer rule's selector, combined with the inner selector. The outer rule is the parent. The inner rule is the nested rule.
Before nesting, you repeated the parent selector for every related rule:
.card {
padding: 1rem;
border: 1px solid #ccc;
}
.card .title {
font-weight: 600;
}
.card:hover {
border-color: #666;
}With nesting, you group those rules inside .card:
.card {
padding: 1rem;
border: 1px solid #ccc;
& .title {
font-weight: 600;
}
&:hover {
border-color: #666;
}
}The two versions produce the same result. The nested & .title rule is equivalent to .card .title. The nested &:hover rule is equivalent to .card:hover.
How the browser reads it
The & is the nesting selector. It stands for the parent rule's selector. The browser takes each nested selector, replaces & with the parent, and matches the result. This chapter only introduces the idea. Later chapters cover & in detail.
Declarations and nested rules together
A parent rule can hold plain declarations and nested rules at the same time. Declarations such as padding apply to the parent selector itself. Nested rules apply to the combined selectors.
In the example above, padding and border style .card. The nested rules style .title inside a card and a card in the hover state.
You can also nest more than one level. Each level adds to the selector built from the levels above it:
.card {
& .body {
color: #333;
& a {
text-decoration: underline;
}
}
}The inner rule & a resolves to .card .body a. Deep nesting makes selectors long and hard to override, so keep it shallow. A later chapter covers how deep to go.
Common errors
- Forgetting that the parent is not repeated. Inside
.card, write& .titleor.title, not.card .title. Writing.card .titleinside.cardmeans.card .card .title, which almost never matches. - Expecting string concatenation. In Sass,
&-titlebuilds the class.card-title. Native nesting does not do this.&-titleis invalid. - Leaving out the parent's braces. Every nested rule must sit inside the parent's
{ }. A missing closing brace makes the browser misread the rest of the stylesheet. - Assuming old browsers ignore it safely. A browser without nesting support drops the nested rules. The parent's own declarations still apply.
Try it
Save this file as nesting.html and open it in a current browser. Hover over the box to see the border change.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Nesting demo</title>
<style>
.card {
padding: 1rem;
border: 2px solid #ccc;
max-width: 20rem;
& .title {
font-weight: 600;
color: #0a4;
}
&:hover {
border-color: #666;
}
}
</style>
</head>
<body>
<div class="card">
<p class="title">Nested rules</p>
<p>Hover over this card.</p>
</div>
</body>
</html>The title turns green and bold because & .title resolves to .card .title. The border darkens on hover because &:hover resolves to .card:hover.
Exercise
Inside a parent rule .menu { ... }, you write a nested rule & a { color: blue; }. Which selector does it resolve to, and which elements does it style?
Show answer
It resolves to .menu a. It styles every a element that is a descendant of an element with the class menu.
Exercise
A stylesheet contains .box { & .box-label { margin: 0; } }. Write the same rule without nesting.
Show answer
.box .box-label { margin: 0; }