Course contentsShow
← Web Development
Lesson 432 of 2,9827. Responsive DesignFree lesson

Setting Up Container Context

Using container-type to establish a containment context for size-based queries on parent elements.

Setting Up Container Context

You'll learn how to designate a parent element as a container so you can write queries based on its size, not the whole viewport.

The Core Idea

Container queries let child elements respond to their parent's size. But first, you must explicitly tell the browser which parent should serve as the reference point. You do this with the container-type property.

Think of it like setting up a "listening zone." Media queries listen to the viewport. Container queries need a specific parent to listen to — and container-type creates that zone.

The container-type Property

Apply container-type to the element whose size you want to query:

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

This tells the browser: "Watch this container's inline-size (width, in horizontal writing modes). Child elements can now query it."

Common Values

  • inline-size — Most common; tracks the container's width. Use this for horizontal layout responsiveness.
  • size — Tracks both width and height. Use cautiously; can cause layout loops if content affects container height.
  • normal — Default; no containment. The element is not a query container.

Why You Need This

Without container-type, the browser doesn't know which parent to measure. The property establishes containment context — a boundary the browser can reliably track and query against.

Example Setup

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

/* Now children can query .sidebar's width */

Key Takeaway: Use container-type: inline-size on a parent element to make it a query container, enabling its children to respond to its width with container queries.