CSS is on a tear lately. Again, I’ve heard of a brand new thing I’ve never seen before, and again it’s via Miriam: CSS Conditionals.
🎉 CSSWG just resolved to adopt @tabatkins when/else proposal into the next level of CSS Conditionals. Here's the proposal:https://t.co/IXEOK7xKcL
— Mia, on Bass (@TerribleMia) September 15, 2021
There is already such a thing as logic in media queries. In fact, a media query is already logic.
@media (min-width: 600px) {
/* WHEN this media query is true, do these styles. */
}
And if you want to have styles that are mutually exclusive to the above, you’d write two media queries:
@media (min-width: 600px) {
/* ... */
}
@media (max-width: 599px) {
/* ... */
}
That’s a little… fidgety. The syntax is much cleaner in this new proposal:
@when media(min-width: 600px) {
/* ... */
}
@else {
/* ... */
}
Looks like you can do multiple conditions via and
, have a waterfall logic stack with multiple @else
statements, and not just use @media
, but @supports
as well.
@when media(width >= 400px) and media(pointer: fine) and supports(display: flex) {
/* A */
} @else supports(caret-color: pink) and supports(background: double-rainbow()) {
/* B */
} @else {
/* C */
}
Looks very logical and handy to me!
I saw one little squabble about the naming. @if
could be a logical name here too. But Sass uses @if
and it would be super annoying to a ton of developers if they had to refactor all their Sass logic to something new or however that would fall out. Should CSS cede to any preprocessor out there? Nah, but Sass has been around a long time and is super popular, and there is a perfectly good alternative, so why cause the pain? In that thread, it’s not just about Sass either — some folks think @when
is a better name anyway.
This is bad code that is not equivalent to the
@when
shown. Remember that CSS pixels are not integral, so you can absolutely have device widths between 599px and 600px (e.g. with a device pixel ratio of 2, a window that is 1199 device pixels wide will by 599.5 CSS pixels wide). Instead, you should write:It’s a pity negation requires a medium to be specified, since
not all and
is an idiosyncratic spelling of what a reasonable person might expect to be justnot
, but that’s what’s needed.… and that’s why I think getting
@when
into CSS might be useful, because too many people get these sorts of negations wrong.Thanks!
We could also do something like:
Introducing an
element()
thingy in passing, and using the calculated value of a property of the element to switch the condition on or off.Did you ever find a solution for this. I am paying 200 bucks if you can find something like that works in html email.
Disagree with the last paragraph. Should be
@if
refactoring sass is pretty simple and temporary annoying, but@when
would be different from every other language, so something specific you have to remember and forever annoying. Otherwise, cool addition!Different sure, but CSS isn’t like other languages. I think this fits.
It’s interesting to me that there’s such a hot debate around @if versus @when, but it seems as if no one has brought up the fact that SASS uses @else as well.
I’d guess @else isn’t a problem because Sass won’t compile @else if not used after an @if. Though it might throw a syntax error during compilation.
Note that
calc
is a problem in Less in a similar way. I mean you have to use a string escape~"calc(100% - 20px)"
So I guess you could work around
@if
problem too… But personally I’m fine with@when
name. There is also when in xsl so css wouldn’t be the first.Honestly, since CSS is declarative rather than imperative this makes sense. Using “when” disambiguates this concept from flow-control.
The timeliness of the word works for it, don’t just do the following IF this condition is so, do it WHENEVER it is so.
Wow, declarative not imperative, this is the best argument, brilliant !
Interesting. Not long before we start unit testing CSS files then I guess? ;)