Web Development

Writing conditionals in CSS: when/else

Advertisements

CSS already has had conditionals in the form of @media queries or @support queries to selectively apply styling to the document. But there is a new proposal called when/else which takes it to a different level.

At the time of writing this post, the when proposal for when has been resolved to be adopted by CSSWG. The else proposal is a separate one and is a level 4 specification right now.

Let us take a look at how they work.

Using media queries

Using media queries, if we were to make our page responsive, we could add some logic like

@media (min-width: 800px) {
  /* styling logic for width > 800px */
}
CSS

And if we wanted to add some logic for smaller resolutions, we would do

@media (min-width: 800px) {
  /* styling logic for width > 800px */
}
@media (max-width: 799px) {
  /* width < 800px */
}
CSS

Using when/else

The when else syntax is cleaner:

@when media(min-width: 800px) {
  /* .. */ 
}
@else {
  /* .. */ 
}
CSS

And it even supports multiple conditionals

@when media(min-width: 800px) {
  /* .. */ 
}
@else media(min-width: 600px) {
  /* .. */ 
}
@else {
  /* .. */ 
}
CSS

Creating generalized rules

When else is not limited to @media queries. It can be combined with other conditionals as well.

So instead of doing:

@media (max-width: 799px) {
    @supports (display: flex) {
        .flex {
            flex-direction: row;
        }
    }
}
CSS

We can simply do:

@when media(max-width: 769px) and supports(display: flex) {
    .flex {
        flex-direction: row;
    }
}
CSS

@when can also be used inline

.button {
  padding: 2rem;
  @when element(max-width: 400px) {
    padding: 1rem;
  }
}
CSS

When the width is less than 400 pixels, the padding would be 1 rem instead of 2 rem.

Additional new ways of writing media queries

There is another proposal to the media queries spec, which will allow more semantic conditions while writing media queries themselves. It is again a level 4 specification right now.

Instead of writing min-width and max-width, media queries could then be rewritten as

@media (width <= 800px) {
  /* styling logic */
}
CSS

Conclusion

The proposals are moving along fairly quickly and CSS has been on a tear lately. There is a debate about using @if instead of @when but the argument is that it’d conflict with Sass. There is a proposal to replace it with @where as well.

Irrespective of the name, this will be a great addition to the CSS toolset.

Saransh Kataria

Born in Delhi, India, Saransh Kataria is the brain behind Wisdom Geek. Currently, Saransh is a software developer at a reputed firm in Austin, and he likes playing with new technologies to explore different possibilities. He holds an engineering degree in Computer Science. He also shares his passion for sharing knowledge as the community lead at Facebook Developer Circle Delhi, NCR which is a developer community in Delhi, India.

View Comments

  • Interesting, I look forward to implementing these technologies into a framework that is in the works... If launch and acceptance line up!

    • Though these are almost at the final stages of approval, the @where spec will be landing first. The only discussion pending there is the name and that should hopefully be a quick one. The @else is a little less further along and will take some time. Not sure about your framework's timeline, but I would not hold my breath for these if you intend to launch soon.

Share
Published by
Saransh Kataria
Tags: CSS

Recent Posts

How To Get The Hash of A File In Node.js

While working on a project, I wanted to do an integrity check of a file…

22 hours ago

Native popover API in HTML

Popovers have been a problem that was typically solved by using a third-party solution. But…

1 week ago

Node.js 20.6 adds built-in support for .env files

Node.js 20.6 added built-in support for the .env file. This is an excellent addition to the platform…

2 weeks ago

Object destructuring in TypeScript

Object destructuring is a powerful ES 6 feature that can help developers write cleaner code.…

4 weeks ago

Improve git clone performance in a CI pipeline

Have you felt particularly annoyed by the time it takes to clone a large repository,…

1 month ago

Fix: Hydration failed because the initial UI does not match what was rendered on the server

Within a React or Next.js app, if you encounter the error "Hydration failed because the…

1 month ago
Advertisements