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.
<!-- svg element made up of two overlapping path elements
both elements are animated in their d attribute
the second is also animated through the stroke-dasharray and stroke-dashoffset properties to progressively draw in the colored shape
-->
<svg
id="progress"
viewBox="0 0 100 100"
width="100"
height="100">
<g
fill="none"
stroke-width="7"
stroke-linecap="round"
stroke-linejoin="round"
transform="translate(5 5) scale(0.9 0.9)">
<path stroke="hsl(0, 0%, 0%)" opacity="0.5" d="M 0 50 100 50"></path>
<path stroke="currentColor" d="M 0 50 100 50"></path>
</g>
</svg>
<!-- buttons used to modify the appearance of the path elements
- sectioned in two containers, each describing the purpose through an SVG icon
- included through JavaScript, one for each option
-->
<div class="controls">
<!-- controls to modify the color -->
<div class="controls__color">
<!-- icon resembling a color palette -->
<svg viewBox="0 0 100 100" width="50" height="50">
<!-- cut out parts of the circle to describe the curvy silhouette of the color palette -->
<defs>
<mask id="mask">
<rect
x="0"
y="0"
width="100"
height="100"
fill="#fff">
</rect>
<circle
cx="50"
cy="20"
r="8"
fill="hsl(0, 0%, 0%)">
</circle>
<circle
cx="72"
cy="35"
r="8"
fill="hsl(0, 0%, 0%)">
</circle>
<circle
cx="28"
cy="35"
r="8"
fill="hsl(0, 0%, 0%)">
</circle>
<path
d="M 40 70 a 10 10 0 0 1 20 0 a 15 15 0 0 0 30 0 v 30 h -80 v -30 a 15 15 0 0 0 30 0"
fill="hsl(0, 0%, 0%)">
</path>
</mask>
</defs>
<g
transform="translate(50 50) rotate(50)">
<g
transform="translate(-50 -50)">
<circle
mask="url(#mask)"
cx="50"
cy="50"
r="45"
fill="hsl(0, 0%, 0%)">
</circle>
</g>
</g>
</svg>
</div>
<!-- controls to modify the shape -->
<div class="controls__shape">
<!-- icon resembling the pen tool -->
<svg viewBox="0 0 100 100" width="50" height="50">
<!-- rotate the icon toward the matching buttons -->
<g
transform="translate(50 50) rotate(30)">
<g
transform="translate(-50 -50)">
<path
fill="none"
stroke-linejoin="round"
stroke-linecap="round"
stroke-width="10"
stroke="hsl(0, 0%, 0%)"
d="M 40 20 h 20 l 20 30 l -30 40 v -40 v 40 l -30 -40 l 20 -30 v -10 h 20 v 10">
</path>
</g>
</g>
</svg>
</div>
</div>
* {
box-sizing: border-box;
padding: 0;
margin: 0;
}
/* display the elements in a column, horizontally centered */
body {
display: flex;
flex-direction: column;
align-items: center;
background: #16132f;
color: hsl(252, 65%, 63%);
}
/* size the larger svg to encompass a considerable portion of the viewport */
svg#progress {
width: 300px;
height: 300px;
/* fill dictated by the color set on the body */
fill: currentColor;
/* transition for the change in color */
transition: color 0.2s ease-in-out;
margin: 2rem 0;
}
/* display the icon and buttons in a row */
.controls__color,
.controls__shape {
margin: 2.5rem 0;
display: flex;
justify-content: center;
align-items: center;
/* position relative to absolute position the svg icons */
position: relative;
}
/* absolute position the first icon of each container to its side */
.controls__color > svg,
.controls__shape > svg {
position: absolute;
top: 50%;
right: 100%;
transform: translate(0%, -50%);
opacity: 0.5;
width: 70px;
height: 70px;
}
/* second icon to the right of the container */
.controls__shape > svg {
right: 0;
transform: translate(100%, -50%);
}
/* style the buttons to remove the default background and border */
.controls__color button,
.controls__shape button {
color: currentColor;
width: 32px;
height: 32px;
background: none;
border: none;
border-radius: 50%;
margin: 0.75rem;
}
/* expand the nested svg to occupy as much space as allocated by the button elements */
.controls__color button svg,
.controls__shape button svg {
display: block;
width: 100%;
height: 100%;
/* transition for the change in color */
transition: color 0.2s ease-in-out;
}
/* animation for the colored path */
@keyframes drawPath {
to {
stroke-dashoffset: 0;
}
}
// colors updating the SVG through the currentColor property
const colors = [
'hsl(166, 100%, 55%)',
'hsl(38, 100%, 60%)',
'hsl(316, 98%, 59%)',
'hsl(273, 100%, 68%)',
'hsl(252, 65%, 63%)',
];
/** syntax updating the SVG through the d attribute
* horizontal line
* heart rate
* lightning bolt
* star
* hourglass
*/
// the coordinates describe absolute points followed by the path element
// ! remember the M command at the beginning of the d attribute
const shapes = [
'0 50 100 50',
'0 50 12.5 50 18.75 65.5 25 50 37.5 50 43.75 34.1 56.25 60.87 62.5 50 68.75 50 75 15.38 81.25 50 87.5 50 93.75 71.33 100 50',
'50 0 80 0 60 30 75 30 45 70 70 70 20 100 35 70 20 70 45 30 30 30 50 0',
'50 10 65 40 90 40 70 60 80 90 50 75 20 90 30 60 10 40 35 40 50 10',
'15 15 85 15 15 85 85 85 15 15',
];
// ! to smoothly animate between the shapes it is necessary to maintain a consistent number of points
const pointRegex = /[\d\.]+ [\d\.]+/gi;
// create an array describing the points of each shape
const points = shapes.map(shape => shape.match(pointRegex));
// create an array detailing the length of the points' array, for each shape
const pointsLength = points.map(point => point.length);
// retrieve the highest number of points
const maxPoint = [...pointsLength].sort((a, b) => (a > b ? -1 : 1))[0];
// create an array of shapes adding to each string as many points to make up the difference between the shapes's points and the maximum amount
// include copies of the last point
const fixedShapes = shapes.map((shape, index) => {
const difference = maxPoint - pointsLength[index];
const lastPoint = points[index][points[index].length - 1];
const addendum = ` ${lastPoint}`;
return `${shape}${addendum.repeat(difference)}`;
});
// function updating the color property set on the body
const updateColor = (color) => { document.querySelector('body').style.color = color; };
// function updating the stroke-dash properties to match the length of the input element
const updateDash = (path) => {
const length = path.getTotalLength();
path.setAttribute('stroke-dasharray', length);
path.setAttribute('stroke-dashoffset', length);
};
// function updating the d attribute of both path elements
const updateShape = (shape) => {
const paths = document.querySelectorAll('svg#progress g path');
anime({
targets: paths,
d: `M ${shape}`,
duration: 800,
// as you animate the points attribute update the stroke-dash properties with the new length of the path
update() {
updateDash(paths[1]);
},
easing: 'easeInOutCirc',
});
};
// target the path elements and single out the colored one
const progressPaths = document.querySelectorAll('svg#progress g path');
const progressPath = progressPaths[1];
// update the d attribute with the first fixed value
progressPaths.forEach(path => path.setAttribute('d', `M ${fixedShapes[0]}`));
// update the dash properties on the colored path
updateDash(progressPath);
// begin the endless animation to draw the colored path and and out of sight
progressPath.style.animation = 'drawPath 7s ease-in-out infinite alternate';
// for the controls include one button for each color, one button for each shape
const controls = document.querySelector('.controls');
const controlsColor = controls.querySelector('.controls__color');
const controlsShape = controls.querySelector('.controls__shape');
// for each color add a button to update the path element with the prescribed hue
colors.forEach((color) => {
const button = document.createElement('button');
button.addEventListener('click', () => updateColor(color));
// in the button include a circle matching the input color
button.innerHTML = `
<svg viewBox="0 0 100 100" width="50" height="50">
<circle
cx="50"
cy="50"
r="50"
fill="${color}">
</circle>
</svg>`;
controlsColor.appendChild(button);
});
// for each shape add a button to change both path elements with the connected points attribute
fixedShapes.forEach((shape) => {
const button = document.createElement('button');
button.addEventListener('click', () => updateShape(shape));
// ! use the currentColor property to match the selected color
// ! use a larger viewBox to reduce the size of the path elements
button.innerHTML = `
<svg viewBox="0 0 150 150" width="50" height="50">
<g
transform="translate(25 25)">
<path
fill="none"
stroke="currentColor"
stroke-width="20"
stroke-linecap="round"
stroke-linejoin="round"
d="M ${shape}">
</path>
</g>
</svg>`;
controlsShape.appendChild(button);
});
Also see: Tab Triggers