Free beginner tutorial

Native CSS Nesting

CSS nesting lets you write related rules inside a parent rule. The browser reads this syntax directly, so native nesting does not need a Sass compiler or build step.

Nest a descendant selector

A nested selector without & is treated as a descendant of the parent selector. This keeps component styles near the rule they belong to.

.card {
  padding: 1rem;
  border: 1px solid #ccd4dd;

  .card-title {
    margin: 0;
    color: #203044;
  }
}

The nested .card-title rule targets a matching descendant, equivalent to the selector .card .card-title.

Use the ampersand selector

The & selector represents the parent selector. Attach it directly to a pseudo-class or class when both parts should belong to the same element.

.button {
  background: #f48024;
  color: white;

  &:hover {
    background: #a74800;
  }

  &.button-secondary {
    background: #203044;
  }
}

Here, &:hover means .button:hover; &.button-secondary means one element that has both classes.

Group related states

You can nest several state rules under a component. Keep the selector structure short so styles remain easy to follow.

.field {
  border: 1px solid #9aa7b5;

  &:focus-within {
    border-color: #a74800;
  }

  &.field-error {
    border-color: #b42318;
  }
}

Nest an at-rule

Rules such as @media can be placed inside a selector block. The nested styles continue to apply to that parent selector.

.navigation {
  display: grid;
  gap: 0.5rem;

  @media (min-width: 48rem) {
    display: flex;
    align-items: center;
  }
}

Keep nesting readable

Deeply nested selectors can make styles hard to override and increase specificity. Prefer a few clear levels, and use the CSS cascade and specificity to understand which matching rule wins.

Native CSS nesting is parsed by the browser. Sass features such as joining class-name fragments (for example, &__title) are not part of native CSS nesting.

Practice by flattening a component

Write a small card style with a descendant title and a hover state, then compare it with the equivalent flat selectors. For component-specific responsive behavior, continue with CSS container queries.