The @container query, that elusive feature developers have been requesting
and proposing for years, has finally made its debut in a browser. Well, sort
of. Here we’ll explain what container queries are, how they work, and what
other features they might come with once fully supported inbrowsers.
In the latest version of Chrome Canary,
the most recent @container query proposal is available for use behind an
experimental flag. Developed by OddBird’s own Miriam
Suzanne, the
draft builds upon the ideas
previously proposed by other web standards experts like David Baron, who wrote
the originaldraft.
@Media queries sparked a responsive design revolution by allowing authors to
change the style of elements based on the size of the entire viewport. Up until
now, what could not be done was changing the style of an element based on the
size of one of its nearestcontainers.
The proposed @container query syntax feels a lot like writing a @media
query, but a main difference is that a @container query has to be implemented
in twoparts.
In this demo (which only works in the latest version of Chrome
Canary), a blockquote is styled
differently depending on the size of itscontainer.
To achieve this, first define a containment context. This lets the browser know
which container to query against later and how exactly to query that specificcontainer.
article,
section {container-type: inline-size;}
The container-type[1] property turns on containment for the
selected elements, allowing for the testing of aspects such as style, size, andstate.
The inline-size value specifies that, in this case, queries will be made
against the containing element’s inline
axis.
Layout and
size[2] containment are applied automatically
aswell.
(Block-size is also being considered as a containment type in the new
proposal. Inline-size seemed to satisfy more use-cases, so is being developedfirst.)
Now that a containment context has been defined, the @container rule is used
to tell the browser when and how styles should change inside eachcontainer.
With a narrow parent container, the blockquote features the image stacked on
top of thetext.
The profile image sits on top of the quote in narrowcontainers.
At the first breakpoint, the layout for the blockquote changes so that the
profile image moves from being above the quote to sitting next to it, and the
text describing the speaker gets a heavier weightapplied.
The profile image changes position at the first breakpoint to sit next to thequote.
/* change styles according to container size */@container(min-width: 35em){.quote {grid-gap: 1rem;grid-template:"media quote" auto/ max-content 1fr;}.media {align-items: center;}.source {font-weight: bold;}}
At the second breakpoint, the size of the text for the quote and the attribution
grows, the background changes color, and the overall shape of the blockquote
changes by way of a clip-path.
At a wider breakpoint, the blockquote gets a clip-path and a different backgroundcolor.
Follow these steps to get started experimenting and making your owndemos:
Download Chrome Canary or update to
the latestversion.
Navigate to chrome://flags.
Search for “CSS container queries” and select Enabled.
Restart the browser.
The OddBird team would love to see what you come up with. Tag us on
Twitter with a link to whatever you create. In
the meantime, check out our collection of demos on
CodePen
forinspiration.
Syntax for establishing a containment context was updated
July 14, 2021 to use the new container-type and container-name
properties instead of contain.↩︎
A previous
version of this article listed style and layout as required values for the
contain property when establishing a containment context. These values are now
automatically applied when using the new container-type and container-name
properties. Updated July 14, 2021. ↩︎
The two values of the shorthand
were originally reversed, with container-type and then container-name.
Updated August 18, 2022.↩︎
Learn how to leverage Web Platform Tests to ensure your polyfills are implementing upcoming browser features correctly, including how to generate a comprehensive report of failing/passing tests on each change.
OddBird sponsored Python Web Conference 2023 and sent me to attend. In this article I showcase my favorite talks and activities from this excellent online event, including a list of useful resources for web application security, introductions to new PaaS providers, and a comparison of the most popular Python web frameworks.