π What Is the & (Ampersand) in CSS Nesting?
The & symbol is used inside CSS Nesting to reference theparent selector. It allows you to build more complex selectors while keeping your code clean, structured, and easy to read β similar to SCSS.
Note
β Represents the parent selector exactly
β Helps create states, modifiers, pseudo-classes, and combinators
π Basic Usage of &
Inside a nested rule, & stands for the entire parent selector.
Basic & usage
.button {
color: white;
&:hover {
background: black;
}
}Equivalent to:
Equivalent output
.button:hover {
background: black;
}π¦ Why Do We Need &?
- To create interactive states (hover, focus, active)
- To build BEM-like structures
- To generate modifiers or variations
- To use combinators (& + selector, & > selector, & ~ selector)
- To concatenate parent selectors (&-child, &--modifier)
π― 1. State Selectors with &
State selectors
.input {
border: 1px solid #aaa;
&:focus {
border-color: blue;
}
&:disabled {
opacity: 0.5;
}
}π¨ 2. Pseudo-elements with &
Pseudo-element nesting
.title {
font-size: 2rem;
&::after {
content: "";
display: block;
height: 2px;
background: black;
}
}π 3. BEM Naming with &
BEM becomes cleaner using &.
BEM example
.card {
padding: 1rem;
&__title {
font-size: 1.4rem;
}
&__button {
padding: 0.5rem 1rem;
}
&--highlight {
background: yellow;
}
}π 4. Using & with Combinators
& can appear on either side of combinators.
Combinators with &
.item {
color: black;
& > span {
color: blue; /* .item > span */
}
& + .item {
margin-top: 1rem; /* .item + .item */
}
& ~ .info {
opacity: 0.6; /* .item ~ .info */
}
}π 5. Nested Descendants Without &
If you donβt use &, the nested selector becomes a descendant selector.
Descendant nesting
.profile {
h2 {
margin-bottom: 0.5rem; /* .profile h2 */
}
}π§ 6. Selector Concatenation with &
& can be used to append or prepend text to the parent selector.
Concatenation
.btn {
padding: 10px;
&-primary {
background: blue;
color: white;
}
&-outline {
border: 2px solid blue;
}
}Produces:
Output
.btn-primary { β¦ }
.btn-outline { β¦ }π§ͺ 7. Nesting Multiple Levels
Deep Nesting (use carefully)
.menu {
background: #111;
ul {
padding: 0;
li {
color: white;
&:hover {
color: orange;
}
}
}
}Note
π― 8. Using & inside Media Queries
Media Query + &
.box {
width: 300px;
@media (max-width: 600px) {
& {
width: 100%;
}
}
}π‘ 9. Using & inside @container
Container Query + &
.card {
container-type: inline-size;
@container (min-width: 500px) {
& {
padding: 2rem;
}
}
}β οΈ Gotchas & Rules
- β & must refer to a selector β NOT inside property declarations
- β Without &, selectors become descendants
- β & always stands for the full parent selector
- β Donβt nest too deeply β avoid SCSS-style selector bloat
Note
β Keep nesting shallow (1β2 levels) for clean CSS
π₯ Summary
The & nested selector is one of the most powerful features of modern CSS nesting. It lets you build clean component styles, manage state styling, create BEM patterns, and write complex selectors without repetition.
Learn more β& Selector β MDN π