Cascade Layers: First Contact

Earlier this week I learned about CSS Cascade Layers and now I’m all hyped up because I really like the concept. I’m eager to find out how we can use them to improve and rethink the architecture of our styles.I will not…


This content originally appeared on Manuel Matuzović - Blog and was authored by Manuel Matuzović

Earlier this week I learned about CSS Cascade Layers and now I’m all hyped up because I really like the concept. I’m eager to find out how we can use them to improve and rethink the architecture of our styles.

I will not explain how CSS Cascade Layers work because Bramus and Stephanie have already done that and they did it much better than I ever could. I just want to get my feet wet and share my first impressions. If you’re new to the topic, read their articles first.

While I’m writing this blog post, I’m looking at a large site I’ve been working on and I’m trying to find scenarios in which cascade layers could’ve been useful.

Overly specific base styles

If you have a layer for base styles and another for your components, you don’t have to worry about selectors with high specificity in the base layer anymore.

@layer base, components;

@layer base {
  ul[class] {
    margin: 0;
    padding: 0;
    list-style-type: none;
  }

  input[type="text"] {
    border: 1px solid #000;
  }
}

@layer components {
  .nav {
    margin-left: 40px;
  }

  .error {
    border: 2px dashed #F00;
  }
}

Rules in .nav and .error overwrite rules in ul[class] and input[type="text"] even though the specificity of the latter is higher.

Try it yourself:

Hint: You have to enable support for Cascade Layers in your browser, they’re still behind a flag.

Third party styles

One of the third-party tools we’re using uses highly specific selectors. In some places we’re overwriting rules and we either have to use selectors with the same specificity 🤢 or !important 🤮.

I imagine having a dedicated layer for third party styles could make sense. Within this layer, you nest other layers, one or more for the styles you import and another one for your custom overwrites.

@layer base, third-party, components;

@layer base {
  /* Base styles */
}

@layer third-party {
  @layer slider {
    .slider.slider-horizontal > .slider-item {
      background: #fefefe;
      padding: 10px;
      border: 1px solid #000;
    }
  }
  /* 
    In reality 3rd party styles would probabaly live in separate files
    @import url(slider.css) layer(third-party); 
  */

  @layer overwrites {
    .slider-item {
      border-color: #F00;
    }
  }
}

@layer components {
  /* Component styles */
}

Utility classes

Having a dedicated layer for utility classes feels way more intuitive than having to apply !important to each rule in a utility class.

<ul id="list" class="u-df">
  <li>One</li>
  <li>Two</li>
  <li>Three</li>
</ul>
@layer base, components, utility;

@layer base {
  /* Base styles */
}

@layer components {
  #list {
    display: block;
    list-style-type: none;
    padding: 0;
  }
}


@layer utility {
  .u-df {
    display: flex;
  }
}

“Utility classes with Cascade Layers” on CodePen

Conclusion

This all makes sense to me in theory, but I guess we’ll see how useful layers will be in reality once support gets better and more people begin experimenting. Either way, I’m really stoked about the progress happening with layers and new selectors like :is() and :where(). Super useful stuff that will make the lives of those specialized in CSS easier. I don’t believe that it will make everyone’s lives easier because with features like these CSS becomes more powerful, but this also increases its complexity. Mastering CSS is already pretty hard, but it will take even more in the future to become really good at writing CSS.

My blog doesn't support comments yet, but you can reply via blog@matuzo.at.


This content originally appeared on Manuel Matuzović - Blog and was authored by Manuel Matuzović


Print Share Comment Cite Upload Translate Updates
APA

Manuel Matuzović | Sciencx (2022-01-28T00:00:00+00:00) Cascade Layers: First Contact. Retrieved from https://www.scien.cx/2022/01/28/cascade-layers-first-contact-2/

MLA
" » Cascade Layers: First Contact." Manuel Matuzović | Sciencx - Friday January 28, 2022, https://www.scien.cx/2022/01/28/cascade-layers-first-contact-2/
HARVARD
Manuel Matuzović | Sciencx Friday January 28, 2022 » Cascade Layers: First Contact., viewed ,<https://www.scien.cx/2022/01/28/cascade-layers-first-contact-2/>
VANCOUVER
Manuel Matuzović | Sciencx - » Cascade Layers: First Contact. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/01/28/cascade-layers-first-contact-2/
CHICAGO
" » Cascade Layers: First Contact." Manuel Matuzović | Sciencx - Accessed . https://www.scien.cx/2022/01/28/cascade-layers-first-contact-2/
IEEE
" » Cascade Layers: First Contact." Manuel Matuzović | Sciencx [Online]. Available: https://www.scien.cx/2022/01/28/cascade-layers-first-contact-2/. [Accessed: ]
rf:citation
» Cascade Layers: First Contact | Manuel Matuzović | Sciencx | https://www.scien.cx/2022/01/28/cascade-layers-first-contact-2/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.