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.
.birthday-button
button.birthday-button__button
svg.birthday-button__cake(xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink' viewBox='0 0 76.609 47.643')
defs
g#candle(transform='translate(-48.82 -137.74)')
rect(ry='.567' y='147.888' x='72.548' height='12.001' width='4.252' fill='#ececec')
g.candle__flame
path.candle__flame-outer(d='M75.672 145.92c.608 1.207.086 2.224-.749 2.224s-1.512-.037-1.512-1.388.558-3.566 1.98-3.973c1.007-.288-.855.884.281 3.138z' fill='#f60')
path.candle__flame-inner(d='M75.081 146.449c.41.51.266 1.043-.13 1.128-.394.084-.682-.228-.822-.866 0 0-.13-1.382.502-1.719.447-.238-.315.505.45 1.457z' fill='#ff0')
g(style='display: none')
path#flame-outer(d='M73.676 145.92c-.609 1.207-.086 2.224.749 2.224s1.511-.037 1.511-1.388-.557-3.566-1.98-3.973c-1.006-.288.856.884-.28 3.138z' fill='#f60')
path#flame-inner(d='M74.267 146.449c-.41.51-.266 1.043.129 1.128.395.084.683-.228.823-.866 0 0 .13-1.382-.502-1.719-.447-.238.315.505-.45 1.457z' fill='#ff0')
clipPath#face-clip
rect(transform='translate(-48.82 -138.799)' width='63.5' height='26.458' x='55.374' y='159.984' ry='3.402')
clipPath#sprinkle-clip
rect(width='63.5' height='26.458' x='55.374' y='159.984' ry='3.402')
clipPath#frosting-clip
path(d='m 58.311339,159.19367 c -1.915439,0 -3.331523,2.04311 -3.986711,3.74446 -0.778328,2.02111 -0.761971,4.73695 0.529167,6.47582 0.861406,1.16012 2.614079,1.3182 4.033715,1.5875 1.321013,0.25059 2.689143,0 4.033714,0 1.344572,0 2.689143,0 4.033715,0 1.344572,0 2.689143,0 4.033715,0 1.344571,0 2.689143,0 4.033714,0 1.344572,0 2.689143,0 4.033715,0 1.344572,0 2.689143,0 4.033715,0 1.344572,0 2.689143,0 4.033715,0 1.344571,0 2.689143,0 4.033714,0 1.344572,0 2.689143,0 4.033715,0 1.344572,0 2.689143,0 4.033715,0 1.344573,0 2.689143,0 4.033713,0 1.34457,0 2.68915,0 4.03372,0 1.34457,0 2.68914,0 4.03371,0 1.34457,0 2.71271,0.25059 4.03372,0 1.41963,-0.2693 3.17231,-0.42738 4.03371,-1.5875 1.29114,-1.73887 1.3075,-4.45471 0.52917,-6.47582 -0.65519,-1.70135 -2.07128,-3.74446 -3.98672,-3.74446 z' style='fill:#ff0041;fill-opacity:0.53424659;fill-rule:nonzero;stroke:none;stroke-width:0.90433162;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1')
g#sprinkles(transform='translate(63.93)')
rect.cake__sprinkle(transform='rotate(-44.064)' ry='.567' y='167.592' x='-91.837' height='1.228' width='3.307')
rect.cake__sprinkle(transform='rotate(127.01)' ry='.567' y='165.35' x='76.642' height='1.228' width='3.307')
rect.cake__sprinkle(transform='rotate(85.232)' width='3.307' height='1.228' x='182.37' y='3.842' ry='.567')
rect.cake__sprinkle(transform='rotate(136.794)' ry='.567' y='-137.013' x='122.349' height='1.228' width='3.307')
rect.cake__sprinkle(transform='rotate(-28.569)' width='3.307' height='1.228' x='-90.997' y='160.466' ry='.567')
rect.cake__sprinkle(transform='rotate(-134.37)' ry='.567' y='-124.272' x='-138.396' height='1.228' width='3.307')
rect.cake__sprinkle(transform='rotate(-37.152)' width='3.307' height='1.228' x='-101.107' y='155.28' ry='.567')
rect.cake__sprinkle(transform='rotate(-72.723)' ry='.567' y='77.322' x='-169.664' height='1.228' width='3.307')
rect.cake__sprinkle(transform='rotate(36.566)' width='3.307' height='1.228' x='124.338' y='135.065' ry='.567')
rect.cake__sprinkle(transform='rotate(-45)' ry='.567' y='150.009' x='-111.692' height='1.228' width='3.307')
rect.cake__sprinkle(transform='rotate(30)' width='3.307' height='1.228' x='119.879' y='141.701' ry='.567')
rect.cake__sprinkle(transform='rotate(-55.914)' ry='.567' y='139.024' x='-129.555' height='1.228' width='3.307')
rect.cake__sprinkle(transform='rotate(-30)' width='3.307' height='1.228' x='-60.133' y='178.671' ry='.567')
rect.cake__sprinkle(transform='rotate(45)' ry='.567' y='95.647' x='162.293' height='1.228' width='3.307')
rect.cake__sprinkle(transform='rotate(45)' ry='.567' y='133.647' x='124.293' height='1.228' width='3.307')
path#frosting(d='M58.311 159.194c-1.915 0-3.283-.087-3.986 1.098-.392.661-.073 1.766.529 2.243 1.054.834 2.689 0 4.034 0H115.36c1.344 0 2.979.834 4.033 0 .602-.477.921-1.582.53-2.243-.703-1.185-2.072-1.098-3.987-1.098z')
path.cake__frosting.cake__frosting--end(d='M58.311 159.194c-1.915 0-3.354 2.034-3.986 3.744-.57 1.537-.953 4.189.529 4.888 1.454.687 2.425-2.645 4.034-2.645 1.608 0 2.425 2.645 4.033 2.645 1.608 0 2.426-2.645 4.034-2.645 1.608 0 2.426 2.645 4.034 2.645 1.608 0 2.425-2.645 4.033-2.645 1.608 0 2.426 2.645 4.034 2.645 1.608 0 2.426-2.645 4.034-2.645 1.608 0 2.426 2.645 4.034 2.645 1.608 0 2.425-2.645 4.033-2.645 1.608 0 2.426 2.645 4.034 2.645 1.608 0 2.436-2.83 4.034-2.645 1.936.222 2.084 4.233 4.033 4.233 1.95 0 2.098-4.01 4.034-4.233 1.598-.184 2.426 2.645 4.034 2.645 1.608 0 2.426-2.645 4.034-2.645 1.608 0 2.58 3.332 4.033 2.645 1.482-.7 1.098-3.35.53-4.888-.633-1.71-2.072-3.744-3.987-3.744z' )
g.cake-decoration(transform='translate(-48.82 -138.799)')
g.cake__frosting-group(clip-path="url(#frosting-clip)")
g.cake__frosting.cake__frosting--start
use(xlink:href="#frosting")
g.cake__frosting.cake__frosting--duplicate
rect.cake__frosting-patch(width="20" height="5" x="110" y="159.25")
use(xlink:href="#frosting")
g.cake__sprinkles-group(clip-path="url(#sprinkle-clip)")
g.cake__sprinkles.cake__sprinkles--initial
use(xlink:href="#sprinkles")
g.cake__sprinkles.cake__sprinkles--duplicate
use(xlink:href="#sprinkles")
g(clip-path="url(#face-clip)")
g.cake__face(transform='translate(-48.82 -138.799)')
g.cake__eyes
g.cake__eye(transform='matrix(1.38031 0 0 1.38031 34.723 -33.58)')
circle.cake__eye-body(cx='29.86' cy='149.022' r='2.457')
circle.cake__eye-pupil(cx='28.773' cy='148.162' r='.756')
g.cake__eye(transform='matrix(1.38031 0 0 1.38031 57.092 -33.58)')
circle.cake__eye-body(r='2.457' cy='149.022' cx='29.86')
circle.cake__eye-pupil(r='.756' cy='148.162' cx='28.773')
g.cake__mouth
path.cake__mouth-opening(d='M83.607 174.436a3.652 3.652 0 003.518 2.674 3.652 3.652 0 003.515-2.674z' stroke-width='1.1801650499999998' stroke-linecap='round' stroke-linejoin='round')
path.cake__tongue(d='M88.836 175.492a3.113 2.329 0 00-2.869 1.425 3.652 3.652 0 001.158.192 3.652 3.652 0 002.899-1.44 3.113 2.329 0 00-1.188-.177z')
g.cake__face--straining(transform='translate(-48.82 -138.799)')
path(d='M100.673 173.886l-5.248-2.073 5.713-1.466M73.574 173.886l5.248-2.073-5.713-1.466' fill='none' stroke='#000' stroke-width='1.01590816' stroke-linecap='round' stroke-linejoin='round')
path(d='M85.016 175.773h4.216' fill='none' stroke='#000' stroke-width='.86683468' stroke-linecap='round')
g.cake__candles
g.cake__candle
use(xlink:href="#candle")
g.cake__candle
use(xlink:href="#candle" transform='translate(25 0)')
g.cake__candle
use(xlink:href="#candle" transform='translate(12 0.5)')
span.birthday-button__text(data-splitting='') Happy Birthday!
input#volume(type='checkbox')
label(for='volume', title='Toggle sound')
//- On
svg(viewBox="0 0 24 24")
path(d="M14,3.23V5.29C16.89,6.15 19,8.83 19,12C19,15.17 16.89,17.84 14,18.7V20.77C18,19.86 21,16.28 21,12C21,7.72 18,4.14 14,3.23M16.5,12C16.5,10.23 15.5,8.71 14,7.97V16C15.5,15.29 16.5,13.76 16.5,12M3,9V15H7L12,20V4L7,9H3Z")
//- Off
svg(viewBox="0 0 24 24")
path(d="M12,4L9.91,6.09L12,8.18M4.27,3L3,4.27L7.73,9H3V15H7L12,20V13.27L16.25,17.53C15.58,18.04 14.83,18.46 14,18.7V20.77C15.38,20.45 16.63,19.82 17.68,18.96L19.73,21L21,19.73L12,10.73M19,12C19,12.94 18.8,13.82 18.46,14.64L19.97,16.15C20.62,14.91 21,13.5 21,12C21,7.72 18,4.14 14,3.23V5.29C16.89,6.15 19,8.83 19,12M16.5,12C16.5,10.23 15.5,8.71 14,7.97V10.18L16.45,12.63C16.5,12.43 16.5,12.21 16.5,12Z")
*
box-sizing border-box
:root
--tongue hsl(4, 80%, 42%)
--pupil hsl(0, 0%, 100%)
--icing hsl(0, 0%, 95%)
--stroke hsl(0, 0%, 10%)
body
--frosting 'hsl(%s, 100%, 55%)' % var(--frosting-hue, 0)
--lightness 50
--flame 0
--glow-hue 40
--glow-saturation 50
--glow-lightness 35
--glow-alpha 0.4
--transparency-alpha 0
--glow 'hsla(%s, %s, %s, %s)' % (var(--glow-hue) calc(var(--glow-saturation) * 1%) calc(var(--glow-lightness) * 1%) var(--glow-alpha))
--transparent 'hsla(0, 0%, 0%, %s)' % var(--transparency-alpha)
display flex
overflow hidden
align-items center
justify-content center
min-height 100vh
background 'hsl(180, 30%, %s)' % calc(var(--lightness, 50) * 1%)
.whitespace
width 7px
.birthday-button
position relative
transform scale(0.6)
cursor pointer
&__text
position absolute
top 0
left 0
height 100%
width 100%
display flex
text-align center
align-items center
justify-content center
font-size 1.5rem
font-family sans-serif
font-weight bold
.char
display inline-block
color 'hsl(%s, %s, %s)' % (var(--hue, 0) calc(var(--char-sat, 0) * 1%) calc(var(--char-light, 0) * 1%))
&:before
content ''
position absolute
height 240px
width 240px
background radial-gradient(var(--glow), var(--transparent) 30%), radial-gradient(var(--glow), var(--transparent) 50%)
border-radius 50%
top 50%
left 50%
transform translate(-50%, -70%) scale(calc(var(--flame) * 3))
z-index 2
filter blur(15px)
opacity var(--flame)
&:after
content ''
height 30px
width 400px
filter blur(15px)
position absolute
background radial-gradient(var(--glow), var(--transparent) 20%), radial-gradient(var(--glow), var(--transparent))
top 100%
left 50%
transform translate(-50%, -50%) scale(calc(var(--flame) * 1))
opacity var(--flame)
&__button
width 240px
height 100px
position relative
border 0
background var(--icing)
border-radius 14px
cursor pointer
&__cake
position absolute
bottom 0
left 50%
transform translate(-50%, 0)
width 289.5px
display none
.cake
&__eye-body
fill var(--stroke)
&__eye-pupil
fill var(--pupil)
&__tongue
fill var(--tongue)
&__mouth-opening
fill var(--stroke)
stroke var(--stroke)
&__sprinkle
--sprinkle 'hsl(%s, 100%, 75%)' % var(--sprinkle-hue, 0)
fill var(--sprinkle)
&__frosting
&--start
fill var(--frosting)
&--duplicate
fill var(--frosting)
.candle__flame
opacity var(--flame, 0)
&-outer
fill hsl(22, 100%, 56%)
&-inner
fill hsl(50, 85%, 52%)
.face__stroke
stroke var(--stroke)
.cake__candle
&:nth-of-type(1)
--flame 0
&:nth-of-type(2)
--flame 0
&:nth-of-type(3)
--flame 0
label
height 44px
width 44px
position fixed
bottom 1rem
right 1rem
cursor pointer
& > svg
position absolute
height 100%
width 100%
top 0
left 0
path
fill hsl(180, 20%, 35%)
svg:nth-of-type(1)
display none
[type='checkbox']
// opacity 0
height 0
width 0
position absolute
top 0
left 100%
:checked ~ label
svg:nth-of-type(1)
display block
svg:nth-of-type(2)
display none
const {
gsap,
gsap: { to, timeline, set, delayedCall },
Splitting,
} = window
Splitting()
const BTN = document.querySelector('.birthday-button__button')
const SOUNDS = {
CHEER: new Audio(
'https://s3-us-west-2.amazonaws.com/s.cdpn.io/605876/cheer.mp3'
),
MATCH: new Audio(
'https://s3-us-west-2.amazonaws.com/s.cdpn.io/605876/match-strike-trimmed.mp3'
),
TUNE: new Audio(
'https://s3-us-west-2.amazonaws.com/s.cdpn.io/605876/happy-birthday-trimmed.mp3'
),
ON: new Audio('https://assets.codepen.io/605876/switch-on.mp3'),
BLOW: new Audio(
'https://s3-us-west-2.amazonaws.com/s.cdpn.io/605876/blow-out.mp3'
),
POP: new Audio(
'https://s3-us-west-2.amazonaws.com/s.cdpn.io/605876/pop-trimmed.mp3'
),
HORN: new Audio(
'https://s3-us-west-2.amazonaws.com/s.cdpn.io/605876/horn.mp3'
),
}
const EYES = document.querySelector('.cake__eyes')
const BLINK = eyes => {
gsap.set(eyes, { scaleY: 1 })
if (eyes.BLINK_TL) eyes.BLINK_TL.kill()
eyes.BLINK_TL = new gsap.timeline({
delay: Math.floor(Math.random() * 4) + 1,
onComplete: () => BLINK(eyes),
})
eyes.BLINK_TL.to(eyes, {
duration: 0.05,
transformOrigin: '50% 50%',
scaleY: 0,
yoyo: true,
repeat: 1,
})
}
BLINK(EYES)
const FROSTING_TL = () =>
timeline()
.to(
'#frosting',
{
scaleX: 1.015,
duration: 0.25,
},
0
)
.to(
'#frosting',
{
scaleY: 1,
duration: 1,
},
0
)
.to(
'#frosting',
{
duration: 1,
morphSVG: '.cake__frosting--end',
},
0
)
// Extract to sprinkle
const SPRINKLES_TL = () =>
timeline().to('.cake__sprinkle', { scale: 1, duration: 0.06, stagger: 0.02 })
// Extract out to your own timeline
const SPIN_TL = () =>
timeline()
.set('.cake__frosting-patch', { display: 'block' })
.to(
['.cake__frosting--duplicate', '.cake__sprinkles--duplicate'],
{ x: 0, duration: 1 },
0
)
.to(
['.cake__frosting--start', '.cake__sprinkles--initial'],
{ x: 65, duration: 1 },
0
)
.to('.cake__face', { duration: 1, x: -48.82 }, 0)
const flickerSpeed = 0.1
const FLICKER_TL = timeline()
.to('.candle__flame-outer', {
duration: flickerSpeed,
repeat: -1,
yoyo: true,
morphSVG: '#flame-outer',
})
.to(
'.candle__flame-inner',
{
duration: flickerSpeed,
repeat: -1,
yoyo: true,
morphSVG: '#flame-inner',
},
0
)
const SHAKE_TL = () =>
timeline({ delay: 0.5 })
.set('.cake__face', { display: 'none' })
.set('.cake__face--straining', { display: 'block' })
.to(
'.birthday-button',
{
onComplete: () => {
set('.cake__face--straining', { display: 'none' })
set('.cake__face', { display: 'block' })
},
x: 1,
y: 1,
repeat: 13,
duration: 0.1,
},
0
)
.to(
'.cake__candle',
{
onComplete: () => {
FLICKER_TL.play()
},
onStart: () => {
SOUNDS.POP.play()
delayedCall(0.2, () => SOUNDS.POP.play())
delayedCall(0.4, () => SOUNDS.POP.play())
},
ease: 'Elastic.easeOut',
duration: 0.2,
stagger: 0.2,
scaleY: 1,
},
0.2
)
const FLAME_TL = () =>
timeline({})
.to('.cake__candle', { '--flame': 1, stagger: 0.2, duration: 0.1 })
.to('body', { '--flame': 1, '--lightness': 5, duration: 0.2, delay: 0.2 })
const LIGHTS_OUT = () =>
timeline().to('body', {
onStart: () => SOUNDS.BLOW.play(),
delay: 0.5,
'--lightness': 0,
duration: 0.1,
'--glow-saturation': 0,
'--glow-lightness': 0,
'--glow-alpha': 1,
'--transparency-alpha': 1,
})
const RESET = () => {
set('.char', {
'--hue': () => Math.random() * 360,
'--char-sat': 0,
'--char-light': 0,
x: 0,
y: 0,
opacity: 1,
})
set('body', {
'--frosting-hue': Math.random() * 360,
'--glow-saturation': 50,
'--glow-lightness': 35,
'--glow-alpha': 0.4,
'--transparency-alpha': 0,
'--flame': 0,
})
set('.cake__candle', { '--flame': 0 })
to('body', {
'--lightness': 50,
duration: 0.25,
})
// SET THESE
set('.cake__frosting--end', { opacity: 0 })
set('#frosting', {
transformOrigin: '50% 10%',
scaleX: 0,
scaleY: 0,
})
set('.cake__frosting-patch', { display: 'none' })
set(['.cake__frosting--duplicate', '.cake__sprinkles--duplicate'], { x: -65 })
set('.cake__face', { x: -110 })
set('.cake__face--straining', { display: 'none' })
set('.cake__sprinkle', {
'--sprinkle-hue': () => Math.random() * 360,
scale: 0,
transformOrigin: '50% 50%',
})
set('.birthday-button', { scale: 0.6, x: 0, y: 0 })
set('.birthday-button__cake', { display: 'none' })
set('.cake__candle', { scaleY: 0, transformOrigin: '50% 100%' })
}
RESET()
const MASTER_TL = timeline({
onStart: () => {
SOUNDS.ON.play()
},
onComplete: () => {
delayedCall(2, RESET)
BTN.removeAttribute('disabled')
},
paused: true,
})
.set('.birthday-button__cake', { display: 'block' })
.to('.birthday-button', {
onStart: () => SOUNDS.CHEER.play(),
scale: 1,
duration: 0.2,
})
.to('.char', { '--char-sat': 70, '--char-light': 65, duration: 0.2 }, 0)
.to('.char', {
onStart: () => SOUNDS.HORN.play(),
delay: 0.75,
y: () => gsap.utils.random(-100, -200),
x: () => gsap.utils.random(-50, 50),
duration: () => gsap.utils.random(0.5, 1),
})
.to('.char', { opacity: 0, duration: 0.25 }, '>-0.5')
.add(FROSTING_TL())
.add(SPRINKLES_TL())
.add(SPIN_TL())
.add(SHAKE_TL())
.add(FLAME_TL(), 'FLAME_ON')
.add(LIGHTS_OUT(), 'LIGHTS_OUT')
SOUNDS.TUNE.onended = SOUNDS.MATCH.onended = () => MASTER_TL.play()
MASTER_TL.addPause('FLAME_ON', () => SOUNDS.MATCH.play())
MASTER_TL.addPause('LIGHTS_OUT', () => SOUNDS.TUNE.play())
BTN.addEventListener('click', () => {
BTN.setAttribute('disabled', true)
MASTER_TL.restart()
})
SOUNDS.TUNE.muted = SOUNDS.MATCH.muted = SOUNDS.HORN.muted = SOUNDS.POP.muted = SOUNDS.CHEER.muted = SOUNDS.BLOW.muted = SOUNDS.ON.muted = true
const toggleAudio = () => {
SOUNDS.TUNE.muted = SOUNDS.MATCH.muted = SOUNDS.POP.muted = SOUNDS.HORN.muted = SOUNDS.CHEER.muted = SOUNDS.BLOW.muted = SOUNDS.ON.muted = !SOUNDS
.BLOW.muted
}
document.querySelector('#volume').addEventListener('input', toggleAudio)
Also see: Tab Triggers