Comments on: Early Days of Container Style Queries https://css-tricks.com/early-days-of-container-style-queries/ Tips, Tricks, and Techniques on using Cascading Style Sheets. Mon, 07 Nov 2022 23:05:07 +0000 hourly 1 https://wordpress.org/?v=6.1.1 By: Miriam Suzanne https://css-tricks.com/early-days-of-container-style-queries/#comment-1797541 Tue, 25 Oct 2022 18:24:08 +0000 https://css-tricks.com/?p=374330#comment-1797541 In reply to Shaun Kardinal.

The replies here capture some of the reasons. Like Geoff suggests, we need to establish containers explicitly – so there’s no avoiding that step, even if we used selectors in the @container syntax.

But also, names are optional, reusable across different elements, and one container can have multiple names. This adds a lot of flexibility.

You don’t have to give containers a name, and you don’t have to mention a name in the query syntax. When no name is added to the condition, the nearest valid ancestor container is queried. If you just want to know the most directly relevant context, leave off the name.
You can apply the same name to multiple selectors to create a ‘class’ of container. So you could add the layout container name to the main area, the sidebar, posts, grid items, etc. Then you can query the nearest layout container.
You can apply multiple names to a single container, to make it part of multiple patterns. So we could say the main area is a container that is part of our layout pattern, but also give it the name main, and the name color-context because it applies a unique color scheme. Now we can write some queries specific to the main area name, some looking for the nearest layout, and some looking at the nearest color-context – and the main area is set up to handle all those queries.

]]>
By: Jeremy https://css-tricks.com/early-days-of-container-style-queries/#comment-1797408 Thu, 13 Oct 2022 15:47:21 +0000 https://css-tricks.com/?p=374330#comment-1797408 In reply to Shaun Kardinal.

My guess is .post and container-name: posts; happen to use the same name in the example but in real world cases this doesn’t have to be the case and the same container-name will be used in multiple class selectors.

]]>
By: Maciej Jur https://css-tricks.com/early-days-of-container-style-queries/#comment-1797403 Thu, 13 Oct 2022 11:09:11 +0000 https://css-tricks.com/?p=374330#comment-1797403 In reply to Shaun Kardinal.

Selector “.posts” could theoretically target multiple HTML nodes, each having the attribute class set to posts. In that case it wouldn’t be obvious to determine which container’s width should be checked in the query.

I can imagine that the CSS property “container-name” serves to make sure that there can only be a single container with a given name.

]]>
By: Geoff Graham https://css-tricks.com/early-days-of-container-style-queries/#comment-1797391 Wed, 12 Oct 2022 14:27:06 +0000 https://css-tricks.com/?p=374330#comment-1797391 In reply to Shaun Kardinal.

That would be super cool and much more efficient. But I also get that there are things that CSS has to know up front, and one of those things is being able to match a container declaration to an element that’s explcitly defined as a container — not to mention what kind of query we want, whether it’s the size or style of that container. We have to tell CSS that so the browser knows how to match things up.

]]>
By: Shaun Kardinal https://css-tricks.com/early-days-of-container-style-queries/#comment-1797390 Wed, 12 Oct 2022 14:21:05 +0000 https://css-tricks.com/?p=374330#comment-1797390 seems unnecessarily convoluted. why not:

@container .posts (min-width: 600px) {
// applies to .posts
}

not snark, really trying to find the logic…

]]>