Skip to article content
FrontendXLesson 30 / 30

Modern CSS: :has(), nesting, subgrid and container units

Build semantic, accessible and responsive interfaces from a blank page, and explain why each layout and styling decision works.

advanced · about 45 minutes · 2 exercises

  • Implement
  • Interview

What you will be able to do

#
  • Use :has() to style a parent based on its children.
  • Apply native CSS nesting and subgrid to reduce duplication.

Overview

#

:has() is a relational selector that matches an element based on what it contains. It solves the long-standing problem of styling a parent when one of its children is in a particular state.

snippet.css
/* label a field when it contains an invalid input */
.field:has(input:invalid) {
  border-color: #d00;
}

/* card with an image gets a two-column layout */
.card:has(img) {
  grid-template-columns: 8rem 1fr;
}

:has() is powerful but not free. A complex :has() on a large tree can be expensive, so scope it tightly. It is now broadly supported in current browsers, but check your target range before relying on it for critical layout.

Native CSS nesting lets you write child selectors inside a parent without a preprocessor. The & refers to the parent selector.

snippet.css
.card {
  padding: 1rem;
  & .title { font-weight: 600; }
  &:hover { transform: translateY(-2px); }
}

Subgrid lets a grid item participate in its parent's tracks, which aligns content across nested components. Without subgrid, a nested grid defines its own tracks and rows do not line up across cards.

snippet.css
.grid { display: grid; grid-template-columns: repeat(3, 1fr); }
.card { display: grid; grid-template-rows: subgrid; grid-row: span 3; }

Container query units are relative to a container rather than the viewport. cqw is one percent of the container's inline size, which makes a component's internal spacing scale with the space it actually receives. Combined with container queries, this makes genuinely self-contained components. As with every modern feature, verify support in your target browsers and provide a sensible fallback for the layout-critical parts.

Exercises

#

Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.

  1. Exercise 1Implement

    Style a .field container with a red border only when it contains an input that is invalid.

    starter.css
    .field { /* base styles */ }

    Reveal the reference answer
    reference.css
    .field { border: 1px solid #a3a3a3; }
    .field:has(input:invalid) {
      border-color: #d00;
    }

    Authored expected output: The field border turns red only when a descendant input is invalid.

    :has() lets the parent respond to a descendant's state without JavaScript.

  2. Exercise 2Interview

    A team wants to use :has() for a critical layout rule. What questions should you ask before adopting it?

    Reveal the reference answer
    reference.text
    Ask which browsers and versions the product supports, whether the rule is layout-critical or progressive enhancement, and how expensive the selector is on the real DOM. If support is incomplete or the rule is critical, provide a fallback or avoid it. Scope :has() tightly to limit performance cost.

    Authored expected output: An answer covering browser support, criticality, fallback and performance.

    This is a realistic trade-off question about adopting a modern feature responsibly.

Common misconceptions

#
  • CSS nesting requires a preprocessor.
  • Subgrid lets a child define its own independent rows that still align with the parent.

Related resources

#

Your progress

#

Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.

Verification: authored. Answers and outputs on this page are authored reference material, not executed results.

© 2026 FrontendX. All rights reserved.