DEV Community

Cover image for if/else in CSS
Damian Brdej
Damian Brdej

Posted on

150 43

if/else in CSS

As we know, there are no conditional statements in CSS, but this may soon change with new @when and @else operator

Currently, the only way to perform a conditional statement was to use media queries like this:

@media (min-width: 600px) {
  /* ... */ 
}
@media (max-width: 599px) {
  /* ... */
}
Enter fullscreen mode Exit fullscreen mode

It works perfectly, but in the new proposal it looks much cleaner and very similar to many programming languages.

@when media(min-width: 600px) {
  /* ... */ 
}
@else {
  /* ... */ 
}
Enter fullscreen mode Exit fullscreen mode

We can do multiple conditions as well by using multiple @else statements, and not just use @media, but @supports too.

@when media(width >= 600px) and media(pointer: fine) and supports(display: grid) {
  /* A */
} @else supports(caret-color: red) and supports(background: double-rainbow()) {
  /* B */
} @else {
  /* C */
}
Enter fullscreen mode Exit fullscreen mode

There may be questions about the naming of this new feature. Some people think that @if would be a better name than @when, the reason behind this naming is probably that Sass already uses @if, and it would be annoying to many developers if they had to refactor their Sass logic.

The bad news is that it's not supported by any browser at this moment, it's not even listed on caniuse.com.

What do you think about this new proposal?

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (19)

Collapse
 
iamschulz profile image
Daniel Schulz β€’

But there are already conditional statements in CSS, they're just not named if/else.

:checked { /* if */ }
:not(:checked) { /* else */ }

[data-attr="true"] { /* if */ }
[data-attr="false"] { /* elseif */ }
:not([data-attr]) { /* else */}
Enter fullscreen mode Exit fullscreen mode

As to the new synatx: I'm curious if it's going to solve componentization issues or rather introduce new side effects and specificity problems. I'll be happy to stay with the old and trusty

width: var(--s-width);
@media (min-width > 600px) {
    width: var(--l-width);
}
Enter fullscreen mode Exit fullscreen mode

for as long as new best practices haven't surfaced. I guess i'm just used to structure my styles by viewport and not by instructions.

Collapse
 
mrbns profile image
Mr. Binary Sniper β€’

These are different things..

Collapse
 
darkwiiplayer profile image
π’ŽWii πŸ³οΈβ€βš§οΈ β€’

It works perfectly

Not quite: What happens for 599.5px? Neither of the two rules applies. If you want it to work perfectly, you'll have to use not instead.

Collapse
 
devdufutur profile image
Rudy NappΓ©e β€’

How can your device have halves pixels ? πŸ€”

Collapse
 
sergeysova profile image
Sergey Sova β€’

Yep. On the phones. Pixels in html/css is not real pixels

Collapse
 
quentindamianino profile image
Damian Brdej β€’

Good point, I didn't think about it

Collapse
 
adam_cyclones profile image
Adam Crockett πŸŒ€ β€’ β€’ Edited

A good long while ago I was working on a new idea before svelte called Jess
github.com/adam-cyclones/Jess/blob...

And

github.com/adam-cyclones/Jess/blob...

Jess is abandonware but the idea was JavaScript for presentational logic and therefore if else
I have no time for this project which is a shame but you lot might like to get inspired πŸ€·β€β™‚οΈ

Collapse
 
matemiller profile image
Matt Miller β€’ β€’ Edited

Also you can play with the :has() and the :empty() selectors as well!
They are pretty useful for this if/else topic! Cheers!

developer.mozilla.org/en-US/docs/W...
developer.mozilla.org/en-US/docs/W...

Collapse
 
jmzeph profile image
jmzeph β€’

why not go further and use switch cases.

Collapse
 
oumaymasghayer profile image
Oumayma JavaScript Developer β€’

This is helpful, Thank you.

Collapse
 
peterbowater profile image
Peter Bowater β€’

This seems shorter, and isn't this SASS style also proposed for plain CSS?

.div {
.......
@media (min-width: 600px {
.......
}
}

Sure you have to write the occasional override, but that is a rarity.

Collapse
 
vish_21 profile image
Vijay β€’

Will help in code unification

Collapse
 
matemiller profile image
Matt Miller β€’

I like your concept! And I'm also a fan of these kind of solutions to follow. Year by year there are many good ideas about the CSS to expand it to the next level to make it like an "~analog programming language" which is not dynamic like JS, but more like a static code which is capable to "~reply" or "~react" for "environment changes" such as media queries, scrolls ..etc. So, would be great to have these CSS (new) features at least as a default ones to get a chance/possibility to play with. Therefore would be amazing thing to use (pure) CSS as a tool to make the web both responsive and adaptive as well at the same time. I hope you get the idea what I mean. Cheers!

Collapse
 
matemiller profile image
Matt Miller β€’

By the way Damian, if you are up to this topic to develop something based on that particular concept then please let me know about it!

Collapse
 
jrohatiner profile image
Judith β€’

This is good to know. Thx for the info. Usually I put these future specs aside until they get adopted and there is multiple browser support. Too bz keeping track of whats current. πŸ€·πŸ»β€β™€οΈ

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free β†’

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay