CSS Reference ยท Container Queries and Logical Properties
The container-type property
Reference for the CSS container-type property: make an element a size query container with inline-size or size, and understand the containment it applies.
The container-type property makes an element a query container. Once an element has inline-size or size, its descendants can use @container rules that respond to the container's dimensions instead of the viewport's.
To make this possible, the browser has to be sure that the container's size does not depend on the things you are styling inside it. That is why the property also switches on a form of CSS containment, and why a few things behave differently on a container.
.panel { container-type: inline-size; } @container (min-width: 30rem) { .panel .title { font-size: 1.5rem; } }
Try it
Syntax
container-type: normal | size | inline-size
- Initial value:
normal - Applies to: all elements
- Inherited: no
- Animatable: no
- Computed value: as specified
Values
| Value | Description |
|---|---|
normal |
The element is not a size container. It can still be a container for style queries, but @container size features (width, height, inline-size and so on) do not match against it. |
inline-size |
A size container in the inline axis. Queries can test the inline size (the width in horizontal writing). The container's inline size no longer depends on its content, but its height still does. Use this for almost every component. |
size |
A size container in both axes. Queries can test width and height, but neither dimension may depend on content, so you must give the element a height by some other means, or it collapses. |
Newer container types, such as scroll-state for scroll-state queries, are not widely supported yet; check a compatibility table.
Usage notes
- Set the container on a parent, not on the element you style. A container cannot query itself, only be queried by its descendants. Add a wrapper if you need to.
- It applies containment.
inline-sizeandsizeapply layout, style and inline-size (or size) containment to the element. In practice this means the container becomes a containing block for absolutely and fixed-positioned descendants, creates a stacking context, and scopes counters and quotes. - A container whose size comes from its content collapses. For
inline-size, a container that is shrink-to-fit (a float, a flex item withautowidth, an inline-block) can end up with zero width because the browser cannot look at the children to size it. Give it a width, or make it a block that fills its parent, or a flex or grid item that stretches. - Prefer
inline-sizeoversize. Height queries need explicit heights, which is rarely what a flowing layout wants. - Do not turn everything into a container. Each container adds layout work. Place them on layout components such as cards, sidebars and widgets, not on every element.
- Style queries. An element with
container-type: normalcan still be queried with@container style(--theme: dark)for custom properties in browsers that support style queries. Support differs between browsers, so check a compatibility table.
Browser support
inline-size and size container queries are supported in all current major browsers and are part of Baseline; check a compatibility table for older releases.
Related
containerโ shorthand for name and typecontainer-nameโ names a container@containerโ the rule that queries containersinline-sizeโ the logical width property- container query units โ
cqw,cqh,cqi,cqb - Lesson: Container Queries