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

Put a Background on Open Details Elements

put-a-background-on-open-details-elements
One thing that can be just a smidge funky about the <details> element is that, when open, it’s not always 100% clear what is inside that element and what isn’t.…

Web Design Trends For 2022: Expert Opinions

web-design-trends-for-2022-expert-opinions
The world does not stand still and things change every year, if no color daily. There are new technologies, trends and growing interests in many areas. Design is no exception.…

Organize your CSS declarations alphabetically

organize-your-css-declarations-alphabetically
There are a few schools of thought when it comes to organizing your CSS declarations. Each approach uses an underlying concept to impose a specific declaration order. When I say…

What font does CNN use that looks so distinctive?

What font does CNN use that looks so distinctive?
Advertisement The choice of font and the overall typography consideration is significant. Anybody can dramatically reduce uncertainty by selecting suitable font faces and allocating hierarchy where appropriate. Allowing customers to…