CSS Responsive & Modern Features

Container Queries

Revolutionize responsive design with container queries that adapt components based on their container size, not the viewport.

What are CSS container queries?

Container queries apply styles based on the size of a parent element instead of the whole viewport. Mark a parent with container-type: inline-size, then use @container (min-width: 400px) { … } to restyle its children. One card component can then lay itself out correctly in a sidebar and in a wide main column.

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

.card { display: grid; gap: 1rem; }
@container (min-width: 400px) {
  .card { grid-template-columns: 120px 1fr; }
}

Interactive Container Queries Demo

Code Editor

<div class="wrap narrow"><div class="card"><img src="https://picsum.photos/id/1062/120/90" alt=""><p>Narrow container: stacked</p></div></div>
<div class="wrap wide"><div class="card"><img src="https://picsum.photos/id/1062/120/90" alt=""><p>Wide container: side by side</p></div></div>

Preview

Container Query Fundamentals

Setting Up Containers

/* Establish containment context */ .container {
  container-type: inline-size; /* Width-based queries */   container-name: main-content; /* Optional identifier */ }

/* Alternative: shorthand property */ .container {
  container: main-content / inline-size;
}

/* Block-size for height-based queries */ .sidebar {
  container-type: block-size;
  container-name: sidebar;
}

/* Both width and height */ .card {
  container-type: size;
  container-name: card;
}

Container context must be established before queries can be applied to child elements.

Basic Query Syntax

/* Width-based container queries */ @container (min-width: 400px) {
  .card {
    display: flex;
    gap: 1rem;
  }
}

/* Named container queries */ @container sidebar (max-width: 300px) {
  .navigation {
    flex-direction: column;
  }
}

/* Height-based queries */ @container (min-height: 500px) {
  .content {
    justify-content: center;
  }
}

/* Complex conditions */ @container (400px ≤ width ≤ 800px) {
  .responsive-grid {
    grid-template-columns: repeat(2, 1fr);
  }
}

Query syntax is similar to media queries but targets container size

Container Types

/* inline-size: Width only */ .horizontal-container {
  container-type: inline-size;
}

/* block-size: Height only */ .vertical-container {
  container-type: block-size;
}

/* size: Both width and height */ .full-container {
  container-type: size;
}

/* normal: No containment (default) */ .no-container {
  container-type: normal;
}

/* Multiple containers */ .nested {
  container: outer / inline-size;
}
.nested .inner {
  container: inner / size;
}

Choose the appropriate container type based on your layout needs

Practical Applications

Adaptive Component Patterns

/* Responsive card component */ .card-container {
  container-type: inline-size;
}

.card {
  padding: 1rem;
  background: white;
  border-radius: 8px;
}

/* Horizontal layout for wider containers */ @container (min-width: 400px) {
  .card {
    display: flex;
    align-items: center;
    gap: 1.5rem;
  }
  
  .card-image {
    width: 150px;
    height: 150px;
    flex-shrink: 0;
  }
  
  .card-content {
    flex: 1;
  }
}

/* Compact layout for narrow containers */ @container (max-width: 300px) {
  .card {
    text-align: center;
  }
  
  .card-title {
    font-size: 1rem;
  }
  
  .card-description {
    display: none;
  }
}

Create components that adapt to their context, not just the viewport.

Navigation Components

/* Responsive navigation */ .nav-container {
  container-type: inline-size;
}

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

/* Horizontal menu for wide containers */ @container (min-width: 600px) {
  .navigation {
    justify-content: space-between;
  }
  
  .nav-item {
    padding: 0.5rem 1rem;
  }
}

/* Compact menu for narrow containers */ @container (max-width: 400px) {
  .navigation {
    flex-direction: column;
  }
  
  .nav-item {
    padding: 0.75rem;
    border-bottom: 1px solid #e5e7eb;
  }
}

Navigation adapts to container width automatically

Form Layouts

/* Responsive form container */ .form-container {
  container-type: inline-size;
}

.form-grid {
  display: grid;
  gap: 1rem;
}

/* Two-column layout for wide forms */ @container (min-width: 500px) {
  .form-grid {
    grid-template-columns: 1fr 1fr;
  }
  
  .form-field.full-width {
    grid-column: 1 / -1;
  }
}

/* Single column for narrow forms */ @container (max-width: 350px) {
  .form-grid {
    gap: 0.75rem;
  }
  
  .form-label {
    font-size: 0.875rem;
  }
}

Forms that adapt to available space intelligently

Advanced Techniques

Container Query Units

/* Container query units */ .container {
  container-type: inline-size;
}

.responsive-element {
  /* cqw: 1% of container's width */   width: 50cqw;
  
  /* cqh: 1% of container's height */   height: 30cqh;
  
  /* cqi: 1% of container's inline size */   padding: 2cqi;
  
  /* cqb: 1% of container's block size */   margin: 1cqb;
  
  /* cqmin: smaller of cqi or cqb */   font-size: clamp(1rem, 4cqmin, 2rem);
  
  /* cqmax: larger of cqi or cqb */   border-radius: min(8px, 2cqmax);
}

/* Fluid typography with container units */ .heading {
  font-size: clamp(1.5rem, 8cqw, 4rem);
  line-height: calc(1em + 2cqw);
}

Container query units provide relative sizing based on container dimensions.

Style Queries (Future)

/* Style-based container queries (experimental) */ .theme-container {
  container-type: inline-size;
  --theme: dark;
}

/* Query based on CSS custom properties */ @container style(--theme: dark) {
  .card {
    background: #1f2937;
    color: white;
  }
}

@container style(--theme: light) {
  .card {
    background: white;
    color: #1f2937;
  }
}

/* Multiple style conditions */ @container style(--layout: grid) and style(--spacing: large) {
  .items {
    display: grid;
    gap: 2rem;
  }
}

Future feature for querying based on style properties

Nested Containers

/* Multiple container levels */ .page-container {
  container: page / inline-size;
}

.sidebar-container {
  container: sidebar / inline-size;
}

.card-container {
  container: card / size;
}

/* Query specific containers */ @container page (min-width: 1200px) {
  .layout {
    display: grid;
    grid-template-columns: 300px 1fr;
  }
}

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

@container card (min-width: 300px) and (min-height: 200px) {
  .card-content {
    display: flex;
    align-items: center;
  }
}

Multiple container contexts can coexist and be queried independently

Best Practices & Browser Support

Best Practices

  • • Use container queries for component-level responsive design
  • • Combine with media queries for page-level layouts
  • • Use meaningful container names for clarity
  • • Test container behavior at various sizes
  • • Consider performance impact of complex queries
  • • Provide fallbacks for older browsers

Browser Support

  • • Chrome 105+ (September 2022)
  • • Firefox 110+ (February 2023)
  • • Safari 16+ (September 2022)
  • • Edge 105+ (September 2022)
  • • Use @supports for feature detection
  • • Progressive enhancement recommended

CSS Container Queries — step by step

1 / 4
1

container-type Declaration

Mark a container with container-type: inline-size — this creates a containment context that children can query with @container.

1 of 4
🔍Container Queries Enable Truly Reusable Components
A card component with container queries can display as a vertical stack in a narrow sidebar and as a horizontal layout in a wide main area — without any changes to the component itself or its CSS class.

🎯 Quick Check

Which property must you set on a parent to enable @container queries on its children?

CSS Container Queries: summary

Key points

  • container-type: inline-size lets you query the container's width. size queries both dimensions but requires an explicit height.
  • Name containers with container-name (or the container shorthand) to target a specific ancestor.
  • Container query units such as cqi (1% of the container's inline size) scale things like font size to the container.
  • Container queries are supported in all modern browsers (since 2023).

Frequently asked questions

›What is the difference between media queries and container queries?

Media queries respond to the viewport. Container queries respond to the size of a specific parent element, which makes components reusable anywhere on the page.

›Can an element query its own size?

No. An element can only query an ancestor container. That's why you wrap the component and put container-type on the wrapper.