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.
.center
.grid
mixin selector(name)
- var radioClass = name + '-radio';
- var degreeClass = name + '-degree';
- var arrowClass = name + '-arrow';
// A set of radio buttons each representing a turn (0°, 90°, 180°, 270°)
// With CSS we'll always show on top the one that leads to the next state
input.radio(class=radioClass, type="radio", name=name, value="1", checked)
input.radio(class=radioClass, type="radio", name=name, value="2")
input.radio(class=radioClass, type="radio", name=name, value="3")
input.radio(class=radioClass, type="radio", name=name, value="4")
// A graphical indicator for the direction of the rotation
// Consists of an arrow head and a bunch of tail parts
// The 3D shape is made by CSS
.arrow(class=arrowClass)
.head
- var n = 0;
while n++ < 30
.tail-part
// The label showing up on hover indicating the current degree of the turn
each val in [1, 2, 3, 4]
- var id = name + val;
.degree(id=id, class=degreeClass) #{val * 90 - 90}°
// The three selectors below the dice
+selector("y")
+selector("z")
+selector("x")
// The dice with each side
// The 2D graphics of sides are defined here in SVGs
// The overall 3D shape is made with CSS
.dice
.box
svg.front
circle(cx="30", cy="30", r="20")
circle(cx="75", cy="30", r="20")
circle(cx="120", cy="30", r="20")
circle(cx="30", cy="120", r="20")
circle(cx="75", cy="120", r="20")
circle(cx="120", cy="120", r="20")
svg.back
circle(cx="75", cy="75", r="20")
svg.left
circle(cx="30", cy="30", r="20")
circle(cx="120", cy="120", r="20")
svg.right
circle(cx="30", cy="30", r="20")
circle(cx="120", cy="30", r="20")
circle(cx="30", cy="120", r="20")
circle(cx="120", cy="120", r="20")
circle(cx="75", cy="75", r="20")
svg.top
circle(cx="30", cy="30", r="20")
circle(cx="120", cy="30", r="20")
circle(cx="30", cy="120", r="20")
circle(cx="120", cy="120", r="20")
svg.bottom
circle(cx="30", cy="30", r="20")
circle(cx="75", cy="75", r="20")
circle(cx="120", cy="120", r="20")
.label Click an axis to rotate
footer
a(href="https://twitter.com/HunorBorbely", target="_top") @HunorBorbely
/*
You can find some of the ideas I used in this pen on the detail page and in the code: https://codepen.io/HunorMarton/details/mdERrLy
Follow me on twitter: https://twitter.com/HunorBorbely
*/
$x-rotation: -20deg;
$y-rotation: -40deg;
$base-color: lighten(#036, 10%);
$font-color: #6f9ceb;
body {
font-family: Avenir, Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
background-color: #ececec;
margin: 0;
}
.center {
position: absolute;
left: 50%;
top: 50%;
margin-left: -180px;
margin-top: -230px;
}
.grid {
position: relative;
display: grid;
grid-template-columns: 100px 100px 100px;
grid-template-rows: 280px 100px 20px;
grid-column-gap: 20px;
grid-row-gap: 30px;
border-radius: 10px;
.dice {
grid-row: 1;
grid-column: 1 / -1;
}
.y-radio,
.y-degree,
.y-arrow {
grid-row: 2;
grid-column: 1;
}
.z-radio,
.z-degree,
.z-arrow {
grid-row: 2;
grid-column: 2;
}
.x-radio,
.x-degree,
.x-arrow {
grid-row: 2;
grid-column: 3;
}
.radio {
margin: 0;
width: 100%;
height: 100%;
opacity: 0; // The radio button should be invisible
z-index: 5; // The radio button must be on top
cursor: pointer;
}
.degree {
border-radius: 10px;
color: $font-color;
font-weight: 900;
font-size: 1.5em;
width: 100%;
height: 100%;
z-index: 3;
box-sizing: border-box;
align-items: center;
justify-content: center;
}
.label {
text-align: center;
grid-column: 1 / -1;
}
}
$axis: "y", "z", "x";
// The buttons use a series of checkboxes on top of each other
// In every case the checkbox leading to the next step is visible
// By default all radio buttons are hidden
.degree,
.radio {
display: none;
}
// Always show the first radio button in the background
// then show the radio button leading for the next state on top of it
// 0° checkbox checked -> Display the checkbox for 90°
// 90° checkbox checked -> Display the checkbox for 180°
// 180° checkbox checked -> Display the checkbox for 270°
// 270° checkbox checked -> Do not display next checkbox but as the 0° is always in the background that will show
@each $a in $axis {
.#{$a}-radio[value="1"],
.#{$a}-radio[value="1"]:checked ~ .#{$a}-radio[value="2"],
.#{$a}-radio[value="2"]:checked ~ .#{$a}-radio[value="3"],
.#{$a}-radio[value="3"]:checked ~ .#{$a}-radio[value="4"] {
display: block;
}
}
// Show the degree on top of the degree in percentage
@each $a in $axis {
@for $i from 1 through 4 {
.#{$a}-radio[value="#{$i}"]:checked ~ .#{$a}-radio:hover ~ ##{$a}#{$i},
.#{$a}-radio:hover ~ .#{$a}-radio[value="#{$i}"]:checked ~ ##{$a}#{$i} {
display: flex;
}
}
}
// Making the dice look 3D
// The rotation of the dice is at the bottom of CSS
.dice {
display: flex;
justify-content: center;
align-items: center;
transform-origin: center center;
transform-style: preserve-3d;
transition: transform 1s;
.box {
transform-style: preserve-3d;
$size: 150px;
width: $size;
height: $size;
position: relative;
svg {
background-color: white;
border: 2px solid lighten($base-color, 5%);
fill: darken($base-color, 10%);
position: absolute;
border-radius: 10px;
width: $size;
height: $size;
}
// Put the sides of the dice in place
.front {
transform: translateX(-$size / 2) translateZ($size / 2);
}
.back {
transform: translateX(-$size / 2) rotateX(180deg) translateZ($size / 2);
}
.right {
transform: translateX(-$size / 2) rotateY(90deg) translateZ($size / 2);
}
.left {
transform: translateX(-$size / 2) rotateY(-90deg) translateZ($size / 2);
}
.top {
transform: translateX(-$size / 2) rotateX(90deg) translateZ($size / 2);
}
.bottom {
transform: translateX(-$size / 2) rotateX(-90deg) translateZ($size / 2);
}
}
}
.arrow {
position: relative;
transform-origin: center center;
transform-style: preserve-3d;
transition: transform 1s;
display: flex;
justify-content: center;
align-items: center;
height: 100%;
width: 100%;
$radius: 40px;
$width: 30px; // Width of the tail
.head {
position: absolute;
transform: translateZ($radius);
// Make a thriangle shape
// https://css-tricks.com/the-shapes-of-css/
width: 0;
height: 0;
border-left: $width/2 + 10 solid transparent;
border-right: $width/2 + 10 solid transparent;
border-bottom: $width/2 + 10 solid $base-color;
}
.tail-part {
position: absolute;
width: $width;
height: $radius/5;
// Make the arrow tail look circular
@for $i from 2 through 31 {
&:nth-of-type(#{$i}) {
// Rotate each by a different angle then translate it by circle radius
transform: rotateX($i * -10deg) translateZ($radius);
background-color: lighten($base-color, $i * 1%);
}
}
}
}
// Arrow 1
@for $y from 1 through 4 {
.y-radio[value="#{$y}"]:checked {
~ .y-arrow {
transform: rotateX($x-rotation)
rotateY($y-rotation)
rotateZ(-90deg)
rotateX(($y - 1) * 90deg);
}
}
}
// Arrow 2
@for $y from 1 through 4 {
.y-radio[value="#{$y}"]:checked {
@for $z from 1 through 4 {
~ .z-radio[value="#{$z}"]:checked {
~ .z-arrow {
transform: rotateX($x-rotation)
rotateY($y-rotation)
rotateZ(180deg)
rotateY(($y - 1) * 90deg - 90deg)
rotateX(($z - 1) * 90deg);
}
}
}
}
}
// Arrow 3
@for $y from 1 through 4 {
.y-radio[value="#{$y}"]:checked {
@for $z from 1 through 4 {
~ .z-radio[value="#{$z}"]:checked {
@for $x from 1 through 4 {
~ .x-radio[value="#{$x}"]:checked {
~ .x-arrow {
transform: rotateX($x-rotation)
rotateY($y-rotation)
rotateY(($y - 1) * -90deg)
rotateZ(($z - 1) * 90deg)
rotateX(($x - 1) * 90deg);
}
}
}
}
}
}
}
// Rotate the dice
@for $y from 1 through 4 {
@for $z from 1 through 4 {
@for $x from 1 through 4 {
.y-radio[value="#{$y}"]:checked
~ .z-radio[value="#{$z}"]:checked
~ .x-radio[value="#{$x}"]:checked
~ .dice {
transform: rotateX($x-rotation)
rotateY($y-rotation)
rotateY(($y - 1) * -90deg)
rotateZ(($z - 1) * 90deg)
rotateX(($x - 1) * 90deg);
}
}
}
}
footer {
position: absolute;
bottom: 20px;
font-size: 0.8em;
text-align: center;
width: 100%;
a:visited {
color: inherit;
}
}
Also see: Tab Triggers