Container Query Units (cqw, cqh)
- CSS Basics
- Selectors & Specificity
- Box Model & Units
- Typography & Text Styling
- Colors & Color Spaces
- Positioning & Stacking Context
- Flexbox
- Grid
- Grid Advanced
- Responsive Design
- Container Queries
- Pseudo-classes & Pseudo-elements
- Modern Selectors
- CSS Custom Properties
- CSS Functions
- Transitions & Animations
- Transforms
- Forms & Input Styling
- CSS Architecture & Methodologies
- Accessibility in CSS
- Performance & Optimization
- View Transitions & Modern APIs
- Cascade Layers & Scope
What Container Query Units Are For
Just as vw and vh size an element relative to the viewport, container query units size an element relative to its nearest queried container instead - useful once a component is meant to scale with the space it is actually given, not the whole browser window.
The Main Container Units
| Unit | Relative to |
|---|---|
cqw | 1% of the container's width |
cqh | 1% of the container's height |
cqi | 1% of the container's inline size (usually width, in horizontal writing modes) |
cqb | 1% of the container's block size (usually height) |
These Only Work Inside a Declared Container
.card-wrapper {
container-type: inline-size;
}
.card-title {
font-size: clamp(1rem, 5cqw, 2rem);
}Here, the card's title font size scales with the width of its own container - card-wrapper - rather than the width of the browser window, so the same card component scales appropriately whether it sits in a wide column or a narrow sidebar.
Pairing Container Units with clamp()
Using a container unit as the flexible middle value inside clamp() (as shown above) is a common pattern: it keeps a sensible minimum and maximum size while letting the value scale smoothly with the container in between.
Why This Matters for Reusable Components
Container units complete the picture started by container queries: not only can layout react to a container's size, but typography and spacing values can scale continuously with it too, without any breakpoint jumps.