Ads
Sunday, June 30, 2024
Thunderstorms/Wind Early today!
With a high of F and a low of 65F. Currently, it's 70F and Partly Cloudy outside.
Current wind speeds: 19 from the Southeast
Pollen: 0
Sunrise: June 30, 2024 at 05:28PM
Sunset: July 1, 2024 at 08:21AM
UV index: 0
Humidity: 77%
via https://ift.tt/5naEF4s
July 1, 2024 at 10:03AM
Saturday, June 29, 2024
Showers Late today!
With a high of F and a low of 58F. Currently, it's 69F and Fair outside.
Current wind speeds: 9 from the Southeast
Pollen: 0
Sunrise: June 29, 2024 at 05:28PM
Sunset: June 30, 2024 at 08:21AM
UV index: 0
Humidity: 50%
via https://ift.tt/jKhE3ap
June 30, 2024 at 10:03AM
Friday, June 28, 2024
Partly Cloudy today!
With a high of F and a low of 59F. Currently, it's 71F and Partly Cloudy outside.
Current wind speeds: 11 from the North
Pollen: 0
Sunrise: June 28, 2024 at 05:28PM
Sunset: June 29, 2024 at 08:21AM
UV index: 0
Humidity: 48%
via https://ift.tt/f057vEF
June 29, 2024 at 10:03AM
Transitioning to Auto Height
I know this is something Chris has wanted forever, so it’s no surprise he’s already got a fantastic write-up just a day after the news broke. In fact, I first learned about it from his post and was unable to dredge up any sort of announcement. So, I thought I’d jot some notes down because it feels like a significant development.
The news: transitioning to auto
is now a thing! Well, it’s going to be a thing. Chrome Canary recently shipped support for it and that’s the only place you’ll find it for now. And even then, we just don’t know if the Chrome Canary implementation will find its way to the syntax when the feature becomes official.
The problem
Here’s the situation. You have an element. You’ve marked it up, plopped in contents, and applied a bunch of styles to it. Do you know how tall it is? Of course not! Sure, we can ask JavaScript to evaluate the element for us, but as far as CSS is concerned, the element’s computed dimensions are unknown.
That makes it difficult to, say, animate that element from height: 0
to height: whatever
. We need to know what “whatever” is and we can only do that by setting a fixed height on the element. That way, we have numbers to transition from zero height to that specific height.
.panel {
height: 0;
transition: height 0.25s ease-in;
&.expanded {
height: 300px;
}
}
But what happens if that element changes over time? Maybe the font changes, we add padding, more content is inserted… anything that changes the dimensions. We likely need to update that height: 300px
to whatever new fixed height works best. This is why we often see JavaScript used to toggle things that expand and contract in size, among other workarounds.
I say this is about the height
property, but we’re also talking about the logical equivalent, block-size
, as well as width
and inline-size
. Or any direction for that matter!
Transitioning to auto
That’s the goal, right? We tend to reach for height: auto
when the height dimension is unknown. From there, we let JavaScript calculate what that evaluates to and take things from there.
The current Chrome implementation uses CSS calc()
to do the heavy lifting. It recognizes the auto
keyword and, true to its name, calculates that number. In other words, we can do this instead of the fixed-height approach:
.panel {
height: 0;
transition: height 0.25s ease-in;
&.expanded {
height: calc(auto);
}
}
That’s really it! Of course, calc()
is capable of more complex expressions but the fact that we can supply it with just a vague keyword about an element’s height is darn impressive. It’s what allows us to go from a fixed value to the element’s intrinsic size and back.
I had to give it a try. I’m sure there are a ton of use cases here, but I went with a floating button in a calendar component that indicates a certain number of pending calendar invites. Click the button, and a panel expands above the calendar and reveals the invites. Click it again and the panel goes back to where it came from. JavaScript is handling the click interaction, triggering a class change that transitions the height in CSS.
A video in case you don’t feel like opening Canary:
This is the relevant CSS:
.invite-panel {
height: 0;
overflow-y: clip;
transition: height 0.25s ease-in;
}
On click, JavaScript sets auto height on the element as an inline style to override the CSS:
<div class="invite-panel" style="height: calc(auto)">
The transition
property in CSS lets the browser know that we plan on changing the height
property at some point, and to make it smooth. And, as with any transition or animation, it’s a good idea to account for motion sensitivities by slowing down or removing the motion with prefers-reduced-motion
.
What about display: none
?
This is one of the first questions that popped into my head when I read Chris’s post and he gets into that as well. Transitioning from an element from display: none
to its intrinsic size is sort of like going from height: 0
. It might seem like a non-displayed element has zero height, but it actually does have a computed height or auto
unless a specific height is declared on it.
So, there’s extra work to do if we want to transition from display: none
in CSS. I’ll simply plop in the code Chris shared because it nicely demonstrates the key parts:
.element {
/* hard mode!! */
display: none;
transition: height 0.2s ease-in-out;
transition-behavior: allow-discrete;
height: 0;
@starting-style {
height: 0;
}
&.open {
height: calc-size(auto);
}
}
- The element starts with both
display: none
andheight: 0
. - There’s an
.open
class that sets the element’s height tocalc(auto)
.
Those are the two dots we need to connect and we do it by first setting transition-behavior: allow-discrete
on the element. This is new to me, but the spec says that transition-behavior
“specifies whether transitions will be started or not for discrete properties.” And when we declare allow-discrete
, “transitions will be started for discrete properties as well as interpolable properties.”
Well, DevTools showed us right there that height: auto
is a discrete property! Notice the @starting-style
declaration, though. If you’re unfamiliar with it, you’re not alone. The idea is that it lets us set a style for a transition to “start” with. And since our element’s discrete height is auto
, we need to tell the transition to start at height: 0
instead:
.element {
/* etc. */
@starting-style {
height: 0;
}
}
Now, we can move from zero to auto
since we’re sorta overriding the discrete height with @starting-style
. Pretty cool we can do that!
To Shared Link — Permalink on CSS-Tricks
Transitioning to Auto Height originally published on CSS-Tricks, which is part of the DigitalOcean family. You should get the newsletter.
from CSS-Tricks https://ift.tt/OudTkHj
via IFTTT
Thursday, June 27, 2024
Partly Cloudy today!
With a high of F and a low of 65F. Currently, it's 73F and Fair outside.
Current wind speeds: 15 from the Southwest
Pollen: 0
Sunrise: June 27, 2024 at 05:27PM
Sunset: June 28, 2024 at 08:22AM
UV index: 0
Humidity: 49%
via https://ift.tt/oak4HvE
June 28, 2024 at 10:03AM
Wednesday, June 26, 2024
Thunderstorms/Wind Late today!
With a high of F and a low of 63F. Currently, it's 79F and Partly Cloudy outside.
Current wind speeds: 11 from the Southeast
Pollen: 0
Sunrise: June 26, 2024 at 05:27PM
Sunset: June 27, 2024 at 08:21AM
UV index: 0
Humidity: 43%
via https://ift.tt/oXMIZF1
June 27, 2024 at 10:03AM
Poppin’ In
Oh, hey there! It’s been a hot minute, hasn’t it? Thought I’d pop in and say hello. 👋
Speaking of “popping” in, I’ve been playing with the Popover API a bit. We actually first noted it wayyyyy back in 2018 when Chris linked up some information about the <dialog>
element. But it’s only been since April of this year that we finally have full Popover API support in modern browsers.
There was once upon a time that we were going to get a brand-new <popover>
element in HTML for this. Chromium was working on development as recently as September 2021 but reached a point where it was dropped in favor of a popover
attribute instead. That seems to make the most sense given that any element can be a popover — we merely need to attach it to the attribute to enable it.
<div popover>
<!-- Stuff -->
</div>
This is interesting because let’s say we have some simple little element we’re using as a popover:
<div>👋</div>
If this is all the markup we have and we do absolutely nothing in the CSS, then the waving emoji displays as you might expect.
Add that popover
attribute to the mix, however, and it’s gone!
That’s perhaps the first thing that threw me off. Most times something disappears and I assume I did something wrong. But cracking open DevTools shows this is exactly what’s supposed to happen.
There may be multiple popovers on a page and we can differentiate them with IDs.
<div popover id="tooltip">
<!-- Stuff -->
</div>
<div popover id="notification">
<!-- Stuff -->
</div>
That’s not enough, as we also need some sort of “trigger” to make the popover, well, pop! We get another attribute that turns any button (or <input>
-flavored button) into that trigger.
<button popovertarget="wave">Say Hello!</button>
<div popover id="wave">👋</div>
Now we have a popover
“targeted ” to a <button>
. When the button is clicked, the popover element toggles visibility.
This is where stuff gets really fun because now that CSS is capable of handling logic to toggle visibility, we can focus more on what happens when the click happens.
Like, right now, the emoji is framed by a really thick black border when it is toggled on. That’s a default style.
A few other noteworthy things are going on in DevTools there besides the applied border. For example, notice that the computed width and height behave more like an inline element than a block element, even though we are working with a straight-up <div>
— and that’s true even though the element is clearly computing as display: block
. Instead, what we have is an element that’s sized according to its contents and it’s placed in the dead center of the page. We haven’t even added a single line of CSS yet!
Speaking of CSS, let’s go back to removing that default border. You might think it’s possible by declaring no border on the element.
/* Nope 👎 */
#wave {
border: 0;
}
There’s actually a :popover-open
pseudo-class that selects the element specifically when it is in an “open” state. I’d love this to be called :popover-popped
but I digress. The important thing is that :popover-open
only matches the popover element when it is open, meaning these styles are applied after those declared on the element selector, thus overriding them.
Another way to do this? Select the [popover]
attribute:
/* Select all popovers on the page */
[popover] {
border: 0;
}
/* Select a specific popover: */
#wave[popover] {
border: 0;
}
/* Same as: */
#wave:popover-open {
border: 0;
}
With this in mind, we can, say, attach an animation to the #wave
in its open state. I’m totally taking this idea from one of Jhey’s demos.
Wait, wait, there’s more! Popovers can be a lot like a <dialog>
with a ::backdrop
if we need it. The ::backdrop
pseudo-element can give the popover a little more attention by setting it against a special background or obscuring the elements behind it.
I love this example that Mojtaba put together for us in the Almanac, so let’s go with that.
Can you imagine all the possibilities?! Like, how much easier will it be to create tooltips now that CSS has abstracted the visibility logic? Much, much easier.
Michelle Barker notes that this is probably less of a traditional “tooltip” that toggles visibility on hover than it is a “toggletip” controlled by click. That makes a lot of sense. But the real reason I mention Michelle’s post is that she demonstrates how nicely the Popover API ought to work with CSS Anchor Positioning as it gains wider browser support. That will help clean out the magic numbers for positioning that are littering my demo.
Here’s another gem from Jhey: a popover doesn’t have to be a popover. Why not repurpose the Popover API for other UI elements that rely on toggled visibility, like a slide-out menu?
Oh gosh, look at that: it’s getting late. There’s a lot more to the Popover API that I’m still wrapping my head around, but even the little bit I’ve played with feels like it will go a long way. I’ll drop in a list of things I have bookmarked to come back to. For now, though, thanks for letting me pop back in for a moment to say hi.
- On popover accessibility: what the browser does and doesn’t do (Hidde de Vries)
- If you’re using popover, also use the dialog element for your modal dialogs 📺(Hidde de Vries)
- Open UI and the Popover API (Brecht De Ruyte)
- Brief Note on Popovers with Dialogs (Adrian Roselli)
- Advanced Form Control Styling With Selectmenu And Anchoring API (Brecht De Ruyte)
- Using the Popover API for HTML Tooltips (Chris Coyier)
- Comparing the Popover API and the <dialog> element (LogRocket)
Poppin’ In originally published on CSS-Tricks, which is part of the DigitalOcean family. You should get the newsletter.
from CSS-Tricks https://ift.tt/YQngtS3
via IFTTT
Tuesday, June 25, 2024
Partly Cloudy today!
With a high of F and a low of 63F. Currently, it's 78F and Fair outside.
Current wind speeds: 4 from the North
Pollen: 0
Sunrise: June 25, 2024 at 05:26PM
Sunset: June 26, 2024 at 08:21AM
UV index: 0
Humidity: 24%
via https://ift.tt/ockUaKy
June 26, 2024 at 10:03AM
Monday, June 24, 2024
CSS Meditation #2: Who gives a flying frick what constitutes a “programming” language.
originally published on CSS-Tricks, which is part of the DigitalOcean family. You should get the newsletter.
from CSS-Tricks https://ift.tt/P8mqoZH
via IFTTT
CSS Meditation #1: If the code works as expected and it fits your mental model, then it’s perfect.
originally published on CSS-Tricks, which is part of the DigitalOcean family. You should get the newsletter.
from CSS-Tricks https://ift.tt/u69S2PV
via IFTTT
Sunday, June 23, 2024
Clear today!
With a high of F and a low of 63F. Currently, it's 80F and Clear outside.
Current wind speeds: 13 from the South
Pollen: 0
Sunrise: June 23, 2024 at 05:26PM
Sunset: June 24, 2024 at 08:21AM
UV index: 0
Humidity: 34%
via https://ift.tt/0Su9WEK
June 24, 2024 at 10:03AM
Saturday, June 22, 2024
Partly Cloudy today!
With a high of F and a low of 64F. Currently, it's 74F and Clear outside.
Current wind speeds: 8 from the Southeast
Pollen: 0
Sunrise: June 22, 2024 at 05:26PM
Sunset: June 23, 2024 at 08:21AM
UV index: 0
Humidity: 38%
via https://ift.tt/SK2Mn8q
June 23, 2024 at 10:03AM
Friday, June 21, 2024
Partly Cloudy today!
With a high of F and a low of 60F. Currently, it's 74F and Clear outside.
Current wind speeds: 11 from the Southwest
Pollen: 0
Sunrise: June 21, 2024 at 05:25PM
Sunset: June 22, 2024 at 08:21AM
UV index: 0
Humidity: 54%
via https://ift.tt/xCJ8Djb
June 22, 2024 at 10:03AM
Thursday, June 20, 2024
Partly Cloudy/Wind today!
With a high of F and a low of 62F. Currently, it's 75F and Partly Cloudy/Wind outside.
Current wind speeds: 22 from the South
Pollen: 0
Sunrise: June 20, 2024 at 05:25PM
Sunset: June 21, 2024 at 08:21AM
UV index: 0
Humidity: 51%
via https://ift.tt/i1TxHPq
June 21, 2024 at 10:03AM
Wednesday, June 19, 2024
Thunderstorms Late today!
With a high of F and a low of 59F. Currently, it's 63F and Cloudy outside.
Current wind speeds: 11 from the Southeast
Pollen: 0
Sunrise: June 19, 2024 at 05:25PM
Sunset: June 20, 2024 at 08:21AM
UV index: 0
Humidity: 88%
via https://ift.tt/jZzvNds
June 20, 2024 at 10:04AM
Tuesday, June 18, 2024
Mostly Cloudy today!
With a high of F and a low of 52F. Currently, it's 62F and Clear outside.
Current wind speeds: 15 from the Northeast
Pollen: 0
Sunrise: June 18, 2024 at 05:25PM
Sunset: June 19, 2024 at 08:20AM
UV index: 0
Humidity: 34%
via https://ift.tt/gWqiou1
June 19, 2024 at 10:03AM
Monday, June 17, 2024
Clear/Wind today!
With a high of F and a low of 59F. Currently, it's 80F and Clear/Wind outside.
Current wind speeds: 24 from the South
Pollen: 0
Sunrise: June 17, 2024 at 05:25PM
Sunset: June 18, 2024 at 08:20AM
UV index: 0
Humidity: 19%
via https://ift.tt/txs4uaP
June 18, 2024 at 10:03AM
Sunday, June 16, 2024
Partly Cloudy/Wind today!
With a high of F and a low of 65F. Currently, it's 82F and Clear/Wind outside.
Current wind speeds: 21 from the Southeast
Pollen: 0
Sunrise: June 16, 2024 at 05:24PM
Sunset: June 17, 2024 at 08:20AM
UV index: 0
Humidity: 34%
via https://ift.tt/oj6nJWP
June 17, 2024 at 10:03AM
Saturday, June 15, 2024
Partly Cloudy today!
With a high of F and a low of 60F. Currently, it's 71F and Fair outside.
Current wind speeds: 10 from the Southeast
Pollen: 0
Sunrise: June 15, 2024 at 05:24PM
Sunset: June 16, 2024 at 08:19AM
UV index: 0
Humidity: 43%
via https://ift.tt/LthwyUW
June 16, 2024 at 10:03AM
Friday, June 14, 2024
Partly Cloudy today!
With a high of F and a low of 57F. Currently, it's 65F and Clear outside.
Current wind speeds: 13 from the South
Pollen: 0
Sunrise: June 14, 2024 at 05:24PM
Sunset: June 15, 2024 at 08:19AM
UV index: 0
Humidity: 66%
via https://ift.tt/WOcl6Gb
June 15, 2024 at 10:03AM
Thursday, June 13, 2024
Partly Cloudy today!
With a high of F and a low of 59F. Currently, it's 73F and Clear outside.
Current wind speeds: 14 from the East
Pollen: 0
Sunrise: June 13, 2024 at 05:24PM
Sunset: June 14, 2024 at 08:18AM
UV index: 0
Humidity: 51%
via https://ift.tt/tBHGyDI
June 14, 2024 at 10:03AM
Wednesday, June 12, 2024
Partly Cloudy today!
With a high of F and a low of 63F. Currently, it's 78F and Fair outside.
Current wind speeds: 8 from the Southwest
Pollen: 0
Sunrise: June 12, 2024 at 05:24PM
Sunset: June 13, 2024 at 08:18AM
UV index: 0
Humidity: 36%
via https://ift.tt/DCSgRlV
June 13, 2024 at 10:03AM
Tuesday, June 11, 2024
Partly Cloudy today!
With a high of F and a low of 60F. Currently, it's 69F and Clear outside.
Current wind speeds: 11 from the South
Pollen: 0
Sunrise: June 11, 2024 at 05:24PM
Sunset: June 12, 2024 at 08:18AM
UV index: 0
Humidity: 55%
via https://ift.tt/Awr1chu
June 12, 2024 at 10:03AM
Monday, June 10, 2024
Partly Cloudy today!
With a high of F and a low of 56F. Currently, it's 64F and Partly Cloudy outside.
Current wind speeds: 9 from the Southeast
Pollen: 0
Sunrise: June 10, 2024 at 05:24PM
Sunset: June 11, 2024 at 08:17AM
UV index: 0
Humidity: 72%
via https://ift.tt/CH3sgWR
June 11, 2024 at 10:03AM
CSS Container Queries
Container queries are often considered a modern approach to responsive web design where traditional media queries have long been the gold standard — the reason being that we can create layouts made with elements that respond to, say, the width of their containers rather than the width of the viewport.
.parent {
container-name: hero-banner;
container-type: inline-size;
/* or container: hero-banner / inline-size; */
}
}
.child {
display: flex;
flex-direction: column;
}
/* When the container is greater than 60 characters... */
@container hero-banner (width > 60ch) {
/* Change the flex direction of the .child element. */
.child {
flex-direction: row;
}
}
Why care about CSS Container Queries?
- When using a container query, we give elements the ability to change based on their container’s size, not the viewport.
- They allow us to define all of the styles for a particular element in a more predictable way.
- They are more reusable than media queries in that they behave the same no matter where they are used. So, if you were to create a component that includes a container query, you could easily drop it into another project and it will still behave in the same predictable fashion.
- They introduce new types of CSS length units that can be used to size elements by their container’s size.
Registering Elements as Containers
.cards {
container-name: card-grid;
container-type: inline-size;
/* Shorthand */
container: card-grid / inline-size;
}
This example registers a new container named card-grid
that can be queried by its inline-size
, which is a fancy way of saying its “width” when we’re working in a horizontal writing mode. It’s a logical property. Otherwise, “inline” would refer to the container’s “height” in a vertical writing mode.
- The
container-name
property is used to register an element as a container that applies styles to other elements based on the container’s size and styles.
- The
container-type
property is used to register an element as a container that can apply styles to other elements when it meets certain conditions.
- The
container
property is a shorthand that combines thecontainer-name
andcontainer-type
properties into a single declaration.
Some Possible Gotchas
- The
container-name
property is optional. An unnamed container will match any container query that does not target a specific container, meaning it could match multiple conditions. - The
container-type
property is required if we want to query a container by itssize
orinline-size
. Thesize
refers to the container’s inline or block direction, whichever is larger. Theinline-size
refers to the container’s width in the default horizontal writing mode. - The
container-type
property’s default value isnormal
. And by “normal” that means all elements are containers by default, only they are called Style Containers and can only be queried by their applied styles. For example, we can query a container’sbackground-color
value and apply styles to other elements when the value is a certain color value. - A container cannot change its own styles. Rather, they change the styles of their contents instead. In other words, we cannot change the container’s
background-color
when it is a certain size — but we can change thebackground-color
of any element inside the container. “You cannot style what you query” is a way to think about it. - A container cannot be sized by what’s in it. Normally, an element’s contents influence its size — as in, the more content in it, the larger it will be, and vice versa. But a container must be sized explicitly as part of a flex or grid layout.
Querying a Container
@container my-container (width > 60ch) {
article {
flex-direction: row;
}
}
- The
@container
at-rule property informs the browser that we are working with a container query rather than, say, a media query (i.e.,@media
).
- The
my-container
part in there refers to the container’s name, as declared in the container’scontainer-name
property.
- The
article
element represents an item in the container, whether it’s a direct child of the container or a further ancestor. Either way, the element must be in the container and it will get styles applied to it when the queried condition is matched.
Some Possible Gotchas
- The container’s name is optional. If we leave it out, then any registered container would match when the conditions are met.
- A container’s
width
can be queried with when thecontainer-type
property is set to eithersize
orinline-size
. That’s becausesize
can query the element’swidth
orheight
; meanwhile,inline-size
can only refer to thewidth
. - You can query any length. So, in addition to
width
(i.e.,inline-size
), there’s an element’saspect-ratio
,block-size
(i.e.,height
), and orientation (e.g.portrait
andlandscape
). - Queries support the range syntax. Most of the examples so far have shown “greater than” (
>
) and “less than” (<
), but there is also “equals” (=
) and combinations of the three, such as “more than or equal to” (>=
) and “less than or equal to” (<=
). - Queries can be chained. That means we can write queries that meet multiple conditions with logical keywords, like
and
,or
, andnot
.
Container Queries Properties & Values
Container Queries Properties & Values
container-name
container-name: none | <custom-ident>+;
Value Descriptions
none
: The element does not have a container name. This is true by default, so you will likely never use this value, as its purpose is purely to set the property’s default behavior.<custom-ident>
: This is the name of the container, which can be anything, except for words that are reserved for other functions, includingdefault
,none
,at
,no
, andor
. Note that the names are not wrapped in quotes.
- Initial value:
none
- Applies to: All elements
- Inherited: No
- Percentages: N/A
- Computed value:
none
or an ordered list of identifiers - Canonical order: Per grammar
- Animation: Not animatable
container-type
container-type: normal | size | inline-size;
Value Descriptions
normal
: This indicates that the element is a container that can be queried by its styles rather than size. All elements are technically containers by default, so we don’t even need to explicitly assign acontainer-type
to define a style container.size
: This is if we want to query a container by its size, whether we’re talking about the inline or block direction.inline-size
: This allows us to query a container by its inline size, which is equivalent towidth
in a standard horizontal writing mode. This is perhaps the most commonly used value, as we can establish responsive designs based on element size rather than the size of the viewport as we would normally do with media queries.
- Initial value:
normal
- Applies to: All elements
- Inherited: No
- Percentages: N/A
- Computed value: As specified by keyword
- Canonical order: Per grammar
- Animation: Not animatable
container
container: <'container-name'> [ / <'container-type'> ]?
Value Definitons
If <'container-type'>
is omitted, it is reset to its initial value of normal
which defines a style container instead of a size container. In other words, all elements are style containers by default, unless we explicitly set the container-type
property value to either size
or inline-size
which allows us to query a container’s size dimensions.
- Initial value:
none
/normal
- Applies to: All elements
- Inherited: No
- Percentages: N/A
- Computed value: As specified
- Canonical order: Per grammar
- Animation: Not animatable
Container Length Units
Container Width & Height Units
Unit | Name | Equivalent to… |
---|---|---|
cqw |
Container query width | 1% of the queried container’s width |
cqh |
Container query height | 1% of the queried container’s height |
Container Logical Directions
Unit | Name | Equivalent to… |
---|---|---|
cqi |
Container query inline size | 1% of the queried container’s inline size, which is its width in a horizontal writing mode. |
cqb |
Container query block size | 1% of the queried container’s inline size, which is its height in a horizontal writing mode. |
Container Minimum & Maximum Lengths
Unit | Name | Equivalent to… |
---|---|---|
cqmin |
Container query minimum size | The value of cqi or cqb , whichever is smaller. |
cqmax |
Container query maximum size | The value of cqi or cqb , whichever is larger. |
Container Style Queries
Container Style Queries is another piece of the CSS Container Queries puzzle. Instead of querying a container by its size
or inline-size
, we can query a container’s CSS styles. And when the container’s styles meet the queried condition, we can apply styles to other elements. This is the sort of “conditional” styling we’ve wanted on the web for a long time: If these styles match over here, then apply these other styles over there.
CSS Container Style Queries are only available as an experimental feature in modern web browsers at the time of this writing, and even then, style queries are only capable of evaluating CSS custom properties (i.e., variables).
Browser Support
The feature is still considered experimental at the time of this writing and is not supported by any browser, unless enabled through feature flags.
This browser support data is from Caniuse, which has more detail. A number indicates that browser supports the feature at that version and up.
Desktop
Chrome | Firefox | IE | Edge | Safari |
---|---|---|---|---|
128 | No | No | 125 | TP |
Mobile / Tablet
Android Chrome | Android Firefox | Android | iOS Safari |
---|---|---|---|
125 | No | 125 | No |
Registering a Style Container
article {
container-name: card;
}
That’s really it! Actually, we don’t even need the container-name
property unless we need to target it specifically. Otherwise, we can skip registering a container altogether.
And if you’re wondering why there’s no container-type
declaration, that’s because all elements are already considered containers. It’s a lot like how all elements are position: relative
by default; there’s no need to declare it. The only reason we would declare a container-type
is if we want a CSS Container Size Query instead of a CSS Container Style Query.
So, really, there is no need to register a container style query because all elements are already style containers right out of the box! The only reason we’d declare container-name
, then, is simply to help select a specific container by name when writing a style query.
Using a Style Container Query
@container style(--bg-color: #000) {
p { color: #fff; }
}
In this example, we’re querying any matching container (because all elements are style containers by default).
Notice how the syntax it’s a lot like a traditional media query? The biggest difference is that we are writing @container
instead of @media
. The other difference is that we’re calling a style()
function that holds the matching style condition. This way, a style query is differentiated from a size query, although there is no corresponding size()
function.
In this instance, we’re checking if a certain custom property named --bg-color
is set to black (#000
). If the variable’s value matches that condition, then we’re setting paragraph (p
) text color
to white (#fff
).
Custom Properties & Variables
.card-wrapper {
--bg-color: #000;
}
.card {
@container style(--bg-color: #000) {
/* Custom CSS */
}
}
Nesting Style Queries
@container style(--featured: true) {
article {
grid-column: 1 / -1;
}
@container style(--theme: dark) {
article {
--bg-color: #000;
--text: #fff;
}
}
}
Specification
CSS Container Queries are defined in the CSS Containment Module Level 3 specification, which is currently in Editor’s Draft status at the time of this writing.
Browser Support
Browser support for CSS Container Size Queries is great. It’s just style queries that are lacking support at the time of this writing.
- Chrome 105 shipped on August 30, 2022, with support.
- Safari 16 shipped on September 12, 2022, with support.
- Firefox 110 shipped on February 14, 2023, with support.
This browser support data is from Caniuse, which has more detail. A number indicates that browser supports the feature at that version and up.
Desktop
Chrome | Firefox | IE | Edge | Safari |
---|---|---|---|---|
106 | 110 | No | 106 | 16.0 |
Mobile / Tablet
Android Chrome | Android Firefox | Android | iOS Safari |
---|---|---|---|
125 | 126 | 125 | 16.0 |
Demos!
Card Component
In this example, a “card” component changes its layout based on the amount of available space in its container.
Call to Action Panel
This example is a lot like those little panels for signing up for an email newsletter. Notice how the layout changes three times according to how much available space is in the container. This is what makes CSS Container Queries so powerful: you can quite literally drop this panel into any project and the layout will respond as it should, as it’s based on the space it is in rather than the size of the browser’s viewport.
Stepper Component
This component displays a series of “steps” much like a timeline. In wider containers, the stepper displays steps horizontally. But if the container becomes small enough, the stepper shifts things around so that the steps are vertically stacked.
Icon Button
Sometimes we like to decorate buttons with an icon to accentuate the button’s label with a little more meaning and context. And sometimes we don’t know just how wide that button will be in any given context, which makes it tough to know when exactly to hide the icon or re-arrange the button’s styles when space becomes limited. In this example, an icon is displayed to the right edge of the button as long as there’s room to fit it beside the button label. If room runs out, the button becomes a square tile that stacks the icons above the label. Notice how the border-radius
is set in container query units, 4cqi
, which is equal to 4% of the container’s inline-size (i.e. width) and results in rounder edges as the button grows in size.
Pagination
Pagination is a great example of a component that benefits from CSS Container Queries because, depending on the amount of space we have, we can choose to display links to individual pages, or hide them in favor of only two buttons, one to paginate to older content and one to paginate to newer content.
Articles & Tutorials
General Information
The Origin Story of Container Queries
A Cornucopia of Container Queries
Container Query Discussion
Container Queries: Once More Unto the Breach
Next Gen CSS: @container
251: Container Queries are the Future
Let’s Not Forget About Container Queries
Minimal Takes on Faking Container Queries
The Raven Technique: One Step Closer to Container Queries
Container Size Query Tutorials
Media Queries in Times of @container
Can We Create a “Resize Hack” With Container Queries?
A Few Times Container Size Queries Would Have Helped Me Out
A New Container Query Polyfill That Just Works
256: When to use @container queries
iShadeed’s Container Queries Lab
Minimal Takes on Faking Container Queries
Playing With (Fake) Container Queries With watched-box & resizeasaurus
Almanac References
CSS Container Queries originally published on CSS-Tricks, which is part of the DigitalOcean family. You should get the newsletter.
from CSS-Tricks https://ift.tt/ACO1cbY
via IFTTT
Mostly Clear today!
With a high of F and a low of 15F. Currently, it's 14F and Clear outside. Current wind speeds: 13 from the Southwest Pollen: 0 S...
-
So you want an auto-playing looping video without sound? In popular vernacular this is the very meaning of the word GIF . The word has stuck...
-
With a high of F and a low of 31F. Currently, it's 37F and Cloudy outside. Current wind speeds: 7 from the Northeast Pollen: 0 S...
-
Last year , we kicked out a roundup of published surveys, research, and other findings from around the web. There were some nice nuggets in ...