Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="intro">
<h1>The Entire Cascade</h1>

<p>
  When multiple styles apply to 
  the same property (e.g. <code>color</code>) 
  on the same element (e.g. one specific <code>button</code>),
  the cascade is used to determine
  which style has priority.
</p>
<p>
  While we often refer to that 
  as <em>specificity</em> in conversation,
  that term actually refers to one small step of the process.
  The overall priority of a declaration
  is called its <strong>Cascade Precedence</strong>.
  But those two are directly related –
  one being a subset of the other.
  <em>Selectors with higher specificity
  also have a higher cascade precedence.</em>
</p>
<p>
  For every set of styles declared
  on the same property of the same element,
  we start at the top of the cascade process.
  At each step, 
  we group the styles into categories.
  The top category with styles in it 
  is the only category that moves on to the next step. 
  Any other styles can be eliminated from consideration.
</p>
<p>
  In the first step,
  we separate out normal styles
  from ❗️important styles –
  and for most of the way down,
  we only need to look at one side
  or the other.
  It's like a single-elimination bracket,
  <strong>only one style can win it all!</strong>
</p>  
</div>

<ol class='category-list'>
  <li data-cat='origin'>
    <h2>Origin & Importance</h2>
    <p>Who applied the style, and how important is it?</p>
    <ol layer-list='combined'>
      <li>CSS Transitions</li>
      
      <li data-side='important'>❗️User Agent (Browser)</li>
      <li data-side='important'>❗️User (Preferences)</li>
      <li data-side='important'>❗️Author (Site)</li>

      <li>CSS Animations</li>
      
      <li data-side='normal'>Author (Site)</li>
      <li data-side='normal'>User (Preferences)</li>
      <li data-side='normal'>User Agent (Browser)</li>
    </ol>
  </li>
  
  <li data-cat='context'>
    <h2>(Shadow) Context</h2>
    <p>Is it coming from the main document, or embedded shadow DOM?</p>
    <strong data-side='important'>important</strong>
    <ol layer-list='important'>
      <li>❗️Shadow DOM</li>
      <li>❗️Host (Light) DOM</li>
    </ol>    
    <strong data-side='normal'>normal</strong>
    <ol layer-list='normal'>
      <li>Host (Light) DOM</li>
      <li>Shadow DOM</li>
    </ol>
  </li>

  <li data-cat='style'>
    <h2>Element-Attached Styles</h2>
    <p>
      Separately for normal and important styles, 
      inline styles win over selectors.
    </p>
    <strong data-side='important'>important</strong>
    <ol layer-list='important'>
      <li>❗️Inline Styles</li>
      <li>❗️Selectors</li>
    </ol>    
    <strong data-side='normal'>normal</strong>
    <ol layer-list='normal'>
      <li>Inline Styles</li>
      <li>Selectors</li>
    </ol>
  </li>

  <li data-cat='layers'>
    <h2>Cascade Layers (new!)</h2>
    <p>
      Layers stack in the order they are declared,
      with the first layer having lowest priority, 
      building to the last layer, 
      and then un-layered styles.
      The order is reversed for important styles.
    </p>
    <strong data-side='important'>important</strong>
    <ol layer-list='important'>
      <li>❗️<i>First-Layer</i></li>
      <li>❗️<i>…</i></li>
      <li>❗️<i>Last-Layer</i></li>
      <li>❗️<i>Un-Layered Styles</i></li>
    </ol>    
    <strong data-side='normal'>normal</strong>
    <ol layer-list='normal'>
      <li><i>Un-Layered Styles</i></li>
      <li><i>Last-Layer</i></li>
      <li><i>…</i></li>
      <li><i>First-Layer</i></li>
    </ol>
  </li>

  <li data-cat='specificity'>
    <h2>Selector Specificity</h2>
    <p>
      The selector with the most IDs wins.
      If there is a tie for the most IDs,
      repeat the process for each selector type.
    </p>
    <ol layer-list='combined'>
      <li><code>#id</code></li>
      <li>
        <code>.class</code> |
        <code>:pseudo-class</code> |
        <code>[attribute]</code>
      </li>
      <li>
        <code>element</code> |
        <code>::pseudo-element</code>
      </li>
      <li>
        <code>*</code> |
        <code>:where()</code>
      </li>
    </ol>
  </li>
  
  <li data-cat='order'>
    <h2>Order of Appearance</h2>
    <p>Last style wins!</p>
  </li>
</ol>

<div class="outro">
  <p>
  For properties without any valid style
  at the end of the cascade,
  we have to move beyond the cascade.
  <strong>Inheritance</strong> (looking at parent values) 
  and then <strong>Defaulting</strong> (using the initial value)
  fill in any remaining gaps.
  </p>
</div>
              
            
!

CSS

              
                body {
  padding: 1em;
  max-width: 120ch;
  margin: 0 auto;
}

ol, h2 { 
  margin: 0;
  padding: 0;
}

li {
  list-style: none;
}

.category-list {
  display: grid;
  grid-gap: 0.5em;
  grid-template-columns: 
    repeat(5, 1fr)
    auto
    repeat(5, 1fr)
  ;
  justify-content: center;
  margin: 1em 0;
}

[data-cat] { 
  --h: calc(180deg + 36deg * var(--i, 0));
  --col-abs: calc(var(--i, 0) + 1);
  --col-neg: calc(var(--col-abs, 1) * -1);
  --pad: min(5vmin, 4em);
  background: hsl(var(--h, 0deg) 50% 90%);
  clip-path: polygon(0 0, 100% 0, calc(100% - var(--pad)) 100%, var(--pad) 100%);
  display: grid;
  grid-auto-flow: dense;
  grid-template-columns: repeat(2, 1fr);
  grid-column: var(--col-abs, 1) / var(--col-neg, -1);
  grid-gap: 0.25em 1em;
  padding: 1em calc(var(--pad) + 0.5em);
}

[data-cat] > * {
  grid-column: span 2;
}

[data-cat='context'] {
  --i: 1;
}

[data-cat='style'] {
  --i: 2;
}

[data-cat='layers'] {
  --i: 3;
}

[data-cat='specificity'] {
  --i: 4;
  --color: gray;
}

[data-cat='order'] {
  --i: 5;
}

h2 {
  text-align: center;
}

h2 + p {
  margin-top: 0;
}

h2 + p:last-child {
  margin-bottom: 0;
}

li li {
  --li-a: calc(1 - var(--li, 0) * 0.125);
  background: hsl(var(--h, 0deg) 50% 80% / var(--li-a, 0.5));
}

li li:nth-child(2) { --li: 1; }
li li:nth-child(3) { --li: 2; }
li li:nth-child(4) { --li: 3; }
li li:nth-child(5) { --li: 4; }
li li:nth-child(6) { --li: 5; }
li li:nth-child(7) { --li: 6; }
li li:nth-child(8) { --li: 7; }

.sr-only {
  border: 0;
  clip: rect(1px, 1px, 1px, 1px);
  -webkit-clip-path: inset(50%);
  clip-path: inset(50%);
  height: 1px;
  overflow: hidden;
  margin: -1px;
  padding: 0;
  position: absolute;
  width: 1px;
  white-space: nowrap;
}

[layer-list] {
  display: grid;
  grid-gap: inherit;
}

[layer-list='normal'] {
  grid-column: 1;
}

[layer-list='important'] {
  grid-column: 2;
}

[layer-list='combined'] {
  display: grid;
  grid: inherit;
  grid-auto-flow: row;
}  

[layer-list='combined'] li {
  grid-column: var(--side, 1 / -1);
}

[data-side] {
  grid-column: var(--side, 1);
}

[data-side='normal'] {
  --side: 1;
}

[data-side='important'] {
  --side: 2;
}

[layer-list] li {
  padding: 0.25em 0.5em;
  color: var(--color, unset);
}

[layer-list] code {
  display: inline-block;
  color: initial;
}

.intro, .outro {
  max-width: 75ch;
  margin: 0 auto;
}
              
            
!

JS

              
                
              
            
!
999px

Console