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.
- let cats = {
- 'Tiger': {
- name: 'Panthera tigris',
- code: '1501705388883-4ed8a543392c',
- desc: 'tiger in the water'
- },
- 'Lion': {
- name: 'Panthera leo',
- code: '1519066629447-267fffa62d4b',
- desc: 'lion and lioness resting on a rock in the sun'
- },
- 'Leopard': {
- name: 'Panthera pardus',
- code: '1566489564594-f2163930c034',
- desc: 'blue-eyed leopard resting high up with its head on its front paws'
- },
- 'Jaguar': {
- name: 'Panthera onca',
- code: '1601544359642-c76c4f7c3221',
- desc: 'jaguar closeup'
- },
- 'Snow leopard': {
- name: 'Panthera uncia',
- code: '1689847190291-f8e0823f13ab',
- desc: 'snow leopard lying low on some rocks, its fur blending in perfecty'
- },
- 'Cheetah': {
- name: 'Acinonyx jubatus',
- code: '1693702366986-cbfbd1cf0450',
- desc: 'cheetah in the grass at dusk'
- },
- 'Cougar': {
- name: 'Puma concolor',
- code: '1661004527094-07d861089aed',
- desc: 'cougar walking through the snow'
- }
- };
- let e = Object.entries(cats), n = e.length;
- let m = 15;
- function rand(max, min, prc = 2)
- return +(min + (max - min)*Math.random()).toFixed(2)
section(style=`--n: ${n}; --k: 0`)
- for(let i = 0 ; i < n; i++)
- let v = e[i][1];
article(style=`--i: ${i}; --a: ${rand(m, -1*m)}deg`)
h2 #{e[i][0]}
em #{v.name}
img(src=`https://images.unsplash.com/photo-${v.code}?w=400` alt=v.desc)
div
button(aria-label='previous' data-inc='-1')
button(aria-label='next' data-inc='1')
$t: .8s;
/* register these two so they can be transitioned */
/* index of previous top item during the animation
* after another has been selected */
@property --p {
syntax: '<number>';
initial-value: 0;
inherits: true
}
/* animated index of top item from previous to current */
@property --v {
syntax: '<number>';
initial-value: 0;
inherits: true
}
* { margin: 0 } /* poor man's reset */
/* grid everything! */
html, body, section, article, button { display: grid }
/* full viewport height container for stack section */
html { min-height: 100% }
body { background: #070410 }
section {
/* set previous top item index to current top index
* 0s duration delayed transition ensures switch happens
* after the switching top item transtion */
--p: var(--k);
/* abs of difference between current & previous top item indices
* 0 if not during animation,
* 1 during animation not between two ends,
* n - 1 > 1 (if n > 2) during animation from one end to another */
--abs-p: abs(var(--k) - var(--p));
/* animation from one end of the n > 2 length list to another?
* 0 if no, 1 if yes */
--end: clamp(0, var(--abs-p) - 1, 1);
/* direction we're going in given by
* sign of difference between current & previous top item indices
* -1 backwards, 1 forwards, 0 no animation;
* switch sign IF we're going from an end to another */
--dir: calc((1 - 2*var(--end))*sign(var(--k) - var(--p)));
/* forward direction flag: 0 backwards, 1 forwards */
--fwd: calc(.5*(1 + var(--dir)));
/* set animated value of top item index to current top index
* a transition ensures switch doesn't happen instantly */
--v: var(--k);
/* absolute value of difference between animated & previous top index */
--abs-v: abs(var(--v) - var(--p));
/* animation progress as a decimal value */
--prg: calc(var(--abs-v)/(1 - var(--end) + var(--end)*(var(--n) - 1)));
/* bigger space between the two columns (image stack & all else) */
grid-gap: .5em 4em;
grid-template: /* 4 row, 2 col grid */
repeat(2, max-content) 1fr max-content/
max-content 1fr;
place-self: center;
color: #f1f5f9;
font: 1em poppins, sans-serif;
counter-reset: k calc(1 + var(--k)) n var(--n);
/* transition previous & animated top item indices
* so they don't change instantly like the current one */
transition: --p 0s $t, --v $t;
/* counter & binomial name faded */
&::before, em { color: RGB(from currentColor r g b/ .6) }
&::before { /* counter */
grid-area: 1/ 2; /* all grid areas need to be set manually */
content: counter(k) '/' counter(n)
}
/* this ridiculousness needed for Chrome without flag */
/* for reference
* https://css-tricks.com/using-absolute-value-sign-rounding-and-modulo-in-css-today/ */
@supports not (scale: Abs(-2)) {
--abs-p: max(var(--k) - var(--p), var(--p) - var(--k));
--abs-v: max(var(--v) - var(--p), var(--p) - var(--v))
}
@supports not (scale: Sign(-2)) {
--dir: clamp(-1, (var(--k) - var(--p))*100000, 1)
}
}
article {
/* absolute value difference between
* currently top item index and current item index */
--abs-top: abs(var(--k) - var(--i));
/* not top item if the absoute value difference ≥ 1
* top if the difference is 0 */
--not-top: min(1, var(--abs-top));
/* top flag is the negation */
--top: calc(1 - var(--not-top));
/* difference between moving image index which is
* previous top item index --p if going backwards,
* current top item index --k if going forwards
* and current item index --i */
--val-mov: ((1 - var(--fwd))*var(--p) + var(--fwd)*var(--k) - var(--i));
--abs-mov: abs(var(--val-mov));
/* not moving image if the absoute value difference > 1
* moving image if the difference is 0 */
--not-mov: min(1, var(--abs-mov));
/* moving flag is the negation */
--mov: calc(1 - var(--not-mov));
grid-area: 1/ 1/ -1/ -1; /* stack to occupy entire parent grid */
grid-template: subgrid/ subgrid; /* and inherit template */
/* debatable whether z-index is the best way
* maybe CSS 3D transforms would be a better idea */
/* depends on number of items, its own index and top item index */
z-index: mod(calc(var(--n) - 1 + var(--i) - var(--k)), var(--n));
/* transition z-index */
transition: z-index $t cubic-bezier(1, -.9, 0, 1.9);
/* this ridiculousness needed for Chrome without flag */
@supports not (scale: Abs(-2)) {
--abs-top: max(var(--k) - var(--i), var(--i) - var(--k));
--abs-mov: max(var(--val-mov), -1*var(--val-mov));
}
}
h2, em {
/* fade out and slide down when not top item anymore,
* delayed fade in and slide up when becoming top item */
translate: 0 calc(var(--not-top)*1lh);
opacity: var(--top);
transition: .5*$t calc(var(--top)*.5*#{$t});
transition-property: translate, opacity
}
/* need to specify grid-area for all */
h2 { grid-area: 2/ 2 }
em { grid-area: 3/ 2 }
img {
/* value that grows from 0 to 1, then goes back to 0
* during the switching top items transition */
--sin: sin(var(--prg)*.5turn);
grid-area: 1/ 1/ -1; /* occupy entire first column */
/* slight glow effect */
border: solid 2px rgba(82, 82, 122, .5);
/* whatever, maybe better ways for setting dims */
height: 13em;
aspect-ratio: 1;
object-fit: cover; /* fill with no distortion */
border-radius: .75em;
/* slide just moving image out & back in */
translate: calc(-150%*var(--mov)*sqrt(var(--sin)));
/* random rotation, but not during slide out & back in transition */
rotate: calc((1 - var(--sin))*var(--a))
}
div { /* button wrapper */
display: flex;
gap: 2em;
grid-area: 4/ 2;
/* prevent button clicks during animation */
z-index: calc((1 - min(1, var(--abs-p)))*var(--n))
}
button { /* prettify button */
--sgn: -1;
--prc: calc(var(--hov, 0)*100%);
--c: color-mix(in hsl, #818cf8 var(--prc), #52527a);
border: none;
width: 1lh;
aspect-ratio: 1;
border-radius: 50%;
background: RGB(from var(--c) r g b/ .2);
color: color-mix(in hsl, #818cf8 var(--prc), currentcolor);
font: 900 2em/ 1.5 sans-serif;
transition: .3s ease-out;
transition-property: background-color, color;
&::before { /* arrow, SVG likely better, whatever */
place-self: center;
border: solid 2px;
border-width: 2px 2px 0 0;
width: 35%;
aspect-ratio: 1;
translate: calc(var(--sgn)*-15%);
rotate: 45deg;
scale: var(--sgn);
content: ''
}
&[data-inc='1'] { --sgn: 1; }
&:is(:hover, :focus) { --hov: 1 }
}
/* all the JS does is update current top item index */
const S = document.querySelector('section').style /* wrapper style */,
N = +S.getPropertyValue('--n') /* number of items */;
let k = +S.getPropertyValue('--k') /* idx of current top item */;
addEventListener('click', e => {
let v = +e.target.dataset.inc /* value (±1) to change top idx by */;
if(v) S.setProperty('--k', k = ((k + v + N)%N))
})
Also see: Tab Triggers