It was not 4 years ago that I was complaining about how things were moving slowly for CSS. Grids were the last new things
and it took years to be refined and specified, and even if browsers were rather quick to implement it
Houdini
So that I talked about CSS Houdini and how it supposed to let us break free of that situation. Now, the project did some
things that promised, but not every one of its APIs has been fully specified - let alone implemented in browsers, not
even Chrome. And now the initial push has basically vanished.
flex-wrap: wrapgrid-template-columns: repeat(auto-fi*, minmax(10rem, 1fr))
I don't know you, but I personally have to check the difference between auto-fill and auto-fit *every time*
I need to use one of them.
Not only the element can be given "contradictory" sizes, but also its descendants can alter its sizes
so that circular size recalculations take place. Among the first proposed solution was to limit these
recalculations to 3, but it wasn't ideal anyway.
“1000px!”
“500px!”
So… how can we solve that?
Container Queries
CSS Containment
CSS Container queries
Containing things…
contain:
layout
size
paint
style
Element containment is introduced by the property `contain`, which can 4 different values or a combination of those.
These values state to the CSS engine that one or more aspects of the process of element rendering are contained
inside the element, in the manners we're now going to explore.
Layout containment
The internal layout is isolated: it can't affect the external layout, nor external factors can affect the internal layout.
It's like a new “viewport”.
Ye olde news of the worlde
LOREM ipsum dolor sit amet, consectetur adipisicing elit. Tenetur, ipsam debitis omnis reiciendis, id nisi veritatis maiores distinctio quae voluptate esse aliquam eaque laboriosam unde, ad eos ratione asperiores dolores?
Corporis accusantium, ipsa rerum temporibus voluptates ducimus deserunt accusamus ullam possimus assumenda sunt dolore voluptatibus quod et consequuntur consequatur quae magni? Fugiat, culpa velit. Fugiat dolor ad laudantium aliquam beatae?
Dolorum, repudiandae blanditiis quo architecto id totam?
Here’s no contain
SUNT est amet cum incidunt ab placeat repellendus, provident vel unde expedita numquam eius. Ipsum blanditiis officiis delectus soluta. Labore.
Possimus nostrum amet sint sed aliquid tempore ratione quaerat incidunt saepe, placeat officia quas molestiae dolore non ut expedita vitae corrupti temporibus! Dolor voluptatem harum itaque impedit accusamus et aut?
Possimus veniam tempora cupiditate impedit amet quod error quidem libero, illum atque expedita? Laborum ducimus fugit excepturi itaque eum sapiente possimus vero saepe dicta quo. Quia omnis odio placeat quis!
Fugiat dolor ad laudantium aliquam beatae?
Ye olde news of the worlde
LOREM ipsum dolor sit amet, consectetur adipisicing elit. Tenetur, ipsam debitis omnis reiciendis, id nisi veritatis maiores distinctio quae voluptate esse aliquam eaque laboriosam unde, ad eos ratione asperiores dolores?
Corporis accusantium, ipsa rerum temporibus voluptates ducimus deserunt accusamus ullam possimus assumenda sunt dolore voluptatibus quod et consequuntur consequatur quae magni? Fugiat, culpa velit. Fugiat dolor ad laudantium aliquam beatae?
Dolorum, repudiandae blanditiis quo architecto id totam?
Lo, here’s contain: layout
SUNT est amet cum incidunt ab placeat repellendus, provident vel unde expedita numquam eius. Ipsum blanditiis officiis delectus soluta. Labore.
Possimus nostrum amet sint sed aliquid tempore ratione quaerat incidunt saepe, placeat officia quas molestiae dolore non ut expedita vitae corrupti temporibus! Dolor voluptatem harum itaque impedit accusamus et aut?
Possimus veniam tempora cupiditate impedit amet quod error quidem libero, illum atque expedita? Laborum ducimus fugit excepturi itaque eum sapiente possimus vero saepe dicta quo. Quia omnis odio placeat quis!
Fugiat dolor ad laudantium aliquam beatae?
Size containment
The element's size is not determined by its descendants.
Ye olde news of the worlde
LOREM ipsum dolor sit amet, consectetur adipisicing elit. Tenetur, ipsam debitis omnis reiciendis, id nisi veritatis maiores distinctio quae voluptate esse aliquam eaque laboriosam unde, ad eos ratione asperiores dolores?
Corporis accusantium, ipsa rerum temporibus voluptates ducimus deserunt accusamus ullam possimus assumenda sunt dolore voluptatibus quod et consequuntur consequatur quae magni? Fugiat, culpa velit. Fugiat dolor ad laudantium aliquam beatae?
Dolorum, repudiandae blanditiis quo architecto id totam?
SUNT est amet cum incidunt ab placeat repellendus, provident vel unde expedita numquam eius. Ipsum blanditiis officiis delectus soluta. Labore.
Possimus nostrum amet sint sed aliquid tempore ratione quaerat incidunt saepe, placeat officia quas molestiae dolore non ut expedita vitae corrupti temporibus! Dolor voluptatem harum itaque impedit accusamus et aut?
Possimus veniam tempora cupiditate impedit amet quod error quidem libero, illum atque expedita?
Hark, contain: size!
LOREM ipsum dolor sit amet, consectetur adipisicing elit. Tenetur, ipsam debitis omnis reiciendis, id nisi veritatis maiores distinctio quae voluptate esse aliquam eaque laboriosam unde, ad eos ratione asperiores dolores?
Corporis accusantium, ipsa rerum temporibus voluptates ducimus deserunt accusamus ullam possimus assumenda sunt dolore voluptatibus quod et consequuntur consequatur quae magni? Fugiat, culpa velit. Fugiat dolor ad laudantium aliquam beatae?
Dolorum, repudiandae blanditiis quo architecto id totam?
SUNT est amet cum incidunt ab placeat repellendus, provident vel unde expedita numquam eius. Ipsum blanditiis officiis delectus soluta. Labore.
Possimus nostrum amet sint sed aliquid tempore ratione quaerat incidunt saepe, placeat officia quas molestiae dolore non ut expedita vitae corrupti temporibus! Dolor voluptatem harum itaque impedit accusamus et aut?
Possimus veniam tempora cupiditate impedit amet quod error quidem libero, illum atque expedita?
Paint containment
Nothing is rendered outside the element's boundaries.
Absolutely positioned img
LOREM ipsum dolor sit amet, consectetur adipisicing elit. Tenetur, ipsam debitis omnis reiciendis, id nisi veritatis maiores distinctio quae voluptate esse aliquam eaque laboriosam unde, ad eos ratione asperiores dolores?
Corporis accusantium, ipsa rerum temporibus voluptates ducimus deserunt accusamus ullam possimus assumenda sunt dolore voluptatibus quod et consequuntur consequatur quae magni? Fugiat, culpa velit. Fugiat dolor ad laudantium aliquam beatae?
Dolorum, repudiandae blanditiis quo architecto id totam? Pariatur consequuntur inventore iusto facere perspiciatis, quas minus illum. Consequatur nobis explicabo, iusto iure quia exercitationem commodi quisquam.
And lo, contain: paint
LOREM ipsum dolor sit amet, consectetur adipisicing elit. Tenetur, ipsam debitis omnis reiciendis, id nisi veritatis maiores distinctio quae voluptate esse aliquam eaque laboriosam unde, ad eos ratione asperiores dolores?
Corporis accusantium, ipsa rerum temporibus voluptates ducimus deserunt accusamus ullam possimus assumenda sunt dolore voluptatibus quod et consequuntur consequatur quae magni? Fugiat, culpa velit. Fugiat dolor ad laudantium aliquam beatae?
Dolorum, repudiandae blanditiis quo architecto id totam? Pariatur consequuntur inventore iusto facere perspiciatis, quas minus illum.
Style containment
Not style encapsulation. It's just for counters.
p::before {
content: counter(num);
}
What do we need?
layout
(inline-)size
Container Queries, take 2
Declaring a container
.container {
container-type: inline-size;
}
We're going to use the new property `container-type`, and not `contain` that we've just seen. In the first drafts of
the spec, `contain` was used, but that was changed afterwards as the two specs specialized in performance (containment)
and in capabilities (container queries).
Nonetheless, defining `container-type` automatically turns on containment for `layout` and `size` or `inline-size`,
depending on the value of `container-type`.
Despite all the common things, a person arrived first at the Grand Prix. Another person, second.
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Laborum earum animi excepturi enim rerum unde commodi incidunt, officiis modi sed libero. Fugit, dolore veniam? Qui ut inventore neque corrupti totam.
Ut repellat veniam aspernatur commodi quis! Excepturi commodi sequi reprehenderit nam rerum fugit obcaecati ullam ea facere veniam id labore, iste velit dignissimos soluta perferendis mollitia consequatur dicta, maxime quia.
Voluptate architecto, quasi quidem consequuntur reiciendis delectus doloribus. Reiciendis sunt omnis accusantium temporibus voluptas ex quia quos hic corrupti sint, illum cum doloremque asperiores quae aut saepe laudantium, ducimus repudiandae?
Harum esse sed nemo ex praesentium incidunt, beatae ad quam! Laboriosam iusto, corrupti nulla numquam beatae animi neque magnam impedit odio sed quisquam fuga dolor possimus velit, enim aliquid! Nam.
Debitis blanditiis ut cumque possimus, impedit nulla culpa nobis placeat distinctio sed modi velit obcaecati tempora cum, dolor neque tenetur nam doloremque odio dolorum! Consequuntur molestiae nobis deleniti nesciunt culpa.
.card-container {
container-name: card;
/* No need to declare container-type!
Every element is a style container */
}@container card style(--look: featured) {
.card {
background: salmon;
}
}
We have loose control on what style are applied to the parent, but we want to react to them.
The goal is avoid messing up the overall result. This applies to style queries in general, but custom
properties are good for predefined appearances.
2. Inheritance
Custom properties are inherited, classes are not. Selecting descendants is problematic.
3. Web Components
Custom properties are inherited and thus get through the Shadow DOM's style encapsulation.
We don't have ::theme() yet and forwarding parts is cumbersome, so this can be a way to set
predetermined styles in the root, and let all the custom elements deep in the Shadow DOM chain read it.
4. Readability
Properties have explicit meaning, so it should be clearer what we're querying for.
This makes more sense when style queries support will be complete.
It's clear that CQ, with their ability to set styles based on the containers, are going to be the
Next Big Thing in layouts and styles. It's going to be even clearer when the style queries specification
will be fully defined and implemented in all browsers.
Every widget that we used to design only as full-width components can now be rethought as adaptable
functionalities that can fit every spot of our website.