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>
<div class="scene">
<div class="card">
<div class="card-face card-face-front">FRONT</div>
<div class="card-face card-face-back">BACK</div>
<div class="card-face card-face-right">RIGHT</div>
<div class="card-face card-face-left">LEFT</div>
<div class="card-face card-face-top">TOP</div>
<div class="card-face card-face-bottom">BOTTOM</div>
</div>
</div>
<div class="padding"></div>
</div>
:root {
--card-max-width: 640px;
--card-max-height: 300px;
--card-thickness: 40px;
}
body {
font-family: Sans-Serif;
font-size: 2rem;
font-weight: 900;
}
.padding {
height: 1000px;
}
.scene {
/* Arbitrary values, just use the ones that better suits your webpage */
width: 40vw;
height: 25vw;
/* First use of the variables, our scene can't be larger than our card */
max-width: var(--card-max-width);
max-height: var(--card-max-height);
/* Small values of perspective makes it look like we are using a fisheye lens
while large values places the camera far away as if we were using a tele-objective */
perspective: 4000px;
position: fixed;
}
.card {
width: 100%;
height: 100%;
max-width: var(--card-max-width);
max-height: var(--card-max-height);
position: relative;
/* Forces child elements (all faces) to be positioned in 3D space */
transform-style: preserve-3d;
/* Our first equations! Read below 👇 */
transform-origin:
/* x-offset */ calc(0.5 * var(--card-max-width))
/* y-offset */ calc(0.5 * var(--card-max-height)) /* z-offset */
calc(0.5 * var(--card-thickness));
/* Just a short transition to make animations smooth */
transition: transform 0.6s ease-out;
}
.card-face {
position: absolute;
display: flex;
align-items: center;
justify-content: center;
}
.card-face-front,
.card-face-back {
/* Front and back faces just fill the horizontal space */
height: 100%;
width: 100%;
}
.card-face-front {
/* Move the front card half the thickness up */
transform: translateZ(calc(0.5 * var(--card-thickness)));
background-color: #390099;
}
.card-face-back {
transform:
/* Rotate the back card in two axis so that it looks good when fully rotated */ rotateY(
180deg
)
rotateZ(180deg) /* Move the back card half the thickness down */
translateZ(calc(0.5 * var(--card-thickness)));
background-color: #9e0059;
}
.card-face-left,
.card-face-right {
width: var(--card-thickness);
height: var(--card-max-height);
/* Position left and right faces at the horizontal center of the cube */
left: calc(0.5 * (var(--card-max-width) - var(--card-thickness)));
}
.card-face-left {
/* Left side faces left and is placed at the left end of the cube */
transform: rotateY(-90deg) translateZ(calc(0.5 * var(--card-max-width)));
background-color: #ff0054;
}
.card-face-right {
/* Right side faces right and is placed at the right end of the cube */
transform: rotateY(90deg) translateZ(calc(0.5 * var(--card-max-width)));
background-color: #ff5400;
}
.card-face-bottom,
.card-face-top {
width: var(--card-max-width);
height: var(--card-thickness);
/* Position left and right faces at the vertical center of the cube */
top: calc(0.5 * var(--card-max-height) - 0.5 * var(--card-thickness));
}
.card-face-bottom {
/* Bottom side faces down and is placed at the bottom of the cube */
transform: rotateX(-90deg) translateZ(calc(0.5 * var(--card-max-height)));
background-color: #ffbd00;
}
.card-face-top {
/* Top side faces up and is placed at the bottom of the cube */
transform: rotateX(90deg) translateZ(calc(0.5 * var(--card-max-height)));
background-color: #ffbdbd;
}
const SCROLL_UNTIL_FULL_ROTATION_PX = 500;
/**
* Clamp restricts a certain value X between a Min and Max values.
* If X < Min then Min is returned
* If X > Max then Max is returned
* Else X is returned
*/
const clamp = (min, max, x) => Math.min(max, Math.max(min, x));
/**
* Lerp stands for Linear Interpolation. Given two values X and Y
* and normalized value T between 0 and 1, it returns a value between
* X and Y that is proportional to the value T.
* If T < 0 then X is returned
* If T > 1 then Y is returned
* Else linear interpolation is returned
*/
const lerp = (x, y, t) => clamp(x, y, x * (1 - t) + y * t);
document.addEventListener("DOMContentLoaded", () => {
const card = document.querySelector(".card");
let scrollRotation = 0;
document.addEventListener("scroll", () => {
scrollRotation = lerp(
0,
360,
window.scrollY / SCROLL_UNTIL_FULL_ROTATION_PX
);
card.style.transform = `rotateX(${scrollRotation}deg)`;
});
});
Also see: Tab Triggers