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.
//- img src base
- const BASE = 'https://assets.codepen.io/2017/';
- const DATA = [
- {
- txt: 'black cockatoo',
- inf: 'The red-tailed black cockatoo is a large black cockatoo native to Australia. Adult males have a characteristic pair of bright red panels on the tail. It is more common in the drier parts of the continent.',
- pts: 25
- },
- {
- txt: 'hyacinth macaw',
- inf: 'The hyacinth macaw is a parrot native to central and eastern South America. At about 1m, it is longer than any other species of parrot. It is the largest macaw and the largest flying parrot species.',
- pts: 50
- },
- {
- txt: 'grey parrot',
- inf: 'The grey parrot is an African parrot. It is medium-sized, predominantly grey and black-billed. The head and wings are generally darker than the body. The head and body feathers have slight white edges; the tail feathers are red.',
- pts: 20
- }
- ], N = DATA.length;
//- max number of points out of all
- let max = Math.max(...DATA.map(c => c.pts));
//- alpha factors for halftone filter
- let f0 = .7, f1 = 1 - f0;
- let r = 2;
svg(width='0' height='0' aria-hidden='true')
filter#noclip
feComponentTransfer
// ditch everything with an alpha of up to .5
feFuncA(type='table' tableValues='0 0 1')
filter#halftone(color-interpolation-filter='sRGB')
// extract halftone map as alpha mask & paint it white
// halftone map
// https://mastodon.social/@anatudor/112401133442879091
// alpha mask
// https://mastodon.social/@anatudor/112286525196818095
// paint
// https://mastodon.social/@anatudor/112157559510002242
feColorMatrix(values=`0 0 0 0 1
0 0 0 0 1
0 0 0 0 1
${f0} 0 0 0 ${f1}` result='map')
// extract first halftone pattern layer
feColorMatrix(in='SourceGraphic'
values=`0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 ${f0} 0 0 ${f1}`)
// intersect with map
feComposite(in='map' operator='in')
// push all semi-transparent pixels to either 0 or 1
// pixels in [0, .25) interval get pushed to 0
// pixels in [.25, .5) interval get pushed to 1
// pixels in [.5, 1] interval get pushed to 1
feComponentTransfer
feFuncA(type='discrete' tableValues='0 1 1')
// fix jagged edges by first applying a tiny blur
feGaussianBlur(stdDeviation=r)
// then pushing most (not all!) semitransparent pixels
// towards either 0 or 1, whichever is closer
// call this result 'dot'
feComponentTransfer(result='dot')
feFuncA(type='table' tableValues=`${-r} ${r + 1}`)
// extract first halftone pattern layer
feColorMatrix(in='SourceGraphic'
values=`0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 ${f0} 0 ${f1}`)
// same steps as before
feComposite(in='map' operator='in')
feComponentTransfer
feFuncA(type='discrete' tableValues='0 1 1')
feGaussianBlur(stdDeviation=r)
feComponentTransfer
feFuncA(type='table' tableValues=`${-r} ${r + 1}`)
// stack 'dot' layer on top
feBlend(in='dot')
// decrease overall alpha
feComponentTransfer
feFuncA(type='linear' slope='.5')
.wrap(style=`--max: ${max}`)
h2 Parrots
- for(let i = 0; i < N; i++)
- let c = DATA[i];
- let s = `${BASE}${c.txt.replace(' ', '_')}`;
- let top = c.pts === max;
.item(class=top ? 'top' : null)
if top
.stars ★
.content
h3 #{c.txt}
p #{c.inf}
.badge #{c.pts}
img(src=`${s}.jpg` alt=c.txt)
img(src=`${s}.png` aria-hidden='true')
if top
.badge.prize top spot!
$s: .5em; // generic spacing
$c: #ea3c63, #07b3be; // base palette
$x: 37.5%; // how much to cut at the top of images
$y: 1.5em; // how much to cut vertically out of sections
$dot-s: 1.5em;
$dot-o: .5*$dot-s;
@font-face {
font-family: 'cloudy with a chance of love';
src:
url('https://assets.codepen.io/2017/cloudy_with_a_chance_of_love.ttf')
format('truetype')
}
* { margin: 0 }
/* grid 'em all!' */
html, body, div { display: grid }
body {
padding: $s;
font: clamp(.75em, 2.5vw, 1.5em)/ 1.25
cloudy with a chance of love,
playpen sans, sans-serif
}
.wrap {
grid-gap: $s; /* gap between items */
place-self: center; /* in the middle */
max-width: 40em; /* limit width */
filter: drop-shadow(2px 2px 5px #000c);
> * {
--_p: var(--p, 0); /* parity flag */
--bgc: /* alternate background based on parity */
color-mix(in hsl, #{$c} calc(var(--_p)*100%));
/* text RGB channel values for best contrast given
* background luminance computed out of RGB channels */
--chv: calc(255 - round(.72*g + .21*r + .07*b, 255));
background: var(--bgc);
color: /* computed for best contrast */
RGB(from var(--bgc)
var(--chv) var(--chv) var(--chv))
}
}
h2 {
--p: 1; /* parity value for background computation */
padding: $s $s .5*$s;
text-align: center
}
.item {
--q: (1 - var(--_p)); /* complement */
--not-ini: 1; /* not the first */
--not-fin: 1; /* not the last */
/* move it up by how much we cut from the previous one */
margin-top: calc(var(--not-ini)*-1*#{$y});
/* prevent horizontal scrollbar */
overflow: hidden;
/* alignment depending on parity */
text-align: var(--p, right);
clip-path: /* get non-rectangulat shape */
polygon(/* corner positions */
/* top left */
0 calc(var(--_p)*var(--not-ini)*#{$y}),
/* top right */
100% calc(var(--q)*var(--not-ini)*#{$y}),
/* bottom right */
100% calc(100% - var(--q)*var(--not-fin)*#{$y}),
/* bottom left */
0 calc(100% - var(--_p)*var(--not-fin)*#{$y}));
&:nth-of-type(2n) { --p: 1 } /* switch parity */
/* first and last */
&:first-of-type { --not-ini: 0 }
&:last-of-type { --not-fin: 0 }
&.top { /* item with max number of points */
&::before {
/* cover entire parent grid */
grid-area: 1/ 1/ span 1/ span 2;
/* base background for halftone effect */
background:
radial-gradient(#0f0, #000),
radial-gradient(circle at 50% 100%, #f00, #000),
radial-gradient(#00f, #000) #{$dot-o $dot-o};
background-size: #{$dot-s $dot-s}, 100%;
background-blend-mode: lighten;
/* filter responsible for halftone magic */
filter: url(#halftone);
content:''
}
}
}
/* stack stars background (if there is one) as well as
* text content in the same grid cell, either
* on the right or left depending on item parity */
.stars, .content { grid-area: 1/ calc(1 + var(--q)) }
.stars { /* the stars for the top item */
place-self: start start; /* top left corner */
/* for absolutely positioned pseudos */
position: relative;
translate: 20% 20%;
rotate: -9deg;
color: hsla(0, 0%, 100%, .5);
font-size: 5em;
&::before, &::after {
position: absolute;
inset: 0;
content: '★'
}
&::before {
translate: 130%;
scale: .5;
text-shadow: -.5em 1.5em,
2em 0, -1.125em -.375em,
-2em 1.5em, -3em 2.5em
}
&::after {
translate: -50% 50%;
scale: .25;
text-shadow: -.5em -2em,
-.25em -3.5em, 1em -4.5em
}
}
/* place them on top of the stars & halftone dots */
.content, img { z-index: 1 }
.content {
grid-gap: $s;
align-content: center;
/* for parity depending alternating badge position */
justify-items: end;
z-index: 1;
/* vertical paddings depend on whether the item is
* first/ last or not (in the middle) */
padding:
calc(var(--not-ini)*#{$y}) $s
calc(var(--not-fin)*#{$y}) $s
}
h3 {
/* revert that justify-items from parent */
justify-self: stretch;
text-transform: capitalize
}
p { font: 600 1em/ 1.125 ysabeau infant, sans-serif }
.badge {
--c: color-mix(in srgb, #{$c} calc(var(--q)*100%));
/* create party depending alternating x axis alignment */
justify-self: var(--p, start);
width: min-content;
/* move down a bit, but only if not last item */
translate: 0 calc(var(--not-fin)*.25*#{$y});
color: #fff;
-webkit-text-stroke: var(--c) .25em;
paint-order: stroke fill;
line-height: 1;
text-align: center;
text-transform: uppercase;
/* first filter helps avoid Chrome bug */
/* https://issues.chromium.org/u/1/issues/373419871 */
filter: url(#noclip)
drop-shadow(2px 2px 5px #000c);
&::before {
/* accommodate lateral star arms */
padding: 0 .75*$s;
/* semi-transparent box-shadow for #noclip filter
* workaround to prevent Chrome issue */
box-shadow: 0 0 0 $s #0007;
color: var(--c);
font-weight: 900;
-webkit-text-stroke: #fff .15em;
/* paint stoke, then fill on top of it */
paint-order: stroke fill;
content: '☆'
}
&:not(.prize) { /* regular points badges only */
/* this also acts upon text node inside */
place-items: end center;
font-size: 2em;
background: /* "connector" box in the middle */
linear-gradient(var(--c) 0 0)
50%/ 50% 50% no-repeat;
&::before {
/* make star occupy both rows of 1st column */
grid-area: 1/ 1/ span 2;
/* undo alignment from parent,
* which was set for text node */
align-self: center;
font-size: 1.5em
}
&::after {
/* second row, second column */
/* this only leaves 1 cell free for text node */
grid-area: 2/ 2;
/* undo alignment from parent,
* which was set for text node */
align-self: start;
/* minor adjustment */
margin-top: -.25em;
font-size: .375em;
content: 'points'
}
}
}
img, .prize { grid-area: 1/ calc(1 + var(--_p)) }
img {
max-width: 100%;
height: 100%;
object-fit: cover;
/* image alignment depending on parity */
object-position: calc(var(--q)*100%);
&:first-of-type {
clip-path: /* image shape depending on parity */
polygon(
calc(var(--_p)*#{$x}) 0 /* top left */,
calc(100% - var(--q)*#{$x}) 0 /* top right */,
/* bottom points */
100% 100%, 0 100%)
}
}
.prize {
place-self: center end;
z-index: 2;
translate: -100% 50%;
rotate: 9deg;
color: var(--c);
-webkit-text-stroke: .375em #fff;
font-size: .75em;
&::before {
display: grid;
place-content: center;
position: absolute;
inset: 0;
z-index: -1;
translate: 0 20%;
scale: 6;
color: #fbbe09
}
}
Also see: Tab Triggers