liveBook · Manning

Cascade

As explained in Vanilla Web

The CSS cascade is the rule system that resolves competing style declarations according to origin, importance, specificity, and source order.

The CSS cascade resolves competing style declarations by origin, importance, specificity, and source order. For web components, a practical question comes first: which elements can a rule reach? A custom element participates in the cascade like any other element, so its tag name can be used as a selector for the host. section 7.7

Without a shadow root, a component uses the page DOM. Page rules can style elements created inside it, and styles declared inside it can affect the rest of the page. Those rules are not confined to the component merely because its markup came from a custom element. section 7.8.2

Listing 7.3 A web component using the page DOM
<style>
button { #1
  background-color: black; #1
  color: white; #1
} #1
</style>

<script>
  class MyComponent extends HTMLElement {
    connectedCallback() {
      this.innerHTML = `
        <style>
            h1 { #2
                border: 1px solid gray; #2
            } #2
        </style>
        <button>Click me</button> #3
      `;
    }
  }
  customElements.define('my-component', MyComponent);
</script>

<h1>CSS with components</h1> #4
<my-component></my-component> #4

The page’s button rule styles the component’s button, while the component’s h1 rule styles the page heading. Neither rule is isolated because the component uses the page DOM. section 7.8.2

Host styling is a separate, useful choice: for example, a rule can change a custom element’s default inline display. The :not(:defined) selector can also hide or label a custom element while its definition has not yet been registered. section 7.7

How Vanilla Web covers it

How deep it goes
Demonstrates host styling and page-DOM style reach, but does not work through cascade-priority calculations.
Examples
Contrasts a page button rule reaching a component button with a component h1 rule reaching a page heading.
What it assumes
Assumes basic HTML and CSS selectors while introducing how custom-element hosts and page-DOM styles interact.
What you can do after
Diagnose/debug with it when unexpected styles reach into or out of a page-DOM custom element.
How it connects
Connects CSS selectors with custom elements, the page DOM, shadow DOM, and the :defined pseudo-class.

Also explained in

Compare how 6 books explain cascade →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Vanilla Web. The section links point to the parts of the book it draws on.