Chapter 9. Shadow CSS

 

This chapter covers

  • Keeping external style out of your Web Components
  • The Shadow DOM for CSS encapsulation
  • Shadow DOM CSS selectors
  • Rediscovering the ID attribute for Web Components

Let’s continue on with our Shadow DOM exploration! In the last chapter, we zeroed in on a really nice aspect of the Shadow DOM. As awesome as DOM encapsulation is, the CSS aspect of the Shadow DOM is even better! Despite coming up with clever ways to mitigate style creep in our web development work over the years, it has always been a problem.

9.1. Style creep

Style creep can sometimes be a bit of a headache in web development work. To sum up, it’s when CSS rules come in and affect elements you didn’t intend to affect. You may be working to style an element in one place, but some style rules you’ve defined in your CSS for another element on your page are unintentionally picked up because the CSS selectors match. Although style creep isn’t limited to Web Components, let’s take a look at a Web Component example to see how it impacts us. Figure 9.1 shows a simple little Web Component that is essentially a stylized numerical stepper.

Figure 9.1. A stylized stepper component comprising two buttons and a text span

9.2. Style creep solved with the Shadow DOM

 
 

9.3. Shadow DOM workout plan

 
 
 

9.4. Adaptable components

 
 
 

9.5. Updating the slider component

 
 
 

Summary

 
sitemap

Unable to load book!

The book could not be loaded.

(try again in a couple of minutes)

manning.com homepage
test yourself with a liveTest