Skip to main content
1:1 mock interviews with Sumit, scored on a real hiring rubric.Book a slot →

Modern CSS You Should Use Today: Container Queries, :has() and Nesting

Learn the modern CSS features now supported in all major browsers: container queries for truly reusable components, the :has() parent selector, native nesting, and clamp() for fluid type, with practical examples.

Sumit KumarUpdated Oct 8, 20263 min readCSS
Modern CSS You Should Use Today: Container Queries, :has() and Nesting

CSS has changed more in the last few years than in the decade before. Features that used to need JavaScript, Sass or awkward workarounds now work natively in every major browser. These four are worth adopting right away: container queries, :has(), nesting and clamp().

1. Container queries: components that adapt to their space#

Media queries respond to the viewport. But a card can sit in a wide main column on one page and a narrow sidebar on another, on the same screen. What the card really needs to know is how wide its container is.

Container query: the same card component stacks vertically in a narrow sidebar and switches to a horizontal layout in the wide main column
CSS
/* 1. Mark the parent as a container */
.card-wrapper {
  container-type: inline-size;
}

/* 2. Default: stacked layout */
.card {
  display: grid;
  gap: 1rem;
}

/* 3. When the container is wide enough, go side by side */
@container (min-width: 480px) {
  .card {
    grid-template-columns: 200px 1fr;
  }
}

Now the same card component looks right wherever you drop it, with no page-specific overrides. You can also name containers with container-name and query a specific one.

Tip

Use media queries for page layout (how many columns the page has) and container queries for components (how a card arranges itself inside its column).

2. :has(): the parent selector we waited years for#

:has() selects an element based on what it contains or what follows it. CSS couldn't style a parent based on its children before.

CSS
/* A card that contains an image gets a different layout */
.card:has(img) {
  padding-top: 0;
}

/* Highlight a form field's wrapper when its input is invalid */
.field:has(input:invalid:not(:placeholder-shown)) {
  border-color: crimson;
}

/* Style a label when its checkbox is checked */
label:has(input[type="checkbox"]:checked) {
  font-weight: 600;
}

/* Stop the page scrolling while a modal is open */
body:has(dialog[open]) {
  overflow: hidden;
}
The :has() selector: when the input inside a field wrapper is invalid, the whole wrapper turns red without any JavaScript

Each of these used to need JavaScript to add and remove a class. Now it is one CSS rule, and it always stays in sync with the page.

3. Native nesting: no preprocessor needed#

Like Sass, you can now write related rules inside each other:

CSS
.nav {
  display: flex;
  gap: 1rem;

  a {
    color: inherit;
    text-decoration: none;

    &:hover {
      text-decoration: underline;
    }
  }

  @media (max-width: 600px) {
    flex-direction: column;
  }
}

& refers to the parent selector, just as in Sass. Media queries can be nested right next to the styles they change, which keeps a component's styles in one place.

Note

Keep nesting shallow, ideally no more than two or three levels. Deep nesting creates long, highly specific selectors that are hard to override, in native CSS just as in Sass.

4. clamp(): fluid sizes without media queries#

clamp(min, preferred, max) picks a value that grows with the screen but never goes below the minimum or above the maximum:

CSS
h1 {
  /* at least 2rem, at most 3.5rem, scales in between */
  font-size: clamp(2rem, 1.2rem + 3vw, 3.5rem);
}

.container {
  width: min(100% - 2rem, 1100px);
  margin-inline: auto;
}

.section {
  padding-block: clamp(2rem, 5vw, 5rem);
}
clamp(2rem, 1.2rem + 3vw, 3.5rem): the font size stays at 32px on small screens, grows with the viewport, and stops at 56px on large screens

One line replaces a stack of breakpoint overrides, and the size changes smoothly instead of jumping at each breakpoint. Mixing rem into the middle value keeps the text responding to the user's browser zoom and font settings.

Browser support#

FeatureStatus
Container size queriesAll major browsers
:has()All major browsers
Native nestingAll major browsers
clamp(), min(), max()All major browsers

All four have been supported across Chrome, Edge, Firefox and Safari since 2023, so they are safe for most audiences. If you must support very old browsers, check caniuse.com and provide a simple fallback layout first.

Putting it together#

CSS
.product-list {
  container-type: inline-size;
}

.product {
  display: grid;
  gap: 0.75rem;
  padding: clamp(0.75rem, 2cqi, 1.25rem);

  &:has(.badge-sale) {
    outline: 2px solid gold;
  }

  @container (min-width: 520px) {
    grid-template-columns: 160px 1fr;
  }
}

cqi is a container query unit: 1% of the container's inline size, so the padding scales with the container rather than the whole screen.

Key takeaways#

  • Container queries let components respond to their own space, not the viewport.
  • :has() styles elements based on their children or state, replacing many small JavaScript class toggles.
  • Native nesting keeps component styles together without Sass; keep it shallow.
  • clamp() gives fluid type and spacing in one line.