Practical use of modern CSS new features (subgrid/container queries)Practical use of modern CSS new features (subgrid/container queries)

Practical use of modern CSS new features (subgrid/container queries)

Working examples of CSS subgrid and container queries, including progressive enhancement and component-width fallbacks.

Paradigm shift from Viewport standards to free-standing components

Traditional responsive web design relies on the overall width of the screen (viewport). However, in today’s world of component-oriented design, the available width of UI parts differs depending on the parent element (sidebar or main column) in which they are placed.

With the advent of CSS Grid’s “Subgrid” and “Container Queries”, layout flexibility and maintainability have dramatically improved.


Recipe 1: subgrid to synchronize button positions while aligning card heights

Even if the number of characters in the title is uneven in a multi-column card UI, the heights of “images, titles, text, and buttons” in each card can be perfectly aligned in a horizontal line.

.card-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  gap: 1.5rem;
}

.card {
  display: grid;
  grid-template-rows: auto auto 1fr auto;
  grid-row: span 4;
}

@supports (grid-template-rows: subgrid) {
  .card {
    grid-template-rows: subgrid;
  }
}

There is no need for any JavaScript to calculate the height for each card.

Recipe 2: @container whose layout adapts according to its width

The card component automatically switches between horizontal and vertical alignment depending on the width of the container in which it is placed.

.card-wrapper {
  container-type: inline-size;
  container-name: card-container;
}

.card-content {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

@container card-container (min-width: 450px) {
  .card-content {
    flex-direction: row;
  }
}

You can reuse the same components for both the wide area of the home page and the narrow area of the sidebar.


Concept of practical implementation

Subgrid has been widely available across browsers since 2023, but check the browser versions your audience uses. The card example includes an ordinary grid fallback and enhances it with subgrid when supported. The container-query example keeps a vertical flex layout unless its own wrapper reaches the chosen width; it does not depend on viewport width.

References

Last updated on 2026/12/02 00:00 JST