πŸ”— Understanding β€œ&” in CSS Nested Selectors β€” Complete Tutorial

🌐 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.

>>β€œThe ampersand (&) lets nested CSS inherit, extend, and combine selectors naturally.”

Note

βœ” Works in native CSS Nesting (no SCSS required)
βœ” 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

⚠️ Avoid more than 2–3 levels of nesting β€” becomes hard to maintain.

🎯 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

βœ” Use & mainly for states, modifiers, and combinators
βœ” 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.

>>β€œ& gives CSS nesting its true power β€” readable, modular, modern.”

Learn more β†’& Selector β€” MDN πŸ“˜