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.
<figure></figure>
<button>Refresh</button>
* {
font-family: sans-serif;
}
html, body {
display: grid;
height: 100%;
place-items: center;
gap: 1em;
}
button {
padding: 1em 2em;
}
.u-visually-hidden {
border: 0;
clip: rect(0 0 0 0);
clip-path: polygon(0 0, 0 0, 0 0);
height: 1px;
margin: -1px;
overflow: hidden;
padding: 0;
position: absolute;
white-space: nowrap;
width: 1px;
}
///
/// Our component container
///
/// 1. Position our children in a single area
///
.circle-meter {
--meter-stroke-width: 3px;
color: #f2f5f7;
display: inline-grid;
grid-template-areas: 'content'; // 1
place-items: center; // 1
padding: var(--meter-stroke-width);
background-color: #0e1c43;
border-radius: 50%;
aspect-ratio: 1;
// This is a magic number, but ensures the meter is large enough to display
// a percentage with a single decimal point.
width: 11rem;
}
.circle-meter > * {
grid-area: content; // 1
}
///
/// Our actual percent is big and bold.
///
/// 1. We undo the tracking change. Since we're just showing a number, the
/// uppercase styling doesn't have any effect.
///
.circle-meter__percent {
font-weight: bold;
font-size: 2rem;
letter-spacing: initial; // 1
line-height: 1.1;
}
// Make sure our "gauge" graphic fills the container
.circle-meter__gauge {
width: 100%;
height: 100%;
}
///
/// The animated circle stroke
///
/// 1. We do some math to determine our circle's circumference. This gives us
/// the length of the stroke on our circle
/// 2. With more math we can determine how much of our circle's stroke should
/// be left undrawn (e.g. if our value is 75%, 25% of the stroke should be
/// undrawn. We need this in pixels.)
/// 3. Use SVG stroke drawing to only draw the relevant part of our stroke
/// @see https://css-tricks.com/svg-line-animation-works/
/// 4. By default, a circle's stroke starts on its right edge. We want it to
/// start from the top. We rotate the circle to achieve this
/// 5. Animate the stroke animation
///
.circle-meter__circle {
--radius: 47px; // 1
--pi: 3.14; // 1 - Close enough for our use case!
--circumference: calc(var(--radius) * 2 * var(--pi)); // 1
--stroke-length: var(--circumference); // 1
--stroke-offset: calc(
var(--circumference) - (var(--circumference) * var(--percent) / 100)
); // 2
fill: none;
r: var(--radius); // 1
rotate: -90deg; // 4
stroke: #3d84f5;
stroke-dasharray: var(--stroke-length); // 3
stroke-dashoffset: var(--stroke-offset); // 3
stroke-width: var(--meter-stroke-width);
transform-origin: center; // 4
@media (prefers-reduced-motion: no-preference) {
animation: stroke 750ms both; // 5
}
}
@keyframes stroke {
from {
stroke-dashoffset: var(--stroke-length);
}
to {
stroke-dashoffset: var(--stroke-offset);
}
}
const duration = 750;
let animationFrameId = 0;
function draw() {
cancelAnimationFrame(animationFrameId);
const endValue = (15 + Math.random() * 75).toFixed(1);
document.querySelector('figure').innerHTML = `
<div class="circle-meter">
<p class="circle-meter__text">
<span class="circle-meter__percent" id="meter-label">
<!--
For screen reader users we hide the animated value,
because it could be noisy and overwhelming.
We expose the end value
-->
<span class="u-visually-hidden">${endValue}%</span>
<span aria-hidden="true"><span class="animated-count">0</span>%</span>
</span>
</p>
<!-- I'm exposing this as a meter element, but I'm not sure if that's the best experience or not. -->
<svg
width="100"
height="100"
viewBox="0 0 100 100"
class="circle-meter__gauge"
role="meter"
aria-labelledby="meter-label"
aria-valuemin="0"
aria-valuemax="100"
aria-valuenow="${endValue}"
>
<circle
cx="50"
cy="50"
class="circle-meter__circle"
style="--percent: ${endValue}"
/>
</svg>
</div>
`;
if(window.matchMedia('(prefers-reduced-motion: reduce)').matches) {
document.querySelector('.animated-count').textContent = endValue;
return;
}
// Our start time (to determine where we are in the animation)
const startTime = performance.now();
// An array of the values that we'll be iterating through and displaying
// e.g. [1, 2, 3, ... 73, 74, 74.1, 74.2, 74.3]
const animationStates = determineAnimationStates(endValue);
function animate() {
// Compare our current time with our start time
let elapsedTime = performance.now() - startTime;
// If we've exceeded our duration, pretend we're right at the end of the
// animation.
if (elapsedTime > duration) {
elapsedTime = duration;
}
// See how far we are compared to the total duration
const percentComplete = elapsedTime / duration;
// Use that value to select a frame to display
document.querySelector('.animated-count').textContent = animationStates[
Math.round(percentComplete * (animationStates.length - 1))
]
// If we haven't hit the end of our animation, queue up another frame
if (elapsedTime < duration) {
animationFrameId = requestAnimationFrame(animate);
}
}
animationFrameId = requestAnimationFrame(animate);
}
draw();
document.querySelector('button').addEventListener('click', draw)
/**
* Determines all of the animation frames necessary to animate from 0 to another
* number.
*
* This function allows for up to one level of decimal point precision.
* However, decimals are only displayed for the final integer in the animation.
*
* For example, if we're animating to 74.3, our returned array would look like this:
* [1, 2, 3, ... 73, 74, 74.1, 74.2, 74.3]
*
* This is because if we displayed every decimal along the way we'd end up with
* an order of magnitude more animation states. If we tried to animate that in
* a short time frame it would be more animation frames than a human could observe.
* (e.g. trying to display 743 frames in 750 milliseconds)
*/
export function determineAnimationStates(endValue) {
const animationStates = [];
// Get our number rounded down
const roundedValue = Math.floor(endValue);
// Get every integer between 1 and our rounded down number
// [1, 2, 3, ... 73, 74]
for (let i = 1; i <= roundedValue; i++) {
animationStates.push(i);
}
// Determine whether there's anything in the first decimal place for us to
// worry about
const remainder = parseFloat((endValue - roundedValue).toFixed(2));
// Iterate over our first decimal place, adding the final decimal states
// [..., 74.1, 74.2, 74.3]]
if (remainder) {
// Originally my loop looked like this:
// `for (let i = 0.1; i <= 0.3; i += 0.1) {}`
// This was buggy due to floating point precision in JS (0.2 + 0.1 = 0.30000000000000004)
// Using integers instead of decimals makes the code a bit more complex
// but fixes this.
for (let i = 1; i <= remainder * 10; i += 1) {
animationStates.push(roundedValue + i / 10);
}
}
return animationStates;
}
Also see: Tab Triggers