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 class="anchor-element js-anchor-element">
<svg
xmlns="http://www.w3.org/2000/svg"
viewBox="0 -960 960 960"
width="24"
>
<path
d="M480-80q-61 0-125-22t-116-60q-52-38-85.5-89T120-360v-120l160 120-62 62q29 51 92 88t130 47v-357H320v-80h120v-47q-35-13-57.5-43.5T360-760q0-50 35-85t85-35q50 0 85 35t35 85q0 39-22.5 69.5T520-647v47h120v80H520v357q67-10 130-47t92-88l-62-62 160-120v120q0 58-33.5 109T721-162q-52 38-116 60T480-80Zm0-640q17 0 28.5-11.5T520-760q0-17-11.5-28.5T480-800q-17 0-28.5 11.5T440-760q0 17 11.5 28.5T480-720Z"
/>
</svg>
</div>
<div class="anchor-element js-anchor-element">
<svg
xmlns="http://www.w3.org/2000/svg"
viewBox="0 -960 960 960"
width="24"
>
<path
d="M480-80q-61 0-125-22t-116-60q-52-38-85.5-89T120-360v-120l160 120-62 62q29 51 92 88t130 47v-357H320v-80h120v-47q-35-13-57.5-43.5T360-760q0-50 35-85t85-35q50 0 85 35t35 85q0 39-22.5 69.5T520-647v47h120v80H520v357q67-10 130-47t92-88l-62-62 160-120v120q0 58-33.5 109T721-162q-52 38-116 60T480-80Zm0-640q17 0 28.5-11.5T520-760q0-17-11.5-28.5T480-800q-17 0-28.5 11.5T440-760q0 17 11.5 28.5T480-720Z"
/>
</svg>
</div>
<div class="anchor-positioned-element">
<h2>Drag the anchors to size me!</h2>
</div>
@layer tokens, reset, animations, base, components, theme, demo;
@layer demo {
.anchor-element {
position: absolute;
cursor: grab;
z-index: 1;
&:active {
cursor: grabbing;
}
&:nth-of-type(1) {
top: var(--y, var(--size-4));
left: var(--x, var(--size-4));
anchor-name: --anchor-element-one;
}
&:nth-of-type(2) {
top: var(--y, calc(100% - var(--size-8) - var(--size-4)));
left: var(--x, calc(100% - var(--size-8) - var(--size-4)));
anchor-name: --anchor-element-two;
}
}
.anchor-positioned-element {
position: absolute;
top: anchor(--anchor-element-one bottom);
left: anchor(--anchor-element-one right);
right: anchor(--anchor-element-two left);
bottom: anchor(--anchor-element-two top);
display: grid;
overflow: clip;
white-space: nowrap;
padding: var(--size-2) var(--size-3);
place-items: center;
background-color: var(--surface-2);
border: 1px solid var(--gray-7);
}
h2 {
color: var(--gray-7);
font-size: var(--font-size-5);
&::selection {
background-color: transparent;
}
}
}
@layer tokens {
:where(html) {
--gray-0: rgb(248, 249, 250);
--gray-1: rgb(241, 243, 245);
--gray-2: rgb(233, 236, 239);
--gray-3: rgb(222, 226, 230);
--gray-4: rgb(206, 212, 218);
--gray-5: rgb(173, 181, 189);
--gray-6: rgb(134, 142, 150);
--gray-7: rgb(73, 80, 87);
--gray-8: rgb(52, 58, 64);
--gray-9: rgb(33, 37, 41);
--blue-0: rgb(231, 245, 255);
--blue-1: rgb(208, 235, 255);
--blue-2: rgb(165, 216, 255);
--blue-3: rgb(116, 192, 252);
--blue-4: rgb(77, 171, 247);
--blue-5: rgb(51, 154, 240);
--blue-6: rgb(34, 139, 230);
--blue-7: rgb(28, 126, 214);
--blue-8: rgb(25, 113, 194);
--blue-9: rgb(24, 100, 171);
--indigo-0: #edf2ff;
--indigo-1: #dbe4ff;
--indigo-2: #bac8ff;
--indigo-3: #91a7ff;
--indigo-4: #748ffc;
--indigo-5: #5c7cfa;
--indigo-6: #4c6ef5;
--indigo-7: #4263eb;
--indigo-8: #3b5bdb;
--indigo-9: #364fc7;
--ease-in-out: cubic-bezier(0.4, 0, 0.2, 1);
--ease-out: cubic-bezier(0, 0, 0.2, 1);
--ease-in: cubic-bezier(0.4, 0, 1, 1);
--sharp: cubic-bezier(0.4, 0, 0.6, 1);
--ease-spring: linear(
0,
0.006,
0.025 2.8%,
0.101 6.1%,
0.539 18.9%,
0.721 25.3%,
0.849 31.5%,
0.937 38.1%,
0.968 41.8%,
0.991 45.7%,
1.006 50.1%,
1.015 55%,
1.017 63.9%,
1.001
);
--font-family-system: system-ui, sans-serif;
--font-family-sans-serif: Inter, Roboto, Helvetica Neue, Arial Nova,
Nimbus Sans, Arial, sans-serif;
--font-line-height-1: 1;
--font-line-height-2: 1.25;
--font-line-height-3: 1.375;
--font-line-height-4: 1.5;
--font-size-1: 1rem;
--font-size-2: 1.1rem;
--font-size-3: 1.25rem;
--font-size-4: 1.5rem;
--font-size-5: 2rem;
--font-size-6: 2.5rem;
--font-size-7: 3rem;
--font-size-8: 3.5rem;
--font-weight-1: 100;
--font-weight-2: 200;
--font-weight-3: 300;
--font-weight-4: 400;
--font-weight-5: 500;
--font-weight-6: 600;
--font-weight-7: 700;
--font-weight-8: 800;
--font-weight-9: 900;
--size-1: 0.5rem;
--size-2: calc(2 * var(--size-1));
--size-3: calc(3 * var(--size-1));
--size-4: calc(4 * var(--size-1));
--size-5: calc(5 * var(--size-1));
--size-6: calc(6 * var(--size-1));
--size-7: calc(7 * var(--size-1));
--size-8: calc(8 * var(--size-1));
--size-9: calc(9 * var(--size-1));
--size-10: calc(10 * var(--size-1));
--radius-1: 1rem;
--duration-1: 100ms;
--duration-2: 150ms;
--duration-3: 200ms;
--duration-4: 400ms;
}
}
@layer reset {
*,
::before,
::after {
box-sizing: border-box;
}
:where(*) {
margin: 0;
padding: 0;
}
:where(h1, h2, h3, h4, h5, h6) {
font-weight: var(--font-weight-9);
line-height: var(--font-line-height-1);
}
:where(h1) {
font-size: var(--font-size-8);
}
:where(h2) {
font-size: var(--font-size-6);
}
:where(h3) {
font-size: var(--font-size-5);
}
:where(h4) {
font-size: var(--font-size-4);
}
:where(h5) {
font-size: var(--font-size-3);
}
:where(a[href]) {
color: var(--link);
}
:where(a[href]):where(:visited) {
color: var(--link-visited);
}
:focus-visible {
outline-color: var(--brand, var(--link));
}
:where(input, button, textarea, select),
:where(input[type="file"])::-webkit-file-upload-button {
font: inherit;
font-size: inherit;
color: inherit;
letter-spacing: inherit;
}
::placeholder {
color: var(--gray-3);
opacity: 0.75;
}
:where(input:not([type="range"]), textarea, select) {
padding: 0.5rem 0.25rem;
}
:where(textarea, select, input:not(button)) {
background-color: var(--surface-2);
}
:where(ul[class]) {
list-style: none;
}
fieldset {
border: 0;
margin: 0;
padding: 0;
}
legend {
font-weight: 600;
font-size: 1.25rem;
margin-bottom: var(--size-2);
padding-inline: var(--size-1);
}
}
@layer animations {
@keyframes scale-out {
to {
scale: 0;
}
}
@keyframes fade-in {
to {
opacity: 1;
}
}
}
@layer base {
:where(html) {
height: 100%;
display: grid;
font-family: var(--font-family-sans-serif);
line-height: var(--font-line-height-4);
color: var(--text-1);
background-color: var(--surface-1);
}
:where(body) {
place-items: center;
display: grid;
overflow: hidden;
&:before {
content: "";
top: 0;
left: 0;
z-index: -1;
position: fixed;
width: 100vw;
height: 100vh;
display: block;
pointer-events: none;
mask: linear-gradient(-30deg, transparent 50%, white);
background: linear-gradient(
90deg,
var(--dot-bg) calc(var(--dot-space) - var(--dot-size)),
transparent 1%
)
center / var(--dot-space) var(--dot-space),
linear-gradient(
var(--dot-bg) calc(var(--dot-space) - var(--dot-size)),
transparent 1%
)
center / var(--dot-space) var(--dot-space),
var(--dot-color);
}
}
::-webkit-scrollbar-track {
background: transparent;
cursor: pointer;
}
::-webkit-scrollbar-thumb {
background-color: var(--indigo-3);
cursor: pointer;
border-radius: var(--radius-1);
}
::-webkit-scrollbar {
height: var(--size-1);
width: var(--size-1);
background: transparent;
cursor: pointer;
}
@media (prefers-reduced-motion: no-preference) {
:where(*) {
scroll-behavior: smooth;
}
}
}
@layer components {
.container {
width: 55ch;
display: grid;
gap: var(--size-2);
}
.chip {
position: relative;
cursor: pointer;
border: 1px solid var(--gray-7);
padding: var(--size-1) var(--size-2);
border-radius: var(--radius-1);
background-color: var(--surface-1);
line-height: 1;
transition: color var(--duration-4) var(--ease-spring),
background-color var(--duration-4) var(--ease-spring);
::selection {
background: transparent;
}
&:after {
content: "";
position: absolute;
inset: -1px;
color: inherit;
opacity: 0;
border-radius: inherit;
pointer-events: none;
border: 1px solid var(--pop);
transition: opacity 400ms var(--ease-spring);
}
&:has(:checked) {
color: var(--pop);
background-color: var(--pop-dark);
&:after {
opacity: 1;
}
}
&:hover:has(:not(:checked)) {
background-color: var(--pop-dark);
&:after {
opacity: 0.2;
}
}
[type="radio"] {
position: absolute;
width: 1px;
height: 1px;
padding: 0;
margin: -1px;
overflow: hidden;
clip: rect(0, 0, 0, 0);
white-space: nowrap;
border-width: 0;
}
}
.chip-list {
flex-wrap: wrap;
display: flex;
list-style-type: none;
gap: var(--size-1);
margin: 0;
padding: 0;
}
.message {
--message-tip-size: var(--size-1);
z-index: 100;
padding: var(--size-1) var(--size-2);
line-height: 1;
white-space: nowrap;
display: grid;
place-items: center;
border-radius: var(--radius-1);
background-color: var(--surface-4);
&.pointer {
margin: var(--message-tip-size);
&:after {
content: " ";
position: absolute;
top: 100%;
left: 50%;
margin-left: calc(-1 * var(--message-tip-size));
border-width: var(--message-tip-size);
border-style: solid;
border-color: var(--surface-4) transparent transparent transparent;
}
}
}
.anchor-element {
anchor-name: --anchor-element;
place-items: center;
display: grid;
width: var(--size-8);
aspect-ratio: 1;
border: 1px solid var(--divider);
border-radius: 22%;
background-color: var(--surface-2);
> svg {
width: 62%;
display: block;
opacity: 0.35;
fill: var(--gray-1);
}
}
}
@layer theme {
:where(html) {
--pop: var(--indigo-3);
--pop-dark: color-mix(in srgb, var(--pop) 10%, var(--surface-1));
--surface-1: var(--gray-9);
--surface-2: var(--gray-8);
--surface-3: var(--gray-7);
--surface-4: var(--gray-6);
--divider: var(--gray-7);
--text-1: var(--gray-1);
--text-2: var(--gray-2);
--link: var(--blue-6);
--link-visited: var(--blue-4);
--dot-bg: var(--surface-1);
--dot-color: var(--gray-7);
--dot-size: 0.125rem;
--dot-space: 1.5rem;
}
}
/**
* Makes an element draggable by updating its position with custom CSS
* properties --x and --y.
*
* @param {HTMLElement} element - The element to make draggable.
*/
function makeElementDraggable(element) {
let xOffset = 0;
let yOffset = 0;
element.addEventListener("mousedown", onMouseDown);
/**
* Handles the mousedown event to initiate dragging.
*
* @param {MouseEvent} event - The mousedown event.
*/
function onMouseDown(event) {
xOffset = event.clientX;
yOffset = event.clientY;
document.addEventListener("mousemove", onMouseMove);
document.addEventListener("mouseup", onMouseUp);
}
/**
* Handles the mousemove event to update element position.
*
* @param {MouseEvent} event - The mousemove event.
*/
function onMouseMove(event) {
const deltaX = event.clientX - xOffset;
const deltaY = event.clientY - yOffset;
// Update offsets to current mouse position
xOffset = event.clientX;
yOffset = event.clientY;
element.style.setProperty("--y", `${element.offsetTop + deltaY}px`);
element.style.setProperty("--x", `${element.offsetLeft + deltaX}px`);
}
/**
* Handles the mouseup event to stop dragging.
*/
function onMouseUp() {
document.removeEventListener("mouseup", onMouseUp);
document.removeEventListener("mousemove", onMouseMove);
}
}
// Bind the drag setup to all anchor elements
document.querySelectorAll(".js-anchor-element").forEach(makeElementDraggable);
Also see: Tab Triggers