Technology News

Early Days of Container Style Queries

We’re still in suuuuuper early days with container queries. Too early for broad browser support, but Chromium already supports it, Safari started supporting it in version 16, and Firefox is presumably not far behind.

Most early days conversations revolving around container queries usually compare the syntax to media queries.

/* Stacked flex container */
.post { display: flex; flex-direction: column;
} /* Change direction when viewport is 600px or wider */
@media(min-width: 600px) { .post { flex-direction: row; }
}
/* Define the container */
.posts { container-name: posts; container-type: inline-size;
} .post { display: flex; flex-direction: column;
} /* Query the container's min-width */
@container posts (min-width: 600px) { /* Change styles when `posts` container is 600px or wider */ .post { flex-direction: row; }
}

Both of these are making queries for min-width: 600. The difference is that the media query is looking at the viewport’s width to trigger those style changes while the container query is looking at the computed width of the .posts element. Sweet!

But after listening to CSS Podcast Episode 59, Una and Adam poked at the future of container queries: style queries! The current working draft of the CSS Containment Module Level 3 spec defines container style queries:

container style query allows querying the computed values of the query container. It is a boolean combination of individual style features (<style-feature>) that each query a single, specific property of the query container.

But no examples on syntax just yet — only a brief description:

The syntax of a <style-feature> is the same as for a declaration, and its query is true if the computed value of the given property on the query container matches the given value (which is also computed with respect to the query container), unknown if the property or its value is invalid or unsupported, and false otherwise. The boolean syntax and logic combining style features into a style query is the same as for CSS feature queries. (See @supports.)

So, yes, given time we should expect to pull off something like this:

.posts { container-name: posts;
} @container posts (background-color: #f8a100) { /* Change styles when `posts` container has an orange background */ .post { color: #fff; }
}

That’s a pretty dumb example. One thing to note is that the container-type is no longer based on the container’s inline-size but by style. We could delcare that like so:

.posts { container-name: posts; container-type: style; /* unnecessary */
}

…but all container queries are style queries by default. Well. at least as it stands today. Miriam Suzanne has a nice outline of the possible issues that might pop up with that.

Where might querying a container’s styles come in handy? I don’t know yet! But my mind gos to a few places:

  • Custom property values: We’ve seen custom properties used like state indicators, such as the DRY-switching method Ana covered a while back. The value changes, and so do styles.
  • Alternate dark mode approach: Instead of basing it all on a body class change that re-assigns custom property values, maybe we can change an entire color palette if, say, the body background changes color.
  • More complex query conditions: Like, say, we want to apply styles when the size and style conditions for a container are met.

Una also mentioned in the CSS Podcast that container style queries could help prevent some awkward styling situations, like if we happen to have italicized text in an already italicized blockquote:

blockquote { container-name: quote;
} @container quote (font-style: italic) { em, i, q, address { font-style: normal; }
}
How to Animate CSS Box Shadows and Optimize Performance
9 Tips on How Not To Miss Your Deadline

Related Articles

9 Best JavaScript and TypeScript ORMs for 2021

Exploring the Potential of the Metaverse: A New World Awaits
This article will briefly explain what object relational mapping (ORM) is, what an ORM library is, and why you should consider using one with your next JavaScript project. We’ll also…

4 Quick Website Design Tips For Law Firms

4-quick-website-design-tips-for-law-firms
As more and more industries and companies enter the online arena, “law firms” are slowly starting to emerge. Every law firm today needs an attractive, informative, and convertible website. Law…

Best Mobile Apps To Manage Trade And Payments For Retail Shops

best-mobile-apps-to-manage-trade-and-payments-for-retail-shops
Mobile-Apps-To-Manage-Trade-And-Payments-For-Retail-Shops Running a business is quite demanding, and that’s where the use of an effective mobile application comes into the picture. Especially for trade and payment scenarios, mobile apps become…

Explore the Top 12 WordPress Themes for 2023

Explore the Top 12 WordPress Themes for 2023
You may have an excellent business plan together with a good product or service to sell. But, the look and feel of your digital storefront doesn’t perfectly represent your brand.…