No more specificity issues?! (or all new ones đŸ€”) - A look at CSS Cascade Layers

SdĂ­let
VloĆŸit
  • čas pƙidĂĄn 31. 05. 2024
  • Cascade layers are pretty much here, so it's time to learn how they work!
    🔗 Links
    ✅ Stephanie Eckles article: www.smashingmagazine.com/2022...
    ✅ Bramus' article: www.bram.us/2021/09/15/the-fu...
    ✅ Una's video: ‱ CSS Cascade Layers: An...
    ✅ My video on Specificity: ‱ CSS Specificity explained
    ⌚ Timestamps
    00:00 - Introduction
    01:14 - Specificity issues
    04:22 - Creating our first layer
    05:16 - Traditional CSS layers
    07:30 - CSS Layers are like sub-layers of the author layer
    07:44 - What happens when something isn't in a layer?
    08:07 - Creating multiple layers
    11:04 - What happens when we use the same layer in two difference places
    12:13 - Declaring the order first, then using them later
    14:00 - !important flips things around
    #css
    --
    Come hang out with other dev's in my Discord Community
    💬 / discord
    Keep up to date with everything I'm up to
    ✉ www.kevinpowell.co/newsletter
    Come hang out with me live every Monday on Twitch!
    đŸ“ș / kevinpowellcss
    ---
    Help support my channel
    👹‍🎓 Get a course: www.kevinpowell.co/courses
    👕 Buy a shirt: teespring.com/stores/making-t...
    💖 Support me on Patreon: / kevinpowell
    ---
    My editor: VS Code - code.visualstudio.com/
    ---
    I'm on some other places on the internet too!
    If you'd like a behind the scenes and previews of what's coming up on my CZcams channel, make sure to follow me on Instagram and Twitter.
    Twitter: / kevinjpowell
    Codepen: codepen.io/kevinpowell/
    Github: github.com/kevin-powell
    ---
    And whatever you do, don't forget to keep on making your corner of the internet just a little bit more awesome!

Komentáƙe • 119

  • @LivingRegiment
    @LivingRegiment Pƙed 2 lety +42

    "Uh oh, looks like we've run into a specificity issue..." so mischieveous 😂 I cannot wait for the official release. CSS Layers are going to be a game changer!

    • @peternicholson26
      @peternicholson26 Pƙed 2 lety +1

      Nerdiest. T-Shirt. Ever

    • @LivingRegiment
      @LivingRegiment Pƙed 2 lety +3

      @@peternicholson26 If Kevin turned this into a shirt, I would totally buy it lol!

    • @peternicholson26
      @peternicholson26 Pƙed 2 lety +3

      I would buy it just out of respect for the amount of times he said specificity without mispronunciation. Ridiculous

    • @Allformyequine
      @Allformyequine Pƙed 8 měsĂ­ci

      HAHA I laughed at that too lol!

  • @dgcp354
    @dgcp354 Pƙed 2 lety +6

    I remember you struggling to say specificity, I think there is actually a dedicated video about it, glad you're saying it with ease is.

  • @SebastianZartner
    @SebastianZartner Pƙed 2 lety +12

    I am really looking forward to cascade layers, as they make my CSS life so much easier! Thank you, Kevin, for explaining this new feature in detail!
    Also a big thank you to the implementers and the specification writers for pushing this feature forward in such a short time! Special thanks go to Miriam Suzanne here!

  • @faisal_foyez
    @faisal_foyez Pƙed 2 lety +9

    Letting the developers defining the layers is going to save their life in large projects. Feeling relieved.

  • @Morrile1
    @Morrile1 Pƙed 2 lety +12

    This is the “Great stuff” that Kevin is a godsend in sharing and explaining in a way we can follow.

  • @rockmonty
    @rockmonty Pƙed 2 lety +1

    Great video again. not an easy subject to explain, but Kevin battles through and gives everyone a good understanding . thank you. its nice that the video isn't too long either

  • @NicholasAlexander1
    @NicholasAlexander1 Pƙed 2 lety +1

    This is brilliant! Makes CSS specificity far less mysterious - when you find yourself searching through modules. Now nodules are essentially layered namespaces. Thank you for the clarity.

  • @ritwik5774
    @ritwik5774 Pƙed 2 lety

    Great video! Very exciting times ahead for CSS with layers and Houdini.

  • @nomadshiba
    @nomadshiba Pƙed rokem

    this video was one year ago and i didnt even know this was existed
    this will be a life changer

  • @vishwa
    @vishwa Pƙed 2 lety

    Thanks for the video. This thing is awesome. Real problem solver for large projects.

  • @iwanttobealihgt
    @iwanttobealihgt Pƙed rokem

    thanks for the explanation and examples. Extremely handy!

  • @voidmind
    @voidmind Pƙed 2 lety +3

    Something that's missing from this video is that you can set a layer to imported CSS files like "@import 'utilities.css' layer(utilities);". So you could import the CSS from a component library this way and have an easier time overriding some styles it defines.

    • @RondellKB
      @RondellKB Pƙed 9 měsĂ­ci

      Hey bro, I have a problem with this. I am using gulp to concatenate component css files into a main styles.css. In each css file, I have the css within layers, but when the files get concatenated, there are multiple components inside the @component layer for example. this causes the layer specificity to not work. Any idea how to get this to work with my use case?

  • @andreibicu5592
    @andreibicu5592 Pƙed 7 měsĂ­ci

    Lovely explanations! I understood better than when read from Tailwind, for instance.

  • @jybedesign
    @jybedesign Pƙed 4 měsĂ­ci

    Excellent video. Thx a lot!

  • @apuravela
    @apuravela Pƙed 2 lety

    Very clear, as ussual. Thanks Kevin

  • @GnomePuntTrainerYT
    @GnomePuntTrainerYT Pƙed 2 lety +7

    You prefaced by saying it's around the corner, and listed the experimental dev browsers that supports it, but it's really important to drive home the point of how CSS Cascade Layers has a whooping 0.04% support right now in the web space. I think we'll be waiting a long time before this CSS feature becomes relevant, but it's still a lot of fun to experiment with and learn!

    • @KevinPowell
      @KevinPowell  Pƙed 2 lety +5

      By being in the experimental browsers right now without a flag, it won't be long for it to make the jump to the production ones, and at that point support will hit 70% within a few weeks (of it being in all 3 of course). It'll take awhile before they're safe to use though, since you won't really be building in fallbacks.

  • @manytrickpony695
    @manytrickpony695 Pƙed 2 lety

    Great way to start the day.

  • @Take-the-Ticket
    @Take-the-Ticket Pƙed 2 lety

    Congrats on your specificity victory!

  • @mrastrogastro
    @mrastrogastro Pƙed rokem

    This will save me Kevin, I'm working on an old project with such bad css practice. I will create my layer and voila! :D

  • @JulienReszka
    @JulienReszka Pƙed rokem

    Very important video thx

  • @Ceko
    @Ceko Pƙed 2 lety +1

    Great video. I was thinking about this technique to override bootstraplike frameworks with importants though. If you have such framework, isn’t it always loaded first? I.e. you can define the layers order but at that point the framework is already loaded?

  • @tithos
    @tithos Pƙed 26 dny +1

    What lorem are you using?

  • @raphaelsanchez1235
    @raphaelsanchez1235 Pƙed 2 lety

    Oh Kevin ! Thanks so much !!!! Can’t wait official release !
    But i so this in tailwind yet. Can you think they used post css preset-env ?

  • @gwemula
    @gwemula Pƙed 2 lety

    Is this similar to SASS nesting where we use a parent-element class selector (e.g. .navbar) and then style all children elements in it?

  • @grezvany1372
    @grezvany1372 Pƙed 2 lety

    I'm happy about the reversed !important order, because it will allow people to overwrite styles at User (or even browser) level when needed.
    We, the designers/developers, always want to make stuff visually look good and be user friendly for "all" visitors. However some things may be bad for specific end users due to visual issues, and not being able to overwrite styling without breaking 90% of the time in a uniform way is an actual issue. So this new standard will actually be a welcome one.
    Accessibility will always be extremely important, but this will help as well.

  • @SamCrowetheCreativeCrowe
    @SamCrowetheCreativeCrowe Pƙed 2 lety +1

    Thanks!

  • @eivindmn
    @eivindmn Pƙed 2 lety +2

    If I understand this correctly, that means that for instance plugin creators for wordpress or similar can declare layers to their css, which then makes my job of overwriting their styling to integrate it better into my theme a lot easier? Because that has been an annoying battle for several occasions.

  • @StefanH
    @StefanH Pƙed 2 lety

    This is HUGE!

  • @dolevgo8535
    @dolevgo8535 Pƙed 2 lety

    is there a way to check if said framework is using layers? and if not, a way to wrap it?

  • @albuslrc
    @albuslrc Pƙed 2 lety

    Great!

  • @sergejstojanovic2518
    @sergejstojanovic2518 Pƙed 2 lety +1

    Love the tumbnail

  • @Fachuro
    @Fachuro Pƙed 2 lety +2

    I just wish there was a way to undo !important in CSS - for those times where you work on legacy code and old projects where a client has been puking !important tags all over everywhere they couldn't figure out how specificity actually works - or you know, just because... Because you dont really just wanna go in and remove them all as you could end up breaking something else, because its not like they wrote any tests or snapshots to tell you when something gets broken either - and some clients have systems that go back 2-3 decades and just simply have too much code to realistically be able to know all the places that would rely on that style rule...

  • @eddiemcconkie4111
    @eddiemcconkie4111 Pƙed 2 lety

    Specificity: 31
    Specificicity: 0
    You've set a new record Kevin!

  • @lizt_official
    @lizt_official Pƙed 2 lety

    Gamechanger 😍

  • @yassinih5189
    @yassinih5189 Pƙed 2 lety

    you can easily give a value of inherent to the a, to avoid that specificity issue

  • @lewis4929
    @lewis4929 Pƙed 4 měsĂ­ci

    With this make it easier to manage dark and light themes?

  • @walnutsandbeastiality866
    @walnutsandbeastiality866 Pƙed 2 lety +1

    ,,Do I need to get into specificsss?"
    (Silvio Dante, The Sopranos, 1999)

  • @chrisallen1745
    @chrisallen1745 Pƙed 2 lety

    I need this t shirt! Where the hell did you get it!? Also, great stuff as usual!

  • @dan110024
    @dan110024 Pƙed 2 lety

    Nice to see you’ve overcome your specificity issue. I mean the one getting tongue tied 😁
    I totally made this comment before you even mentioned it loll

  • @antisk8
    @antisk8 Pƙed 2 lety

    Great video. Better shirt

  • @sergejstojanovic2518
    @sergejstojanovic2518 Pƙed 2 lety

    Can we name layers however we want or are there already existing names for them: reset, base, components, etc?

    • @KevinPowell
      @KevinPowell  Pƙed 2 lety +2

      We can name them however we want, and create as many as we want :)

  • @EmSixTeen
    @EmSixTeen Pƙed 2 lety

    This is confusing, but.. still interesting!

  • @Acaykath
    @Acaykath Pƙed 2 lety +2

    !important is for when the user wants to overwrite something and can't figure out why it gets overridden by the creator. I use it all the time in reStyle when I want a page to have a dark theme that doesn't burn my eyes. But, it really should never be used in production code. If your website code has !important in it, you're doing something wrong.
    This layers thing sounds real useful. Hopefully, this and nesting become mainstream soon.

    • @Xamy-
      @Xamy- Pƙed 2 lety +4

      Or you’re caught by corporate bloat and it’s the only way to override shitty corporate “frameworks”

  • @carolmckay5152
    @carolmckay5152 Pƙed 2 lety +1

    Is there, or will there be a way to import an external css like this: ?

    • @erics2133
      @erics2133 Pƙed 2 lety +1

      That went through my mind as well. Maybe create a wrapper that consists of nothing but a layer with an @import inside it? I can see where that could or couldn't work, so I'll have to test it. Using @import is/was discouraged, I'll have to test how this would affect the reasoning as well.

  • @danielk.3017
    @danielk.3017 Pƙed 2 lety

    I find that it's a great addition for people who are new to CSS, since it makes things more organized, and it makes it easy to deal with problems quickly. Though having said that, I still think that not using it is better if you have the time to write organized and structured CSS, since less lines of code would be needed, and layering could be done through splitting up your stylesheet into multiple ones.
    Thank you for the video, I found it very informative! âœŒđŸŒ

  • @andyx4506
    @andyx4506 Pƙed 2 lety +1

    This is amazing! By the way, do you have any ideas on making content have a border, but also have rounded corners?

    • @DTunezOfficial
      @DTunezOfficial Pƙed 2 lety

      I assume you cant just make a border radius with a border?

    • @andyx4506
      @andyx4506 Pƙed 2 lety

      @@DTunezOfficial Yep. I used border and then border-image gradient, not sure if that disables the border-radius.

    • @hugoanderkivi
      @hugoanderkivi Pƙed 2 lety

      I think box-shadow could enable that.

    • @andyx4506
      @andyx4506 Pƙed 2 lety

      @@hugoanderkivi thanks, though i alreay figured it out some time ago

  • @RevivingCulture
    @RevivingCulture Pƙed 2 lety

    Thanks Kevin it a gr8 video. I have a question this will works for all browsers? Chrome,safari,firefox and IE ?plz help on this

    • @KevinPowell
      @KevinPowell  Pƙed 2 lety

      Right now, no, but all of them are actively working on it, so in the not too distant future it will work in all of them, yes

    • @Killyspudful
      @Killyspudful Pƙed 2 lety

      @@KevinPowell Er, well, it ain't never going to work in IE...

  • @katetretyakova4901
    @katetretyakova4901 Pƙed 2 lety

    so lit t-shirt, man!)))

  • @jemandanders3173
    @jemandanders3173 Pƙed 2 lety +2

    Tolkien ipsum, very cool.
    Edit: It appears to be called LOTRem ipsum, if anyone else is interested ;)

  • @FredUA
    @FredUA Pƙed 2 lety

    Could you make an example of this issue using BEM methodology? Maybe this is not a problem with specificity but bad naming and using tag/class/id mash?

    • @FredUA
      @FredUA Pƙed 2 lety

      Awful example.Why do you use class .button for link? Why do you use tag selector? Do anybody write CSS like this? Do you realy need custom properties if you are using nesting selectors and mix it with id/class/attr selectors đŸ€š

  • @dasten123
    @dasten123 Pƙed 2 lety

    interesting feature

  • @germantoenglish898
    @germantoenglish898 Pƙed 2 lety

    Your placeholder text is much more interesting and entertaining than Lorem Ipsum...did you use a gibberish random text generator, if so, where can I find it? Sorry for the off-topic question and thanks for the great tutorial.

  • @ErickXavier
    @ErickXavier Pƙed 2 lety

    Is it possible to '@import' css within a layer?

    • @KevinPowell
      @KevinPowell  Pƙed 2 lety +1

      I tried and it didn't work in Chrome Canary, but I think it's supposed to, so I'm not sure where it'll end up.

  • @fredhair
    @fredhair Pƙed 2 lety +1

    Seems to me like there'd be a lot of issues here especially for library / framework developers. What would be more useful imo is proper namespacing & scoping.. yeah usable scoping (:scope).. and not just for CSS variables. At least we're getting nesting soon but I really am looking forward to the time when I can use is(), has(), where() without worrying about wrapping everything in @supports and pollyfilling. Parent selector is my dream CSS feature and potentially kinda possible with has() but we shall see.

    • @KevinPowell
      @KevinPowell  Pƙed 2 lety +2

      has() will be a gamechanger for sure!
      And scoping is on the way as well, though that's going to be a bit longer still.
      I also think this will make libraries and frameworks easier to work with, rather than harder. Could be useful within site builders with custom CSS with WP themes as well.
      We'll only really know once it's out in the wild though!

    • @fredhair
      @fredhair Pƙed 2 lety

      @@KevinPowell Yeah I think if the scoping was there it would help, I just think there's going to be issues with ordering and redeclaration on the user side. Seeing as you mentioned WP, the naming could be solved with just prefixing everything with something very unique (like most do in WP themes / plugins).
      I suppose the more I think about it, the more it makes sense it just doesn't seem like an ideal solution to me but for sure it will be helpful for some specificity issues.
      Anyway, thanks for the video Kevin, I was unaware of layers and you covered them well 👍

    • @MiriamCodes
      @MiriamCodes Pƙed 2 lety +1

      @@fredhair Lucky for you, `@scope` is the primary feature planned for the next update to the Cascade. :) Here's the current draft: www.w3.org/TR/css-cascade-6/

    • @fredhair
      @fredhair Pƙed 2 lety +2

      @@MiriamCodes Yeah it's progress but it will be a long while before it's usable in production. That's still at first draft stage and was only published just over a month ago but at least it's moving in the right direction!

  • @DeltaNrOne
    @DeltaNrOne Pƙed 2 lety

    This sounds like a pain and a blessing at the same time.

  • @R4GEFORME
    @R4GEFORME Pƙed 2 lety

    What about .nav-list a:not(.button)?

  • @ssygon2
    @ssygon2 Pƙed 2 lety

    Nothing worse when trying to add/update a new framework/module and seeing breaking changes in layouts/font styles/etc.
    I've always used an id at the top level in my templates to override:
    my button
    #main {
    .section {
    .button {
    }
    }
    }
    Nice once all browsers adopt this cascade layer specificity can set the styling order.

  • @simpleman1218
    @simpleman1218 Pƙed 2 lety

    After third SPECIFICITY word i was like Who are you and where is real Kevin😼 And then you told about the wordđŸ€Ł

  • @finalsecretofchrono1339
    @finalsecretofchrono1339 Pƙed 2 lety

    Kevin, what do you think of Metaverse buzz? Is HTML/CSS going to be history in 10 years?

  • @i007c
    @i007c Pƙed 2 lety

    cool

  • @FrancisPerronLongpre
    @FrancisPerronLongpre Pƙed 2 lety

    16:29 There's 292 !important in the latest Bootstrap version :(

  • @sidthetech7623
    @sidthetech7623 Pƙed 2 lety

    Kevin, I will be looking for a front-end designer very soon for our animation festival that will feature our selection online.
    Perhaps someone in your comments is proficient enough and reasonably priced.?!

  • @2Fast4Mellow
    @2Fast4Mellow Pƙed 2 lety

    For a second I was afraid that Mozille would introduce (document) layers again... It's the stuff nightmares are made off!

  • @MakoSDV
    @MakoSDV Pƙed 2 lety +2

    Is there ever a valid reason to use !important? It always seems to be used as a hack because the dev didn't want to do it the right way.

  • @levyroth
    @levyroth Pƙed 2 lety

    html * {
    font-size: 16px;
    line-height: 1.6;
    }

  • @oliver139
    @oliver139 Pƙed 2 lety

    I think this technique will either make thing simplier, or mess up the CSS LOL
    and btw !important is still evil..damn boostrap

  • @user-cp4sk7rb3v
    @user-cp4sk7rb3v Pƙed rokem

    is there a way to set a level of importance? such !important Lvl 9

  • @sharkinahat
    @sharkinahat Pƙed 2 lety +1

    I try to go for the everything-is-a-class thing and get super annoyed when the fix for my problems is to copy-paste to a different place. Argh!

  • @ChanyArpin
    @ChanyArpin Pƙed 2 lety

    In Chrome, the User Agent throws an !important on a input’s writing-mode.

  • @user-ms8ei9le7x
    @user-ms8ei9le7x Pƙed 2 lety

    @layer === position : z - index :)))))))
    Kevin thanks 🎓

  • @Strasbourgeois
    @Strasbourgeois Pƙed rokem

    The reason unlayered rules have higher specificity is because they get put inside an anonymous layer that is below all other layers

  • @tanaygupta6905
    @tanaygupta6905 Pƙed 2 lety

    Me after watching this đŸ‘€đŸ˜łđŸ€©đŸ˜â€ïž

  • @deatho0ne587
    @deatho0ne587 Pƙed 2 lety

    Old code with tons of important (that aren't important at all). Think this is just going to make it harder to maintain.

    • @KevinPowell
      @KevinPowell  Pƙed 2 lety +1

      If there are a lot of !importants floating around in an old code base, I'd probably wouldn't look at updating it with layers cause yeah, it would be a bit of a nightmare.

    • @deatho0ne587
      @deatho0ne587 Pƙed 2 lety

      I kind of wish the important would vanish from modern browsers, but this would most likely break most corporate code looks.

  • @welling1
    @welling1 Pƙed 2 lety +1

    Doesn't this have the potential to become a complete bloated mess if not carefully handled, especially if working on a large team? This seems like it would be very easy to abuse this power if in a hurry under a tight deadline (or just being lazy).

  • @theomegamale5335
    @theomegamale5335 Pƙed 2 lety

    So, because people can't grasp the cascade, they decided to add another layer to the onion? I find specificity as is to be elegant if done right, never really thought it to be difficult. I guess maybe it will help people....I just don't understand the need. Is it just a QOL kind of thing?

    • @Killyspudful
      @Killyspudful Pƙed 2 lety

      As was mentioned in the video - if you've ever had to 'work' with Bootstrap and had designs that differed from its defaults, you'll soon find that you need to write more and more overriding selectors because of how opinionated it is. This will allow you to isolate imported third-party styles without resorting to .class.class.class and other ugly specificity boosting.

  • @owolabioluwapelumi5070
    @owolabioluwapelumi5070 Pƙed 2 lety

    First yo get here

  • @DanielRios549
    @DanielRios549 Pƙed 6 měsĂ­ci

    I didn't like that selectors outside a layer have higher specificity, it should be the opposite

  • @user-ft5gd8es3p
    @user-ft5gd8es3p Pƙed 2 lety

    If the js can access @layer then it will be totally gane changer i think 🙄

  • @whynot9963
    @whynot9963 Pƙed 2 lety

    I mean, all of this is great, but using SCSS with nesting (or anything else that offers similar features, like scoped classes, etc) is much better than using plain CSS. My question is, why would anyone bother with vanilla CSS in this day and age?

    • @ritwik5774
      @ritwik5774 Pƙed 2 lety

      It's simple. Those tools can use layers under the hood. If they can use native features like layers instead of having to do complex processing, build steps get faster.
      Also, not everyone uses CSS tools. The ideal should always be focusing on getting vanilla CSS as usable as possible, so that we can one day use it directly instead of wrappers.
      CSS can also do things that stuff like sass cannot, like custom variables for instance. And now with Houdini, it's going to get a whole lot more powerful.

    • @KevinPowell
      @KevinPowell  Pƙed 2 lety +1

      Well, nesting can cause problems, as I looked at in this video when my .button wouldn't work in the nav, because it had a descendant selector. So, in this case at least, nesting doesn't solve that. I do think this will play SUPER well with Sass, tbh.

    • @whynot9963
      @whynot9963 Pƙed 2 lety

      @Kevin Powell but it's much easier to keep stuff organized so that this doesn't happen, you just make a button component, and off you go, no need for complex selectors. But I guess more features and options never hurt anybody :)

    • @whynot9963
      @whynot9963 Pƙed 2 lety

      @Ritwik but with SCSS you can avoid this complex selector completely as you just make a button component instead of specifically selecting button in certain spots. But as I mentioned in previous comments, the more features the merrier :)

    • @ritwik5774
      @ritwik5774 Pƙed 2 lety +1

      ​@@whynot9963 like I said, not everyone uses scss, and scss has to transpile to css under the hood.
      Kevin already mentioned a very real world use case for layers with the framework overrides.
      Pretty sure the number of people using scss is a miniscule minority compared to the people on frameworks like bootstrap.

  • @mma93067
    @mma93067 Pƙed 11 měsĂ­ci

    Wait. Does this mean i can finally throw away all those god awful CSS-in-JS libs?!

  • @Jan.Feldmann
    @Jan.Feldmann Pƙed 2 lety +1

    Say "specificity" 10 times fast!

  • @pushqrdx
    @pushqrdx Pƙed 2 lety

    To me it looks like a and !important block lol

  • @xanpk2074
    @xanpk2074 Pƙed 2 lety

    No more !important; ? :/

  • @mahdienejadhoseini6856
    @mahdienejadhoseini6856 Pƙed 2 lety

    Your videos are viewing from around the world.
    You speak absolutly fast.
    Could you say a little slower? :((

  • @MatveyAndreyev
    @MatveyAndreyev Pƙed 2 lety

    CSS specificity is one of the easiest things on Earth.