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.
<main>
<h1>Performant Animations</h1>
<p>Favour using the properties <code>transform</code> and <code>opacity</code> for your animations. You will be surprised by how much you can achieve with only these 2 properties. Below, I demonstrate some of the actions you can do with these properties.</p>
<h2>Size</h2>
<div class="item">
<h3>Width</h3>
<figure>
<div class="container">
<div class="rect widen"></div>
</div>
<figcaption>
Changing width of a rectangle
</figcaption>
</figure>
<p>You can change the width of an element with <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/transform-function/scaleX()"><code>transform: scaleX()</code></a>.</p>
<pre>
<code class="language-css">
.widen {
animation: widen 2s infinite alternate;
}
@keyframes widen {
to {
transform: scaleX(2);
}
}
</code></pre>
<p>You can use <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/transform-function/scale()"><code>transform: scale()</code></a> if you wish to change the width and height together.</p>
</div>
<div class="item">
<h3>Height</h3>
<figure>
<div class="container">
<div class="rect heighten"></div>
</div>
<figcaption>
Changing height of a rectangle.
</figcaption>
</figure>
<p>You can change the height of an element with
<a href="https://developer.mozilla.org/en-US/docs/Web/CSS/transform-function/scaleY()"><code>transform: scaleY()</code></a>.
</p>
<pre>
<code class="language-css">
.heighten {
animation: heighten linear 2s infinite alternate;
}
@keyframes heighten {
to {
transform: scaleY(2);
}
}
</code>
</pre>
<p>You can use <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/transform-function/scale()"><code>transform: scale()</code></a> if you wish to change the width and height together.</p>
</div>
<hr>
<h2>Rotation</h2>
<div class="item">
<h3>Directional Rotation</h3>
<figure>
<div class="container rotation">
<svg class="arrow arrowRight rotate" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
<path d="M0 0h24v24H0z" fill="none" />
<path d="M16.01 11H4v2h12.01v3L20 12l-3.99-4z" />
</svg>
</div>
<figcaption>
Rotating right arrow clockwise 90 degrees.
</figcaption>
</figure>
<p>You can change the direction of an element with <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/transform-function/rotate()"><code>transform: rotate()</code></a>.</p>
<pre>
<code class="language-css">
.rotate {
animation: rotate linear 2s infinite alternate;
}
@keyframes rotate {
to {
transform: rotate(90deg);
}
}
</code>
</pre>
</div>
<div class="item rotation">
<h3>Sideways Rotation</h3>
<figure>
<div class="container">
<svg class="arrow arrowRight rotateSideways" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
<path d="M0 0h24v24H0z" fill="none" />
<path d="M16.01 11H4v2h12.01v3L20 12l-3.99-4z" />
</svg>
</div>
<figcaption>
This is a right arrow being rotated 360 degrees on Y axis.
</figcaption>
</figure>
<pre>
<code class="language-css">
.rotateSideways {
animation: rotateSideways linear 2s infinite alternate;
}
@keyframes rotateSideways {
to {
transform: rotateY(360deg);
}
}
</code>
</pre>
<p>This was counter-intuitive for me when I was began learning transformations. I thought it would be <code>rotateX()</code> to get this effect.</p>
</div>
<div class="item">
<h3>Lengthways Rotation</h3>
<figure>
<div class="container rotation">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" class="arrowUp arrow rotateLengthways">
<path d="M0 0h24v24H0V0z" fill="none" />
<path d="M4 12l1.41 1.41L11 7.83V20h2V7.83l5.58 5.59L20 12l-8-8-8 8z" />
</div>
<figcaption>
This is an up arrow being rotated 360 degrees on the X axis.
</figcaption>
</figure>
<p>You can rotate an element lengthways with
<a href="https://developer.mozilla.org/en-US/docs/Web/CSS/transform-function/rotateX()"><code>transform:rotateX()</code></a>.
</p>
<pre>
<code class="language-css">
.rotateLengthways {
animation: rotateLengthways linear 2s infinite alternate;
}
@keyframes rotateLengthways {
to {
transform: rotateX(360deg);
}
}
</code>
</pre>
<p>This was counter-intuitive for me when I was began learning transformations. I thought it would be <code>rotateY()</code> to get this effect.</p>
</div>
<hr>
<h2>Movement</h2>
<div class="item">
<h3>Horizontal Movement</h3>
<figure>
<div class="container">
<svg class="arrow arrowRight moveHorizontal" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
<path d="M0 0h24v24H0z" fill="none" />
<path d="M16.01 11H4v2h12.01v3L20 12l-3.99-4z" />
</svg>
</div>
<figcaption>
Moving right arrow horizontally.
</figcaption>
</figure>
<p>You can move an element horizontally with <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/transform-function/translateX"><code>transform:translateX()</code></a>.</p>
<pre>
<code class="language-css">
.moveHorizontal {
animation: moveHorizontal linear 2s infinite alternate;
}
@keyframes moveHorizontal {
to {
transform: translateX(150%);
}
}
</code>
</pre>
</div>
<div class="item">
<h3>Vertical Movement</h3>
<figure>
<div class="container">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" class="arrowUp arrow moveVertical">
<path d="M0 0h24v24H0V0z" fill="none" />
<path d="M4 12l1.41 1.41L11 7.83V20h2V7.83l5.58 5.59L20 12l-8-8-8 8z" />
</svg>
</div>
<figcaption>
Moving up arrow vertically.
</figcaption>
</figure>
<p>You can move an element vertically with <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/transform-function/translateY"><code>transform:translateY()</code></a></p>
<pre>
<code class="language-css">
.moveVertical {
animation: moveVertical linear 2s infinite alternate;
}
@keyframes moveVertical {
to {
transform: translateY(30px);
}
}
</code>
</pre>
</div>
<div class="item">
<h3>Diagonal Movement</h3>
<figure>
<div class="container">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" class="arrowUp arrow moveDiagonally1">
<path d="M0 0h24v24H0V0z" fill="none" />
<path d="M4 12l1.41 1.41L11 7.83V20h2V7.83l5.58 5.59L20 12l-8-8-8 8z" />
</svg>
</div>
<figcaption>
Moving right arrow diagonally (north-east).
</figcaption>
</figure>
<p> You can move an element direction diagonally by using <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/transform-function/translate"><code>transform:translate()</code></a> to move the element horizontally and vertically at the same time. </p>
<pre>
<code class="language-css">
.moveDiagonally1 {
animation: moveDiagonally1 linear 2s infinite alternate;
}
@keyframes moveDiagonally1 {
to {
transform: translate(-50%, 50%);
}
}
</code>
</pre>
<figure>
<div class="container">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" class="arrowUp arrow moveDiagonally2">
<path d="M0 0h24v24H0V0z" fill="none" />
<path d="M4 12l1.41 1.41L11 7.83V20h2V7.83l5.58 5.59L20 12l-8-8-8 8z" />
</svg>
</div>
<figcaption>
Rotating and moving right arrow diagonally (north-east)</code>.
</figcaption>
</figure>
<p>If you want the element to face in the direction of movement, you need to rotate the element and use <code>translate()</code> to move it in the correct direction.</p>
<p>The order of transformations is important, <code>transform:rotate() translate()</code> and <code>transform:translate() rotate()</code> give different results.</p>
<pre>
<code class="language-css">
.moveDiagonally2 {
animation: moveDiagonally2 linear 2s infinite alternate;
}
@keyframes moveDiagonally2 {
0% {
transform: rotate(45deg) translateY(0);
}
100% {
transform: rotate(45deg) translateY(105%);
}
}
</code>
</pre>
</div>
<div class="item">
<h3>Moving Closer/Further Away</h3>
<figure>
<div class="container">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" class="train scale">
<path d="M0 0h24v24H0V0z" fill="none" />
<path d="M12 2c-4 0-8 .5-8 4v9.5C4 17.43 5.57 19 7.5 19L6 20.5v.5h2.23l2-2H14l2 2h2v-.5L16.5 19c1.93 0 3.5-1.57 3.5-3.5V6c0-3.5-3.58-4-8-4zM7.5 17c-.83 0-1.5-.67-1.5-1.5S6.67 14 7.5 14s1.5.67 1.5 1.5S8.33 17 7.5 17zm3.5-7H6V6h5v4zm2 0V6h5v4h-5zm3.5 7c-.83 0-1.5-.67-1.5-1.5s.67-1.5 1.5-1.5 1.5.67 1.5 1.5-.67 1.5-1.5 1.5z" />
</svg>
</div>
<figcaption>
Moving train closer and further away.
</figcaption>
</figure>
<p>You can use <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/transform-function/scale()"><code>transform: scale()</code></a> or <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/transform-function/translateZ()"><code>transform: translateZ()</code></a>.</p>
<p>Using <code>transform: scale()</code> is shrinking or enlarging an element. It is kind of cheating! It works well to create a perception of depth for simple movement e.g. straight lines.</p>
<pre>
<code class="language-css">
.scale {
animation: scale linear 2s infinite alternate;
}
@keyframes scale {
to {
transform: scale(0);
}
}
</code>
</pre>
<p>For more complex movements, you need to make a translation along the Z-axis. The Z-axis is the depth dimension. Using <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/transform-function/translate3D()"><code>transform: translate3d()</code></a> will give you complete control of moving an element in all 3 dimensions.</p>
</div>
<hr>
<h2>Visibility</h2>
<div class="item">
<h3>Hide/Show</h3>
<figure>
<div class="container">
<div class="rect visibility"></div>
</div>
<figcaption>
Hiding/showing rectangle.
</figcaption>
</figure>
<p>Use <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/opacity"><code>opacity</code></a> to control visibility of an element. You can use it for toggling between states and flashing objects at a particular frequency.</p>
<pre>
<code class="language-css">
.visibility {
animation: visibility linear 1s infinite alternate;
}
@keyframes visibility {
to {
opacity: 0;
}
}
</code>
</pre>
</div>
</main>
:root {
--element-color: rgb(255, 0, 191);
--max-width: 600px;
}
html {
box-sizing: border-box;
}
body {
background-color: black;
color: white;
font-family: "Lucida Sans", "Lucida Sans Regular", "Lucida Grande",
"Lucida Sans Unicode", Geneva, Verdana, sans-serif;
}
main {
padding: 1rem 0.25rem;
max-width: var(--max-width);
margin: 0 auto;
}
a {
color: var(--element-color);
text-decoration: none;
}
h1,
h2,
h3 {
width: 100%;
text-align: center;
}
h2 {
text-decoration: underline;
}
hr {
color: white;
}
.item {
display: grid;
width: 100%;
max-width: var(--max-width);
row-gap: 1rem;
margin: 1rem auto;
}
figure {
margin: 0 5px;
}
figcaption {
margin-top: 0.5rem;
text-align: center;
font-size: 0.75rem;
}
pre[class*="language-"] {
overflow: auto;
margin: 0 5px;
}
.container {
height: 100px;
display: flex;
justify-content: center;
align-items: center;
background-color: rgb(0, 53, 199);
box-shadow: rgba(88, 188, 255, 0.5) 0 0 2px 2px;
}
/* animated elements */
.rect {
width: 60px;
height: 30px;
background-color: var(--element-color);
box-shadow: rgba(0, 0, 0, 0.3) 0 0 2px 1px;
}
svg {
fill: var(--element-color);
}
.arrow,
.train {
width: 60px;
}
.arrowUp {
width: 40px;
}
@media screen and (min-width: 600px) {
}
/* animations */
.widen {
animation: widen 2s infinite alternate;
}
@keyframes widen {
to {
transform: scaleX(2);
}
}
.heighten {
animation: heighten linear 2s infinite alternate;
}
@keyframes heighten {
to {
transform: scaleY(2);
}
}
.rotate {
animation: rotate linear 2s infinite alternate;
}
@keyframes rotate {
to {
transform: rotate(90deg);
}
}
.rotateSideways {
animation: rotateSideways linear 2s infinite alternate;
}
@keyframes rotateSideways {
to {
transform: rotateY(360deg);
}
}
.rotateLengthways {
animation: rotateLengthways linear 2s infinite alternate;
}
@keyframes rotateLengthways {
to {
transform: rotateX(360deg);
}
}
.moveHorizontal {
animation: moveHorizontal linear 2s infinite alternate;
}
@keyframes moveHorizontal {
to {
transform: translateX(150%);
}
}
.moveVertical {
animation: moveVertical linear 2s infinite alternate;
}
@keyframes moveVertical {
to {
transform: translateY(30px);
}
}
.moveDiagonally1 {
animation: moveDiagonally1 linear 2s infinite alternate;
}
@keyframes moveDiagonally1 {
to {
transform: translate(-50%, 50%);
}
}
.moveDiagonally2 {
animation: moveDiagonally2 linear 2s infinite alternate;
}
@keyframes moveDiagonally2 {
0% {
transform: rotate(45deg) translateY(0);
}
100% {
transform: rotate(45deg) translateY(105%);
}
}
.scale {
animation: scale linear 2s infinite alternate;
}
@keyframes scale {
to {
transform: scale(0);
}
}
.visibility {
animation: visibility linear 1s infinite alternate;
}
@keyframes visibility {
to {
opacity: 0;
}
}
Also see: Tab Triggers