π What Are Container Query Units?
Container Query Units allow elements to size themselves based on thedimensions of their nearest container β not the viewport. These units are essential for component-level responsive design.
Note
β Perfect for responsive cards, grids, navbars, widgets
π¦ Overview of Container Units
| Unit | Meaning | Relative To |
|---|---|---|
| cqw | Container Query Width | 1% of container width |
| cqh | Container Query Height | 1% of container height |
| cqi | Container Inline Size | 1% of container inline dimension |
| cqb | Container Block Size | 1% of container block dimension |
| cqmin | Smaller of inline/block size | min(cqi, cqb) |
| cqmax | Larger of inline/block size | max(cqi, cqb) |
Note
β cqmin/cqmax behave like vmin/vmax but for containers
π Make a Container First
Enable Container Queries
.card {
container-type: inline-size; /* or size */
}Once a container is enabled, child elements can use container query units.
π’ 1. cqw β Container Width
cqw Example
.title {
font-size: 5cqw; /* scales with container width */
}Perfect for responsive card titles that grow/shrink with their card container.
π 2. cqh β Container Height
cqh Example
.box {
height: 50cqh; /* 50% of container height */
}Note
π€ 3. cqi & cqb β Writing-Mode Aware Units
cqi = inline direction
cqb = block direction
These change depending on writing-mode.
cqi/cqb Example
.paragraph {
padding-inline: 4cqi;
padding-block: 2cqb;
}Important for internationalization (vertical writing modes, RTL languages).
πΊ 4. cqmin & cqmax
Similar to vmin/vmax, but based on container size.
cqmin/cqmax Example
.avatar {
width: 20cqmin;
height: 20cqmin; /* perfect responsive square */
}π§ How Container Units Compare
| Unit | Container-Based? | Writing-Mode Aware? |
|---|---|---|
| cqw | β Yes | β No |
| cqh | β Yes | β No |
| cqi | β Yes | β Yes |
| cqb | β Yes | β Yes |
| cqmin | β Yes | β Yes |
| cqmax | β Yes | β Yes |
π§ͺ Practical Examples
1οΈβ£ Responsive Card Layout
Responsive Card
.card {
container-type: inline-size;
}
.card-title {
font-size: 4cqw;
}
.card-content {
padding: 3cqi; /* inline-aware padding */
}2οΈβ£ Adaptive Avatar Scaling
Avatar example
.profile {
container-type: size;
}
.avatar {
width: 30cqmin;
height: 30cqmin;
border-radius: 50%;
}Avatar stays perfectly proportional regardless of container shape.
3οΈβ£ Dashboard Tile Resizing
Dashboard widget
.tile {
container-type: size;
}
.tile-content {
font-size: 2cqmax; /* grows with largest container dimension */
}π Real Use Cases
- Responsive cards that scale depending on grid column width
- Dashboard components that resize inside tiles
- Reusable UI components that adapt in any parent container
- Fully fluid typographic systems inside components
- Widgets with proportional aspect ratios
β οΈ Common Mistakes & Gotchas
- β Forgetting to set container-type
- β Expecting container units to work without a container
- β Using cqh when the container height is auto β unstable results
- β Mixing container units with vh/vw inconsistently
Note
π₯ Summary
Container units (cqw, cqh, cqi, cqb, cqmin, cqmax) makecomponent-level responsive design possible without relying on global media queries. They adapt UI elements based on the size of their nearest container β making components portable, flexible, and truly reusable.
Learn more βMDN Docs π