DEV Community

Cover image for Inline if() in CSS πŸ€”: What You Need to Know About This Upcoming Feature
Aslam Shah
Aslam Shah

Posted on

6

Inline if() in CSS πŸ€”: What You Need to Know About This Upcoming Feature

Last week, the CSS Working Group gathered in Spain, and there was some exciting news

πŸ₯

they agreed to add an inline if() function to CSS.

This idea had been bouncing around for a while, but they finally managed to pin it down to an MVP that could be implemented quickly. After some friendly chats with implementers and putting out a solid proposal, they reached a consensus. It's a significant leap forward for CSS, adding some nifty new tricks to the toolkit πŸŽ‰.

Lets see how this will work ( according to the proposal ):




.my-element {
    color: if(
        style(--status: active) ? var(--color-active-30) :
        style(--status: error) ? var(--color-error-30) :
        /* Add other statuses here */
        var(--color-default-30)
    );
    background-color: if(
        style(--status: active) ? var(--color-active-95) :
        style(--status: error) ? var(--color-error-95) :
        /* Add other statuses here */
        var(--color-default-95)
    );
}



Enter fullscreen mode Exit fullscreen mode

Based on the --status variable, we can conditionally change the css properties in a single line.

You can also use it using pre defined variables.



:root {
    --xl: media(width > 1600px);
    --l: media (width > 1200px);
    --m: media (width > 800px);
}
/* usage */
border-width: if(
    var(--xl) ? var(--size-3) :
    var(--l) or var(--m) ? var(--size-2) :
    var(--size-1)
);




Enter fullscreen mode Exit fullscreen mode

Browser Support:

Don’t get too excited just yetβ€”it'll likely take at least 1-2 years for this to make its way into our browsers. So, while the future of CSS looks bright, we’ll need to be patient πŸ˜€!

Stay tuned and follow me for the latest updates and tips on new web development features, so you'll be the first to know when the future of CSS and other web technologies lands in your browser!

Photo by Cam Adams on Unsplash

Sentry blog image

How to reduce TTFB

In the past few years in the web dev world, we’ve seen a significant push towards rendering our websites on the server. Doing so is better for SEO and performs better on low-powered devices, but one thing we had to sacrifice is TTFB.

In this article, we’ll see how we can identify what makes our TTFB high so we can fix it.

Read more

Top comments (1)

Billboard image

Deploy and scale your apps on AWS and GCP with a world class developer experience

Coherence makes it easy to set up and maintain cloud infrastructure. Harness the extensibility, compliance and cost efficiency of the cloud.

Learn more

πŸ‘‹ Kindness is contagious

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

Okay