Defining containers to measure in context
By default, 1cqi (1/100 container query inline size) is the same as 1svi
(1/100 small viewport inline size) because the “small”
viewport
acts as the initial container for any web page. In order to take full advantage
of the cqi unit, you need to define additional “containers” within the page.
The primary layout containers on this page are the product-list and
shopping-cart – so they are set to expose their inline-size.
product-list,
shopping-cart {
container-type: inline-size;
}
Container units are powerful, but sometimes it’s useful to make more dramatic
changes in a component layout when the available size crosses a threshold. These
are often called breakpoints – since the fix is applied at the point when a
given layout begins to break. You may already be familiar with using @media to
add breakpoints based on the viewport size. The new @container rule works the
same way, but measuring container elements instead of the page!
Article contents
- Defining containers to measure in context
- Explicit container queries
- Transition grid templates and visibility
- Querying media versus containers
The demo where we put it all together
See the Pen Baseline Bakery by @web-dot-dev on CodePen.