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.
<header data-animation="fadeIn">
<h1>
<div class="title" data-animation="slideInDown" data-animation-delay=".75s"><b>Animate</b></div>
<div class="subtitle" data-animation="slideInUp" data-animation-delay="1.5s">On Scroll</div>
</h1>
</header>
<section>
<h1><b>Slide</b> Animation</h1>
<div class="box" data-animation="slideInRight"></div>
<div class="box" data-animation="slideInUp" data-animation-delay=".3s"></div>
<div class="box" data-animation="slideInDown" data-animation-delay=".6s"></div>
<div class="box" data-animation="slideInLeft" data-animation-delay=".9s"></div>
</section>
<section>
<h1><b>Zoom</b> Animation</h1>
<div class="box" data-animation="zoomIn"></div>
<div class="box" data-animation="zoomIn" data-animation-delay="300ms"></div>
<div class="box" data-animation="zoomIn" data-animation-delay="600ms"></div>
</section>
<section>
<h1><b>Reverse Zoom</b> Animation</h1>
<div class="box" data-animation="zoomReverseIn"></div>
<div class="box" data-animation="zoomReverseIn" data-animation-delay="300ms"></div>
<div class="box" data-animation="zoomReverseIn" data-animation-delay="600ms"></div>
</section>
<section>
<h1><b>Fade</b> Animation</h1>
<div class="box" data-animation="fadeIn"></div>
<div class="box" data-animation="fadeIn" data-animation-delay="300ms"></div>
<div class="box" data-animation="fadeIn" data-animation-delay="600ms"></div>
</section>
<section>
<h1><b>Flip</b> Animation</h1>
<div class="box" data-animation="flipInY"></div>
<div class="box" data-animation="flipInY" data-animation-delay="300ms"></div>
<div class="box" data-animation="flipInY" data-animation-delay="600ms"></div>
</section>
// Variables
// ---------
:root {
--animation-timing-function: cubic-bezier(0.4, 0, 0.2, 1);
--animation-duration: 1s;
}
// Keyframes
// ---------
@keyframes slideInUp {
0% {
opacity: 0;
transform: translateY(25%);
}
100% {
opacity: 1;
transform: none;
}
}
@keyframes slideInDown {
0% {
opacity: 0;
transform: translateY(-25%);
}
100% {
opacity: 1;
transform: none;
}
}
@keyframes slideInleft {
0% {
opacity: 0;
transform: translateX(25%);
}
100% {
opacity: 1;
transform: none;
}
}
@keyframes slideInRight {
0% {
opacity: 0;
transform: translateX(-25%);
}
100% {
opacity: 1;
transform: none;
}
}
@keyframes fadeIn {
0% {
opacity: 0;
}
100% {
opacity: 1;
}
}
@keyframes zoomIn {
0% {
opacity: 0;
transform: scale(0.75);
}
100% {
opacity: 1;
transform: none;
}
}
@keyframes zoomReverseIn {
0% {
opacity: 0;
transform: scale(1.25);
}
100% {
opacity: 1;
transform: none;
}
}
@keyframes flipInY {
0% {
opacity: 0;
transform: perspective(90vw) rotateY(67.50deg);
}
100% {
opacity: 1;
transform: none;
}
}
// Animations
// ----------
[data-animation] {
opacity: 0;
animation-timing-function: var(--animation-timing-function);
animation-fill-mode: both;
animation-duration: var(--animation-duration);
will-change: transform, opacity;
}
// Disable animation of the childs
// Disable self animation
.animations-disabled {
&,
[data-animation] {
animation: none !important;
opacity: 1 !important;
}
}
// Slide Animations
.slideInUp {
animation-name: slideInUp;
}
.slideInDown {
animation-name: slideInDown;
}
.slideInLeft {
animation-name: slideInleft;
}
.slideInRight {
animation-name: slideInRight;
}
// Fade Animations
.fadeIn {
animation-name: fadeIn;
}
// Zoom Animations
.zoomIn {
animation-name: zoomIn;
}
.zoomReverseIn {
animation-name: zoomReverseIn;
}
// Flip Animations
.flipInY {
animation-name: flipInY;
}
.flipOutY {
animation-name: flipInY;
animation-direction: reverse;
}
// ===========
// Demo Styles
// ===========
* {
box-sizing: border-box;
line-height: calc(1em + 0.25rem);
}
html {
background: #000;
font: 18px/1.5 "Poppins", sans-serif;
letter-spacing: -0.1em;
}
body {
margin: 0;
min-height: 100vh;
overflow-x: hidden;
color: hsl(210, 20%, 60%);
font-weight: 200;
}
h1 {
width: 100%;
font-weight: 200;
text-align: center;
margin: 0 0 4rem;
font-size: 3rem;
}
b {
font-weight: 600;
}
header,
section {
display: flex;
flex-wrap: wrap;
width: 100vw;
height: 100vh;
justify-content: space-evenly;
align-items: center;
align-content: center;
padding: 3rem 1rem;
}
header {
background: radial-gradient(
circle at top right,
hsl(340, 80%, 50%),
hsl(260, 80%, 50%)
);
color: #fff;
}
header h1 {
margin: 0;
color: #fff;
}
header .title {
font-size: 5rem;
}
header .subtitle {
font-size: 4rem;
}
section {
background: #fff;
&:nth-child(odd) {
background: hsl(210, 25%, 97.5%);
}
}
.box {
width: 7rem;
height: 7rem;
margin: 1rem;
background: radial-gradient(
circle at top right,
hsl(340, 80%, 50%),
hsl(260, 80%, 50%)
);
border-radius: 1.5rem;
box-shadow: 0 0 0 3px rgba(255,255,255,0.3) inset, 0 3px 9px rgba(0, 0, 0, 0.3);
}
const AnimateOnScroll = function ({ offset } = { offset: 10 }) {
// Define a dobra superior, inferior e laterais da tela
const windowTop = (offset * window.innerHeight) / 100;
const windowBottom = window.innerHeight - windowTop;
const windowLeft = 0;
const windowRight = window.innerWidth;
this.start = (element) => {
window.requestAnimationFrame(() => {
// Seta os atributos customizados
element.style.animationDelay = element.dataset.animationDelay;
element.style.animationDuration = element.dataset.animationDuration;
// Inicia a animacao setando a classe para animar
element.classList.add(element.dataset.animation);
// Seta o elemento como animado
element.dataset.animated = "true";
});
};
this.inViewport = (element) => {
// Obtem o boundingbox do elemento
const elementRect = element.getBoundingClientRect();
const elementTop =
elementRect.top + parseInt(element.dataset.animationOffset) ||
elementRect.top;
const elementBottom =
elementRect.bottom - parseInt(element.dataset.animationOffset) ||
elementRect.bottom;
const elementLeft = elementRect.left;
const elementRight = elementRect.right;
// Verifica se o elemento esta na tela
return (
elementTop <= windowBottom &&
elementBottom >= windowTop &&
elementLeft <= windowRight &&
elementRight >= windowLeft
);
};
// Percorre o array de elementos, verifica se o elemento está na tela e inicia animação
this.verifyElementsInViewport = (els = elements) => {
for (let i = 0, len = els.length; i < len; i++) {
// Passa para o proximo laço se o elemento ja estiver animado
if (els[i].dataset.animated) continue;
this.inViewport(els[i]) && this.start(els[i]);
}
};
// Obtem todos os elementos a serem animados
this.getElements = () =>
document.querySelectorAll("[data-animation]:not([data-animated])");
// Atualiza a lista de elementos a serem animados
this.update = () => {
elements = this.getElements();
elements && this.verifyElementsInViewport(elements);
};
// Inicia os eventos
window.addEventListener("load", this.update, false);
window.addEventListener(
"scroll",
() => this.verifyElementsInViewport(elements),
{ passive: true }
);
window.addEventListener(
"resize",
() => this.verifyElementsInViewport(elements),
{ passive: true }
);
};
// Initialize
const options = {
offset: 15 // percentage of the window
};
const animation = new AnimateOnScroll(options);
Also see: Tab Triggers