Technology News

Proposal for CSS @when

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.

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.

Jeff Bezos commits $1 billion to conservation as Amazon destroys the world
How to Source Content for Your Website?

Related Articles

A CSS Slinky in 3D? Challenge Accepted!

a-css-slinky-in-3d-challenge-accepted
Braydon Coyer recently launched a monthly CSS art challenge. He actually had reached out to me about donating a copy of my book Move Things with CSS to use as…

Neuromarketing: Takeaways for Marketers

neuromarketing-takeaways-for-marketers
“Sleep on it” – some of us hear this expression very often. But did you know that you can take this expression literally? Sleeping before making an important decision can…

How to Create Viral Instagram Visuals

how-to-create-viral-instagram-visuals
Instagram is all about visuals. The caption plays a role too, but people are focused on the visuals. You are most likely to be successful on Instagram if you create…