HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. So you don't have access to higher-up elements like the <html>
tag. If you want to add classes there that can affect the whole document, this is the place to do it.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. If you need things in the <head>
of the document, put that code here.
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
CSS preprocessors help make authoring CSS easier. All of them offer things like variables and mixins to provide convenient abstractions.
It's a common practice to apply CSS to a page that styles elements such that they are consistent across all browsers. We offer two of the most popular choices: normalize.css and a reset. Or, choose Neither and nothing will be applied.
To get the best cross-browser support, it is a common practice to apply vendor prefixes to CSS properties and values that require them to work. For instance -webkit-
or -moz-
.
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
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.
You can apply CSS to your Pen from any stylesheet on the web. Just put a URL to it here and we'll apply it, in the order you have them, before the CSS in the Pen itself.
You can also link to another Pen here (use the .css
URL Extension) and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
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.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
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.
Using packages here is powered by esm.sh, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ESM usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
<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>
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;
}
Also see: Tab Triggers