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="wrapper">
<div class="sky"></div>
<div class="earth">
<div id="red-queen_and_alice">
<img id="red-queen_and_alice_sprite" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/641/sprite_running-alice-queen_small.png" srcset="https://s3-us-west-2.amazonaws.com/s.cdpn.io/641/sprite_running-alice-queen.png 2x" alt="Alice and the Red Queen running to stay in place.">
</div>
</div>
<div class="scenery" id="foreground1">
<img id="palm3" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/641/palm3_small.png" srcset="https://s3-us-west-2.amazonaws.com/s.cdpn.io/641/palm3.png 2x" alt=" ">
</div>
<div class="scenery" id="foreground2">
<img id="bush" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/641/bush_small.png" srcset="https://s3-us-west-2.amazonaws.com/s.cdpn.io/641/bush.png 2x" alt=" ">
<img id="w_rook_upright" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/641/w_rook_upright_small.png" srcset="https://s3-us-west-2.amazonaws.com/s.cdpn.io/641/w_rook_upright.png 2x" alt=" ">
</div>
<div class="scenery" id="background1">
<img id="r_pawn_upright" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/641/r_pawn_upright_small.png" srcset="https://s3-us-west-2.amazonaws.com/s.cdpn.io/641/r_pawn_upright.png 2x" alt=" ">
<img id="w_rook" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/641/w_rook_small.png" srcset="https://s3-us-west-2.amazonaws.com/s.cdpn.io/641/w_rook.png 2x" alt=" ">
<img id="palm1" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/641/palm1_small.png" srcset="https://s3-us-west-2.amazonaws.com/s.cdpn.io/641/palm1.png 2x" alt=" ">
</div>
<div class="scenery" id="background2">
<img id="r_pawn" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/641/r_pawn_small.png" srcset="https://s3-us-west-2.amazonaws.com/s.cdpn.io/641/r_pawn.png 2x" alt=" ">
<img id="r_knight" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/641/r_knight_small.png" srcset="https://s3-us-west-2.amazonaws.com/s.cdpn.io/641/r_knight.png 2x" alt=" ">
<img id="palm2" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/641/palm2_small.png" srcset="https://s3-us-west-2.amazonaws.com/s.cdpn.io/641/palm2.png 2x" alt=" ">
</div>
</div>
* { user-select: none; }
img {
position: absolute;
top: 50%; left: 50%;
transform: translate(-50%, -50%);
}
.scenery {
width: 100%; height: 50%;
position: absolute;
bottom: 0; left: 0;
}
#foreground1, #foreground2 { z-index: 1 ;}
#foreground2, #background2 {
transform: translateX(100%);
}
#palm3 {
top: 0; left: 10%;
}
#w_rook_upright {
top: 30%; left: 75%;
}
#r_pawn {
top: 10%; left: 15%
}
#w_rook {
top: 10%; left: 80%
}
#r_pawn_upright {
top: 5%; left: 30%
}
#r_knight {
top: 0; left: 70%
}
#palm2 {
top: -15%; left: 90%
}
#palm1 {
top: -15%; left: 40%
}
#bush {
top: 55%; left: 20%
}
#red-queen_and_alice {
position: absolute;
top: 0; left: 50%;
transform: translate(-50%, -50%);
overflow: hidden;
width: 80%;
max-width: 450px; /*height: 783px / 2*/
z-index: 1;
}
#red-queen_and_alice:before {
content: " ";
display: block;
padding-top: 87%;
}
#red-queen_and_alice img {
position: absolute;
top: 0; left: 0;
width: 100%;
}
.sky, .earth {
position: absolute;
left: 0;
height: 50vh;
width: 100%;
}
.earth {
background: #eb125d url( "https://s3-us-west-2.amazonaws.com/s.cdpn.io/641/bg_earth.jpg") repeat-x 0 0 / 100% auto;
bottom: 0;
}
.sky {
background: #246e89 url("https://s3-us-west-2.amazonaws.com/s.cdpn.io/641/bg_sky.jpg") repeat-x 100% 100% / auto 100%;
top: 0;
}
html, body {
width: 100%; height: 100%;
}
.wrapper {
width: 100%; height: 100%;
overflow: hidden;
position: relative;
}
/* Background animations */
var sceneryFrames = [
{ transform: 'translateX(100%)' },
{ transform: 'translateX(-100%)' }
];
var sceneryTimingBackground = {
duration: 36000,
iterations: Infinity
};
var sceneryTimingForeground = {
duration: 12000,
iterations: Infinity
};
var background1 = document.getElementById('background1');
var background2 = document.getElementById('background2');
var background1Movement = background1.animate(
sceneryFrames, sceneryTimingBackground);
background1Movement.currentTime = background1Movement.effect.timing.duration / 2;
var background2Movement = background2.animate(
sceneryFrames, sceneryTimingBackground);
var foreground1 = document.getElementById('foreground1');
var foreground2 = document.getElementById('foreground2');
var foreground1Movement = foreground1.animate(
sceneryFrames, sceneryTimingForeground);
foreground1Movement.currentTime = foreground1Movement.effect.timing.duration / 2;
var foreground2Movement = foreground2.animate(
sceneryFrames, sceneryTimingForeground);
var spriteFrames = [
{ transform: 'translateY(0)' },
{ transform: 'translateY(-100%)' }
];
var redQueen_alice_sprite = document.getElementById('red-queen_and_alice_sprite');
var redQueen_alice = redQueen_alice_sprite.animate(
spriteFrames, {
easing: 'steps(7, end)',
direction: "reverse",
duration: 600,
playbackRate: 1,
iterations: Infinity
});
/* Alice tires so easily!
Every so many seconds, reduce their playback rate so they slow a little.
*/
var sceneries = [foreground1Movement, foreground2Movement, background1Movement, background2Movement];
var adjustBackgroundPlayback = function() {
if (redQueen_alice.playbackRate < .8) {
sceneries.forEach(function(anim) {
anim.playbackRate = redQueen_alice.playbackRate/2 * -1;
});
} else if (redQueen_alice.playbackRate > 1.2) {
sceneries.forEach(function(anim) {
anim.playbackRate = redQueen_alice.playbackRate/2;
});
} else {
sceneries.forEach(function(anim) {
anim.playbackRate = 0;
});
}
}
adjustBackgroundPlayback();
/* If Alice and the Red Queen are running at a speed of 1, the background doesn't move. */
/* But if they fall under 1, the background slides backwards */
setInterval( function() {
/* Set decay */
if (redQueen_alice.playbackRate > .4) {
redQueen_alice.playbackRate *= .9;
}
adjustBackgroundPlayback();
}, 3000);
var goFaster = function() {
/* But you can speed them up by giving the screen a click or a tap. */
redQueen_alice.playbackRate *= 1.1;
adjustBackgroundPlayback();
}
document.addEventListener("click", goFaster);
document.addEventListener("touchstart", goFaster);
Also see: Tab Triggers