正在加载视频...
视频加载失败
CSS Tip! ✨ You can make your component styles portable with container queries 😎 Change a card layout based on size 🔥 Remove button text when it's small 🤏 .card {container-type: inline-size;} @ container (min-width: 50ch) {.info { grid-column: 2; }} CodePen.IO link below 👇
10 条评论

Here's that @CodePen link! 🚀 Each component is in a resizable container so you can see the queries in action 💪

Hard to really express how magical this CSS feature is in one tweet but you can basically wave goodbye to media queries 👋 The trickiest part is conveying that to design. Instead of designing based on a whole page, the mental model needs to shift to the component level 🤔

Added a video of these in action within a full layout 😍 I hadn't had the opportunity to really use container queries in the wild to fully appreciate them

@CodePen Cool. Few issues in Firefox, but can be overcome /w min- Nice element.

@CodePen Yeah, you're totally right Jett, could definitely set a min-width here 😅 I put these together hastily for a talk at the end of last year and was reminded of them today 🤓

@CodePen Didn't know about this CSS feature until now! Could you explain the difference between `container-type: size` and `container-type: inline-size`, perhaps with an example? Can't seem to trigger any discernable difference.

@CodePen Sure thing Jeff! 🤙 `inline-size` deals with the inline axis which is typically width related. Whereas the `size` query gives you both height and width For example, these two demos use `size`. The beard grows with height and changes color with width 😅

@CodePen Is this presently compatible with most browsers? Looks really solid.

@CodePen Very useful, but this can also be done with media queries

@CodePen Unfortunately not as cleanly 🥲 media queries deal with the size of the viewport and can't account for a components size The same card in different parts of a layout would require different classes and media queries. But shown here, not needed ✨
