Sub topic 3
When You Can Leave Out the &
This chapter assumes you know class, type, attribute, and pseudo-class selectors, the descendant, child, and sibling combinators, and basic specificity. It also assumes you have read the previous chapter on the & nesting selector. Here you will learn when a nested selector can drop its &, what the browser does when it isn't there, and which mistakes this shortcut causes.
The rule: a missing & means "descendant of the parent"
When you write a nested selector with no &, the browser treats it as relative to the parent rule. It adds an implicit & (an & followed by a descendant combinator) to the front. These two rules mean the same thing:
.card {
.title {
font-weight: 600;
}
}
.card {
& .title {
font-weight: 600;
}
}Both compile to .card .title. The & is never required for a plain descendant. It is only required when you want to attach something to the parent itself, such as a class, a pseudo-class, or an ancestor.
What "relaxed" syntax means
The first browser releases of nesting were stricter. A nested selector could not begin with a type selector (an element name such as a, p, or li). Nested rules had to start with ., #, :, [, *, &, or a combinator. Writing a { ... } directly inside .nav was invalid, and you had to write & a.
The relaxed syntax removed that restriction. Every nested selector can now be written without &, including ones that start with an element name:
.nav {
display: flex;
a {
padding: 0.5rem;
}
li {
list-style: none;
}
}This is the same as .nav a and .nav li. The relaxed syntax shipped in Chrome 120, Safari 17.2, and Firefox 117. Chrome 112 to 119 and Safari 16.5 to 17.1 support nesting but not the relaxed form. Check current browser support data before you rely on these numbers.
Forms you can write without &
The table lists each nested form and what it resolves to, with the parent .nav.
| Nested selector | Resolves to | Notes |
|---|---|---|
| `a` | `.nav a` | Type selector, valid only in relaxed syntax |
| `.item` | `.nav .item` | Descendant, not compound |
| `#logo` | `.nav #logo` | ID selector |
| `[href]` | `.nav [href]` | Attribute selector |
| `:hover` | `.nav :hover` | Descendant that is hovered, not the nav itself |
| `> li` | `.nav > li` | Leading combinator |
| `+ .banner` | `.nav + .banner` | Next sibling of the parent |
| `~ .banner` | `.nav ~ .banner` | Later sibling of the parent |
A leading combinator replaces the implicit descendant combinator. You don't get & > li and a descendant on top. The & is still inserted in front, but the space is not.
Where leaving out the & goes wrong
Pseudo-classes without & target descendants
This is the most common error. The following code does not style the card on hover:
.card {
border: 1px solid #ccc;
:hover {
border-color: #666;
}
}It resolves to .card :hover, which matches any hovered element inside the card. The card's own border never changes. To style the card itself, attach the pseudo-class with &:
.card {
border: 1px solid #ccc;
&:hover {
border-color: #666;
}
}The same applies to classes. .is-open nested in .nav means .nav .is-open. To match an element that is both .nav and .is-open, write &.is-open.
Compare the two spellings side by side
.nav {
.is-open {
background: #eee; /* .nav .is-open: a descendant */
}
&.is-open {
background: #ddd; /* .nav.is-open: the same element */
}
}Think of the rule this way. A space means "inside", so no & gives you a descendant. No space means "same element", so & glued to a selector gives you a compound.
Ancestor context needs &
You cannot put a selector before the parent without writing the & yourself. Without it, the browser always puts the parent first.
.nav {
.dark-theme & {
background: #222;
}
}This resolves to .dark-theme .nav. Writing .dark-theme .nav as a normal nested rule, with no &, would give .nav .dark-theme .nav.
Starting a nested rule with a type selector and a colon
The relaxed syntax makes the parser decide whether a line is a declaration or a nested rule. For a:hover { ... } this is not a problem, because the block starting with { marks it as a rule. Keep the braces and the line is parsed correctly. Placing such rules in browsers that lack relaxed support is the problem, as the next section shows.
What older browsers do
A browser with no nesting support and a browser with only the strict syntax handle a relaxed rule differently from what you might expect. The nested rule is invalid for them, so they skip it, and the parent rule's own declarations normally still apply. You lose the nested styles but the page doesn't break.
.nav {
display: flex; /* kept by every browser */
a { /* dropped by strict-only browsers */
padding: 0.5rem;
}
& li { /* works in all nesting browsers */
list-style: none;
}
}If your support targets include Chrome 112 to 119 or Safari 16.5 to 17.1, write & a for type selectors. If you only support current browsers, the shorter form is fine. Test in your actual target browsers, because error recovery details are the part that differs between engines.
A decision flow
Specificity does not change when you drop the &
The implicit & has the same value as the explicit one. It takes the parent's specificity, and the rest of the nested selector adds to it. For .card { .title { } }, the result is .card .title, with specificity (0,2,0). A nested a in .nav gives (0,1,1), the same as .nav a.
Selector lists in the parent are the one case that needs care. A parent list such as #main, .note behaves like :is(#main, .note), so a nested p has the specificity of the most specific item, the ID. Specificity of nested selectors and lists has its own chapter later in this tutorial. For now, remember that omitting & never makes the nested rule more or less specific than writing it.
A worked example
Here is a complete page you can open in a current browser. Every nested rule omits & except the ones that must attach to the parent.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Nesting without &</title>
<style>
.card {
padding: 1rem;
border: 1px solid #ccc;
max-width: 20rem;
h2 {
margin: 0 0 0.5rem;
}
> p {
color: #444;
}
.tag {
color: darkgreen;
}
&:hover {
border-color: #666;
}
&.featured {
border-color: gold;
}
}
</style>
</head>
<body>
<div class="card">
<h2>Title</h2>
<p>Intro <span class="tag">tagged</span></p>
</div>
<div class="card featured">
<h2>Featured</h2>
<p>Gold border, still hovers darker.</p>
</div>
</body>
</html>The h2, > p, and .tag rules are descendants of the card, so no & is needed. :hover and .featured apply to the card itself, so they need &.
Hover on the featured card won't show the gray border, because &.featured and &:hover have the same specificity and &.featured comes later. That behavior is ordinary cascade order, not a nesting quirk.
Style guidance for this chapter
- Use the short form for descendants, children, and siblings. It reads closer to how you describe the structure.
- Use
&whenever you attach to the parent. A bare:hoveror.activeis almost always a typo. - Write
&before type selectors if you must support Chrome 112 to 119 or Safari 16.5 to 17.1. - Some teams write
&everywhere so every nested selector looks the same and the intent is obvious. That is a valid convention. The code behaves identically.
Exercise
Given .menu { .open { color: red; } }, what selector does the nested rule resolve to? Rewrite it so it matches a .menu element that also has the class open.
Show answer
It resolves to .menu .open, which matches an .open element inside the menu. To match the menu itself when it has the class, write &.open { color: red; } inside .menu, which gives .menu.open.
Exercise
Write nested CSS inside .toolbar that styles button descendants with padding: 0.5rem, direct child span elements with font-weight: 600, and the toolbar itself with a gray background when it sits inside .dark-theme. Which of the three rules needs &?
Show answer
.toolbar {
button { padding: 0.5rem; }
> span { font-weight: 600; }
.dark-theme & { background: gray; }
}
Only the third rule needs &, because the parent must come after .dark-theme. The first two can omit it. button without & needs relaxed syntax support, so write & button for Chrome 112 to 119 and Safari 16.5 to 17.1.