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.


The post Proposal for CSS @when appeared first on CSS-Tricks. You can support CSS-Tricks by being an MVP Supporter.


This content originally appeared on CSS-Tricks and was authored by Chris Coyier

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.


The post Proposal for CSS @when appeared first on CSS-Tricks. You can support CSS-Tricks by being an MVP Supporter.


This content originally appeared on CSS-Tricks and was authored by Chris Coyier


Print Share Comment Cite Upload Translate Updates
APA

Chris Coyier | Sciencx (2021-09-21T22:54:51+00:00) Proposal for CSS @when. Retrieved from https://www.scien.cx/2021/09/21/proposal-for-css-when/

MLA
" » Proposal for CSS @when." Chris Coyier | Sciencx - Tuesday September 21, 2021, https://www.scien.cx/2021/09/21/proposal-for-css-when/
HARVARD
Chris Coyier | Sciencx Tuesday September 21, 2021 » Proposal for CSS @when., viewed ,<https://www.scien.cx/2021/09/21/proposal-for-css-when/>
VANCOUVER
Chris Coyier | Sciencx - » Proposal for CSS @when. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/09/21/proposal-for-css-when/
CHICAGO
" » Proposal for CSS @when." Chris Coyier | Sciencx - Accessed . https://www.scien.cx/2021/09/21/proposal-for-css-when/
IEEE
" » Proposal for CSS @when." Chris Coyier | Sciencx [Online]. Available: https://www.scien.cx/2021/09/21/proposal-for-css-when/. [Accessed: ]
rf:citation
» Proposal for CSS @when | Chris Coyier | Sciencx | https://www.scien.cx/2021/09/21/proposal-for-css-when/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.