Pen Settings



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


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


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.


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.


                <h1 id="title" class="in-theory">Custom (<em>Property</em>) Cascades</h1>

<h2>"Custom Origins"</h2>
  I often use short variable stacks
  to create a property-specific "cascade"
  based on different "origins" of a style:
  user, theme, component patterns, states, etc.
  For these buttons,
  I used state > 'btn' > type > theme, 
  where 'btn' allows explicit one-off settings.

<button>theme default</button>
<button disabled>disabled theme</button> <br>
<button data-type="warning">warning type</button>
<button disabled data-type="warning">disabled warning</button> <br>
<button style="--btn-bg: green">green btn</button>
<button disabled style="--btn-bg: green">disabled green</button>

  No matter where these variables are defined,
  they will always stack in the same order.
  Since btn "state" is given top priority,
  it will always override theme and type --
  even "btn" colors that are set with the inline style attribute.
  Specificity only matters when
  <em>the same variable</em>
  is being set in two places.

  I noticed recently that <em>in theory</em>
  this creates a rough approximation
  of "<a href="">Custom Origins</a>" --
  a proposed spec that I'm working on
  with the W3C.

  The cascade works by accepting multiple versions
  of a property declaration,
  and then choosing between them
  based on a set of filtering criteria:

    <strong>origin</strong> and <strong>importance</strong></li>
      <li>classes & attributes</li>
  <li><strong>source order</strong></li>

  Specificity is only considered
  when there are multiple values
  from the highest-weight origin --
  and source-order is only considered
  if origins, importance, ids, attributes, and types
  all fail to filter out a winner.

  The idea of a "custom origin"
  is that it provides a layer between
  existing origin/importance rules,
  and specificity.

  <li>origin and importance</li>
    <strong>custom origins???</strong>
      <li>custom origin 1</li>
      <li>custom origin 2</li>
      <li>classes & attributes</li>
  <li>source order</li>

  Given the right setup,
  custom property stacks can achieve the same goal --
  and even be nested for more complex layering within origins.
  While I use small, selective "stacks"
  (like the button example in production,
  it can be taken much farther.
  Let's do that!

  Here I've established four variable "origins"
  on the `color` property 
  each with four sub-origins 
  These variables are applied to every element
  with the universal selctor.

  We do that to avoid inheritance of the 
  <em>origin variables</em> themselves,
  and only inherit 
  the <code>color</code> property results.
  That matches the normal cascade,
  where specificiy overrides inheritance.
  can be re-specified on
  any element from any origin --
  without needing to override
  higher-origin settings on an ancestor.

  You can see this applied to the
  headline of this page.
  The <code>title</code> ID has a color of 
  <code>maroon</code> set on the lowest
  <code>--color-1-1</code> color origin.
  The <code>h1</code> tag is lower specificity,
  but applies <code>deeppink</code>
  to a higher origin (<code>--color-2-1</code>) --
  which takes precedence.

  That's not to say specificity is ignored.
  As with the normal cascade,
  we move to the next layer in case of a tie.
  The <code>em</code> tag has a color of <code>red</code>
  set on the <code>h1 em { --color-1-1 }</code>.
  It also has a color of <code>rebeccapurple</code>
  set on the same origin,
  but at a higher specificity:
  <code>#title em { --color-1-1 }</code>.

  This example is extreme,
  and I don't recommend taking it this far in production --
  but I think it's a decent mental model
  for understanding the feature,
  and how you might use it in more targeted
  real-life situations.

<h2>"Custom Scopes"</h2>

  "Scopes" get a lot of attention these days,
  especially in the world of JS frameworks.
  There's an existing
  <a href="">specification</a>,
  though implementation is not clear.
  The primary access point was meant to be
  "scoped style sheets" --
  but those have been abandoned.

  Like "origins", 
  a "scope" should be able to override specificity --
  but the goal is based more on "proximity"
  rather than layers of "intent".
  A button component has narrow scope,
  inside a broader form component,
  inside a layout.

  Specificity is <em>meant</em> to handle something like this,
  where the more "specific" selector
  overrides the less specific.
  That's a useful and related concept 
  but weights "uniqueness" over "proximity".
  The only CSS tool that gives proximity weight
  is <em>inheritance</em>.

  custom properties can help us 
  aproximate the idea.
  This time we only need a single variable,
  with inheritance intact.
  We're going to take advantage
  of the fact that custom properties:

    inherit like any other property,
    giving us <strong>proximity</strong>
    don't need to render 
    on the element that defines them

  I'll use the same
  <code>var(--btn)</code> and <code>var(--btn-bg)</code>
  That I defined earlier
  for a button's
  <code>color</code> and <code>background</code>
  We can ignore the rest of the "origin"
  stack here.

  Now, if we set those variables
  on any "component" with a button,
  we can see that the
  most "proximate" (nearest ancestor)
  component take precedence --
  no matter what selectors are used

<article id="article">
  <button>button in article</button>
    <p>nested form</p>
    <button>button in form</button>



                /* in-practice */
html {
  --btn-bg--theme: rebeccapurple;
  --btn--theme: white;

button {
  background: var(--btn-bg--state, var(--btn-bg, var(--btn-bg--type, var(--btn-bg--theme))));
  color: var(--btn--state, var(--btn, var(--btn--type, var(--btn--theme))));
  border: 1px solid currentcolor;
  padding: 0.5em 2em;

[disabled] {
  --btn-bg--state: gray;

[data-type='warning'] {
  --btn-bg--theme: maroon;

/* in-theory: 4*4 property "origins" for color */
/* some day…
@property --color-1-1 {
  syntax: "<color>";
  inherits: false;
} */

* { 
  --color-1-1: initial;
  --color-1-2: initial;
  --color-1-3: initial;
  --color-1-4: initial;
  --color-2-1: initial;
  --color-2-2: initial;
  --color-2-3: initial;
  --color-2-4: initial;
  --color-3-1: initial;
  --color-3-2: initial;
  --color-3-3: initial;
  --color-3-4: initial;
  --color-4-1: initial;
  --color-4-2: initial;
  --color-4-3: initial;
  --color-4-4: initial;
  --color-1: var(--color-1-4, var(--color-1-3, var(--color-1-2, var(--color-1-1))));
  --color-2: var(--color-2-4, var(--color-2-3, var(--color-2-2, var(--color-2-1, var(--color-1)))));
  --color-3: var(--color-3-4, var(--color-3-3, var(--color-3-2, var(--color-3-1, var(--color-2)))));
  --color-4: var(--color-4-4, var(--color-4-3, var(--color-4-2, var(--color-4-1, var(--color-3)))));
  color: var(--color-4, inherit);

/* Property "origin" takes precedence over specificity */
#title {
  --color-1-1: maroon;

h1 {
  --color-2-1: deeppink;

/* specificity still applies when origins match */
#title em {
  --color-1-1: rebeccapurple;

h1 em {
  --color-1-1: red;

/* Scopes */
form {
  border: 1px solid;
  margin: 0.5em;
  padding: 0.5em;

#article {
  --btn-bg: maroon;

form {
  --btn-bg: green;

/* basic page layout… */
body {
  max-width: 34em;
  padding: 1em;
  margin: 0 auto;

code {
  background: #eef;
  display: inline-block;
  padding: 0 0.3em;
  border-radius: 0.1em;