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="body-wrap">
<h1 class="title">This heading changes colour as you vertically scroll the page</h1>
<section class="box box--1">
<div class="box__bd">
<h2><abbr title="Too long, didn't read">TL;DR</abbr></h2>
<p>This is a pure-CSS solution for having a heading change colour as it scrolls past backgrounds of different colours. It <strong>does not</strong> require multiple copies of the heading in different colours.<p>
<ol>
<li>Apply <code>position: sticky</code> to the heading in question. Remember to make sure its parent also contains all the backgrounds you want it to scroll past.</li>
<li>Each background colour that the heading scrolls by should be in a wrapper. This demo uses <code><section class="box"></code>. Remember to add appropriate padding/spacing so that the heading does not block any content in your wrapper.</li>
<li>Use the <code>:after</code> selector to add a pseudo-element for each wrapper that spans the wrapper height and is at least as wide as the heading. Also give this pseudo-element an appropriate value for <code>backdrop-filter</code> that corresponds to the colour change you want. This demo simply inverts heading colour.</li>
<li>Final adjustments: add a <code>z-index</code> to both the heading and the pseudo-element so that the heading is <em>between</em> the pseudo-element and the wrapper.</li>
</ol>
</div>
</section>
<section class="box box--2 no-colour-change">
<div class="box__bd">
<h2>A few additional notes</h2>
<ol>
<li>The pseudo-element will also change the background colour of the wrapper. This may or may not be desired. One work-around is to make the pseudo-element span the entire width of the wrapper and then adjust your wrapper background colour accordingly.</li>
<li>If the width of the pseudo-element increases after page load (e.g. by making the browser wider), <code>backdrop-filter</code> <em>does not</em> apply in the newly created space.</li>
<li>Any text under the pseudo-element is not selectable.</li>
</ol>
</div>
</section>
<section class="box box--3">
<div class="box__bd">
<p>We spend so much of our life looking - but never seeing. Let's do that again. I was blessed with a very steady hand; and it comes in very handy when you're doing these little delicate things. Everyone is going to see things differently - and that's the way it should be. Just think about these things in your mind - then bring them into your world. You're meant to have fun in life.</p>
<p>In life you need colors. That easy. Automatically, all of these beautiful, beautiful things will happen.</p>
<p>It's all a game of angles. Maybe we got a few little happy bushes here, just covered with snow. Maybe there was an old trapper that lived out here and maybe one day he went to check his beaver traps, and maybe he fell into the river and drowned.</p>
<p>Nice little clouds playing around in the sky. Now then, let's play. Let's have a little bit of fun today.</p>
</div>
</section>
<!-- Empty lines to allow heading to scroll further down the page. -->
<p><br/></p>
<p><br/></p>
<p><br/></p>
<p><br/></p>
<p><br/></p>
<p><br/></p>
<p><br/></p>
<p><br/></p>
<p><br/></p>
<p><br/></p>
<p><br/></p>
<p><br/></p>
<p><br/></p>
<p><br/></p>
<p><br/></p>
<p><br/></p>
<p><br/></p>
<p><br/></p>
<p><br/></p>
<p><br/></p>
<p><br/></p>
<p><br/></p>
<p><br/></p>
<p><br/></p>
<p><br/></p>
<p><br/></p>
<p><br/></p>
<p><br/></p>
<p><br/></p>
<p><br/></p>
<p><br/></p>
</div>
body {
font-family: sans-serif;
}
.title {
position: sticky;
inset-block-start: 3vh;
inset-inline-start: 3vw;
inline-size: 40vw;
z-index: 5;
font-size: 300%;
}
.box {
position: relative;
padding-inline-start: 46vw;
min-block-size: 50vh;
font-size: 120%;
}
.box--1 {
background-color: #fff;
}
.box--2 {
background-color: #a0a0a0;
}
.box--3 {
background-color: #dfdfbf;
}
.box:after {
content: '';
position: absolute;
inset-block-start: 0;
inset-inline-start: 0;
block-size: 100%;
inline-size: 43vw; /* If this is set to span 100% of the parent, making the browser wider after page load does not stretch this element. Also, anything under this element will not be selectable. */
z-index: 10;
backdrop-filter: invert(100%);
}
.no-colour-change:after {
backdrop-filter: none;
}
Also see: Tab Triggers