← Back to Blog
Tutorials·Jan 30, 2026·8 min read

10 Modern CSS Layout Techniques Every Frontend Developer Must Know in 2026

Container queries, :has(), clamp() for responsive typography, and 7 more modern CSS patterns with real-world use cases, pros/cons, and code examples.

CSSWeb DevelopmentFrontendLayoutResponsive Design
JV

Jose Viscasillas

January 30, 2026 · 8 min read

10 Modern CSS Layout Techniques Every Frontend Developer Must Know in 2026

CSS layout has evolved beyond media queries and flexbox.

Here are 10 modern techniques with real-world examples, browser support, and when to use each.

1. Container Queries: Component-Level Responsiveness

Media queries respond to viewport size. Container queries respond to parent container size.

The Problem Media Queries Can't Solve

css
/* Media query: responds to viewport */
@media (max-width: 768px) {
  .card {
    flex-direction: column;
  }
}

Issue: What if the card is in a sidebar at 300px wide, even on a 1920px screen?

Container Queries to the Rescue

css
.sidebar {
  container-type: inline-size;
  container-name: sidebar;
}

.card {
  display: flex;
  gap: 1rem;
}

@container sidebar (max-width: 400px) {
  .card {
    flex-direction: column;
  }
}

Result: Card responds to its container, not the viewport.

Real-World Example: Product Card

css
.product-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 1rem;
  container-type: inline-size;
}

.product-card {
  display: grid;
  grid-template-columns: 1fr 2fr;
  gap: 1rem;
}

@container (max-width: 350px) {
  .product-card {
    grid-template-columns: 1fr;
  }

  .product-card img {
    width: 100%;
  }
}

Browser Support: Chrome 105+, Safari 16+, Firefox 110+

2. The :has() Selector: Parent Selection

CSS finally has a parent selector.

Select Parent Based on Child

css
/* Style card differently if it has an image */
.card:has(img) {
  display: grid;
  grid-template-columns: 200px 1fr;
}

/* Style card without image */
.card:not(:has(img)) {
  padding: 2rem;
  text-align: center;
}

Form Validation Styling

css
/* Style label when input is invalid */
label:has(+ input:invalid) {
  color: red;
}

/* Style form when any field is invalid */
form:has(input:invalid) .submit-button {
  opacity: 0.5;
  pointer-events: none;
}

Interactive States

css
/* Change header when any navigation item is hovered */
header:has(.nav-item:hover) {
  background: rgba(0, 0, 0, 0.95);
}

/* Different layout when sidebar is expanded */
.layout:has(.sidebar.expanded) {
  grid-template-columns: 300px 1fr;
}

.layout:has(.sidebar:not(.expanded)) {
  grid-template-columns: 60px 1fr;
}

Browser Support: Chrome 105+, Safari 15.4+, Firefox 121+

3. clamp() for Fluid Typography

No more breakpoints for font sizes.

The Old Way

css
h1 {
  font-size: 2rem;
}

@media (min-width: 768px) {
  h1 {
    font-size: 2.5rem;
  }
}

@media (min-width: 1024px) {
  h1 {
    font-size: 3rem;
  }
}

The Modern Way

css
h1 {
  font-size: clamp(2rem, 5vw, 3rem);
  /* min: 2rem, preferred: 5vw, max: 3rem */
}

How It Works:

  • At narrow viewports: 2rem (minimum)
  • Scales with viewport: 5vw
  • At wide viewports: 3rem (maximum)

Complete Fluid Type System

css
:root {
  --font-size-sm: clamp(0.875rem, 0.8rem + 0.25vw, 1rem);
  --font-size-base: clamp(1rem, 0.9rem + 0.5vw, 1.125rem);
  --font-size-lg: clamp(1.25rem, 1.1rem + 0.75vw, 1.5rem);
  --font-size-xl: clamp(1.5rem, 1.3rem + 1vw, 2rem);
  --font-size-2xl: clamp(2rem, 1.5rem + 2vw, 3rem);
}

body { font-size: var(--font-size-base); }
h1 { font-size: var(--font-size-2xl); }
h2 { font-size: var(--font-size-xl); }
small { font-size: var(--font-size-sm); }

Browser Support: All modern browsers

4. Subgrid: Nested Grid Alignment

Align nested grids with parent grid tracks.

The Problem

css
.grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
}

.card {
  display: grid;
  grid-template-rows: auto 1fr auto;
  /* Not aligned with parent grid */
}

Subgrid Solution

css
.grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 1rem;
}

.card {
  display: grid;
  grid-template-rows: subgrid;
  grid-row: span 3;
}

Result: Card rows align with parent grid.

Real-World: Product Grid

css
.products {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  grid-template-rows: repeat(3, auto); /* Image, title, button */
  gap: 1rem;
}

.product {
  display: grid;
  grid-template-rows: subgrid;
  grid-row: span 3;
}

.product-image { grid-row: 1; }
.product-title { grid-row: 2; }
.product-button { grid-row: 3; align-self: end; }

All buttons align at the bottom, regardless of title length.

Browser Support: Safari 16+, Firefox 71+, Chrome: Coming soon

5. aspect-ratio: Fixed Proportions

No more padding-bottom hacks.

The Old Hack

css
.video-wrapper {
  position: relative;
  padding-bottom: 56.25%; /* 16:9 aspect ratio */
  height: 0;
}

.video-wrapper iframe {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}

The Modern Way

css
.video-wrapper {
  aspect-ratio: 16 / 9;
}

.video-wrapper iframe {
  width: 100%;
  height: 100%;
}

Common Use Cases

css
/* Square thumbnails */
.thumbnail {
  aspect-ratio: 1;
  object-fit: cover;
}

/* Widescreen images */
.hero-image {
  aspect-ratio: 21 / 9;
  width: 100%;
}

/* Profile avatars */
.avatar {
  aspect-ratio: 1;
  border-radius: 50%;
}

Browser Support: All modern browsers

6. gap with Flexbox: Finally

gap works with flexbox now, not just grid.

Before

css
.flex-container {
  display: flex;
}

.flex-container > * {
  margin-right: 1rem;
}

.flex-container > *:last-child {
  margin-right: 0; /* Remove trailing margin */
}

Now

css
.flex-container {
  display: flex;
  gap: 1rem;
}

That's it.

Complex Layouts

css
.toolbar {
  display: flex;
  justify-content: space-between;
  gap: 0.5rem;
}

.toolbar-left {
  display: flex;
  gap: 0.25rem;
}

.toolbar-right {
  display: flex;
  gap: 0.75rem;
}

Browser Support: All modern browsers

7. Grid Auto-Fit vs Auto-Fill

Create responsive grids without media queries.

auto-fill: Always Fill Space

css
.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 1rem;
}

Behavior: Creates as many columns as fit, leaves empty columns if needed.

auto-fit: Collapse Empty Columns

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

Behavior: Collapses empty columns, stretches items to fill space.

When to Use Each

Use auto-fill when:

  • You want consistent item sizes
  • Empty space is acceptable

Use auto-fit when:

  • You want items to stretch
  • You have a small number of items

Browser Support: All modern browsers

8. object-fit: Image Cropping

Control how images fill containers.

The Problem

html
<div class="card">
  <img src="wide-image.jpg" alt="Product">
</div>
css
.card {
  width: 300px;
  height: 300px;
}

.card img {
  width: 100%;
  height: 100%;
  /* Image gets distorted */
}

Solutions

css
/* Cover: Crop to fill (like background-size: cover) */
.card img {
  width: 100%;
  height: 100%;
  object-fit: cover; /* Crops image */
  object-position: center; /* Crop from center */
}

/* Contain: Fit within bounds (like background-size: contain) */
.card img {
  object-fit: contain; /* Letterboxes if needed */
}

/* Scale-down: Use original size or contain, whichever is smaller */
.logo {
  object-fit: scale-down;
  max-width: 200px;
  max-height: 100px;
}

Browser Support: All modern browsers

9. CSS Grid Named Areas

Semantic grid layouts.

Traditional Grid

css
.layout {
  display: grid;
  grid-template-columns: 200px 1fr 200px;
  grid-template-rows: auto 1fr auto;
}

.header { grid-column: 1 / -1; grid-row: 1; }
.sidebar { grid-column: 1; grid-row: 2; }
.main { grid-column: 2; grid-row: 2; }
.aside { grid-column: 3; grid-row: 2; }
.footer { grid-column: 1 / -1; grid-row: 3; }

Named Areas

css
.layout {
  display: grid;
  grid-template-columns: 200px 1fr 200px;
  grid-template-rows: auto 1fr auto;
  grid-template-areas:
    "header header header"
    "sidebar main aside"
    "footer footer footer";
  gap: 1rem;
}

.header { grid-area: header; }
.sidebar { grid-area: sidebar; }
.main { grid-area: main; }
.aside { grid-area: aside; }
.footer { grid-area: footer; }

Responsive Layout Changes

css
@media (max-width: 768px) {
  .layout {
    grid-template-columns: 1fr;
    grid-template-areas:
      "header"
      "main"
      "aside"
      "sidebar"
      "footer";
  }
}

Just redefine the areas—no need to change individual element styles.

Browser Support: All modern browsers

10. Logical Properties: Internationalization

Write direction-agnostic CSS.

The Problem

css
.button {
  margin-left: 1rem; /* Breaks in RTL languages (Arabic, Hebrew) */
}

Logical Properties

css
.button {
  margin-inline-start: 1rem; /* LTR: left, RTL: right */
}

Complete Mapping

css
/* Physical → Logical */
margin-top → margin-block-start
margin-right → margin-inline-end
margin-bottom → margin-block-end
margin-left → margin-inline-start

padding-left → padding-inline-start
padding-right → padding-inline-end

border-left → border-inline-start
border-right → border-inline-end

width → inline-size
height → block-size

text-align: left → text-align: start
text-align: right → text-align: end

Example

css
.card {
  padding-block: 1rem; /* Top and bottom */
  padding-inline: 2rem; /* Left and right (or right and left in RTL) */
  border-inline-start: 3px solid blue; /* Left border in LTR */
}

Browser Support: All modern browsers

Bonus: Combining Techniques

Here's a real-world component using multiple techniques:

css
.product-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: clamp(1rem, 2vw, 2rem);
  container-type: inline-size;
}

.product-card {
  display: grid;
  grid-template-rows: auto auto 1fr auto;
  gap: 0.5rem;
  padding-block: 1rem;
  padding-inline: 1rem;
  aspect-ratio: 3 / 4;
}

.product-image {
  aspect-ratio: 1;
  object-fit: cover;
}

.product-price {
  font-size: clamp(1rem, 3cqi, 1.5rem); /* Container query unit */
}

@container (max-width: 300px) {
  .product-card {
    grid-template-rows: auto 1fr auto;
  }

  .product-description {
    display: none;
  }
}

.product-card:has(.sale-badge) {
  border: 2px solid red;
}

Features used:

  • CSS Grid with auto-fit
  • clamp() for responsive spacing
  • Container queries
  • Logical properties
  • aspect-ratio
  • object-fit
  • :has() selector

All without a single media query.

Conclusion

Modern CSS is incredibly powerful:

  1. Container queries for component-level responsiveness
  2. :has() for parent selection
  3. clamp() for fluid typography
  4. Subgrid for nested alignment
  5. aspect-ratio for proportions
  6. gap with flexbox
  7. Grid auto-fit/auto-fill
  8. object-fit for images
  9. Grid named areas
  10. Logical properties for i18n

Stop reaching for JavaScript. CSS can handle it.

---

Resources:

JV

Written by Jose Viscasillas

Senior Software Engineer building video platforms at ON24. 21 years of coding experience. I write about React, TypeScript, AI, and developer tools.

Recommended Reads

📬

Subscribe to the Newsletter

New articles delivered to your inbox. No spam, unsubscribe anytime.

Join 500+ developers getting weekly insights on React, TypeScript, and building products.