DEV Community

Cover image for CSS nesting is coming

CSS nesting is coming

Alvaro Montoro on January 15, 2023

Browsing caniuse.com the other day, I found a pleasant surprise: a little green flag in a red box for a feature I'd been waiting for a long time. ...
Collapse
 
darkwiiplayer profile image
𒎏Wii 🏳️‍⚧️

Would it be too pessimistic to say "Meh, firefox will probably just kill it like every other good thing in the web"?

I'm probably being a bit hard on firefox here, but JFC every single feature I want seems to be waiting for them to finally implement it.

Collapse
 
ivan_jrmc profile image
Ivan Jeremic

You mean Safari

Collapse
 
alvaromontoro profile image
Alvaro Montoro

I ha(v/t)e to admit that Safari has been doing a good job lately. While it is way behind in features and options, some of the latest CSS things pop up on Safari first... which doesn't make much sense, but oh well...

Collapse
 
darkwiiplayer profile image
𒎏Wii 🏳️‍⚧️

Doesn't safari actually still have TCO in its javascript engine, even though firefox unilaterally decided to kill that feature for no reason whatsoever?

Automatically x3 times better if you ask me

Collapse
 
nlxdodge profile image
NLxDoDge

The main reasons I use SCSS/SASS is because of nesting and variables. If they fix those two we are getting closer to not needing to compile everything, that would be huge.

But I have some other things, like functions or using a variable in a function call that I really like to see as well.

Collapse
 
darkwiiplayer profile image
𒎏Wii 🏳️‍⚧️

What do you need variables for that you cannot do with custom properties already?

Collapse
 
nlxdodge profile image
NLxDoDge • Edited

I had some issues with trying to make the border of div transparent but not the element itself (else a opacity filter would have worked).

So instead I wanted to do:

div {
    border-color: argb(#123123, 0.4);
}
Enter fullscreen mode Exit fullscreen mode

But then with a variable instead as I wanted to make a light and dark togglable theme. But I found out that using css variables in this context doesn't work.

:host {
    --test: #213213;
}

div {
    border-color: argb(var(--test), 0.4);
}
Enter fullscreen mode Exit fullscreen mode

Maybe I am doing something wrong (or Firefox doesn't allow this?).

Thread Thread
 
darkwiiplayer profile image
𒎏Wii 🏳️‍⚧️ • Edited

Well, no, that doesn't work, because argb isn't a thing, and if you want to use rgb you'll have to provide R, G and B separately.

What you can do, for example:

:host {
   --theme-hue: 140deg;
   --theme-saturation: 20%;
   --theme-lightness: 10%;
}

div {
   border-color: hsl(
      var(--theme-hue),
      var(--theme-saturation),
      var(--theme-lightness),
      0.4
   );
}
Enter fullscreen mode Exit fullscreen mode

and I'm 90% sure you can combine those three variables into a --theme-hsl variable somehow but cba to test it right now so won't include it in the code.


EDIT: And of course, once CSS Color Module Level 5 becomes a thing, you can just do

my-element {
   --test: #123123;
   border-color: rgb(from var(--test) r g b 0.2);
}
Enter fullscreen mode Exit fullscreen mode
Thread Thread
 
nlxdodge profile image
NLxDoDge

Thanks for your explanation, I don't really do front-end that much (I do Java backend at work) but I never knew you could to things like this. Thanks!

Thread Thread
 
starkraving profile image
Mike Ritchie

argb isn’t a thing as mentioned, but rgba is, which is what you’re writing in the sample code anyways. R G and B go from 0 to 255, and A goes from 0.0 to 1.0

Collapse
 
fruntend profile image
fruntend

Сongratulations 🥳! Your article hit the top posts for the week - dev.to/fruntend/top-10-posts-for-f...
Keep it up 👍

Collapse
 
orix profile image
Waleedh Nihal

Cool makes work much easier 👍🏽!

Collapse
 
ota200 profile image
O.T.A

Css is going to get much easier now, thank you for sharing, can't wait for this feature :)

Collapse
 
bigahuna profile image
Michael Kettel

I was using sass/scss just because it is so much better to have nested css instructions. Love to see this feature becoming a standard...

Collapse
 
anothertobi profile image
Tobi

This is a nightmare. Css should only define styles, not handle logic. Javascript is enough logic to handle already

Collapse
 
alvaromontoro profile image
Alvaro Montoro

How is this logic?

Collapse
 
anothertobi profile image
Tobi

If (thing a is nested in thing b)
Apply style to thing a

Now what if I don't want to use thing b anymore but thing a? Then I have to change the css file, even though that makes no sense at all

Thread Thread
 
alvaromontoro profile image
Alvaro Montoro

That is how it happens right now in CSS when you put two selectors together (even without the nesting):

.car {
  width: 200px;
}

.car .door {
  color: red;
}
Enter fullscreen mode Exit fullscreen mode

Those styles apply to the .door in a .car, but not to a .door inside a .house.

With CSS nesting this will be more evident for the developer:

.car {
  width: 200px;

  .door {
    color: red;
  }
}
Enter fullscreen mode Exit fullscreen mode

It's just syntactic sugar that helps keep the code more organized and clean. The main danger is getting into the curly braces hell.

Thread Thread
 
anothertobi profile image
Tobi

Fair enough. Still I would rather move in a direction where logic is handled in Javascript and css is styling and only styling. Just today I had to debug some complex sass function. That's hell. But of course this is subjective

Thread Thread
 
someofthethings profile image
Peter Carter • Edited

Nesting is not logic in any way that makes the code more difficult to read when used properly and doesn't muddy seperation of concerns in any meaningful way. Once you get really adept at SCSS/Sass you start to be able to nest in ways that are intuitive, elegent and make the code a joy to read and reason about.

If you're having to debug a complex Sass function that's not intuitive, it is 100% not an issue with nesting, and everything to do with the developer not doing stuff properly. If you're struggling here then SonarCloud has some really neat features for helping to organise such code. If it's really bad it'll throw a proper riot-tantrum initially, but if you can find anyone good enough to actually fix issues it flags then you'll see what good nesting does for you.

Collapse
 
darkwiiplayer profile image
𒎏Wii 🏳️‍⚧️ • Edited

@alvaromontoro by the way, thoughts on @scope?