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.
.game
form
.board
- for(let g = 1; g < 5; g++)
svg.board__line(style=`--x: ${g < 3 ? g : 0}; --y: ${g < 3 ? 0 : g - 2}`)
path(d='M 5 5 L 295 5', fill='none', stroke-width='10', stroke-linecap='round', stroke-dasharray='300', stroke-dashoffset='300')
- for(let x = 0; x < 9; x++)
input(type='checkbox', id=`x--${x}`, style=`--col: ${x % 3}; --row: ${Math.floor(x / 3)}`)
span(style=`--col: ${x % 3}; --row: ${Math.floor(x / 3)}`)
svg.x
path.cross(d='M 20 20 L 80 80', fill='none', stroke-width='10', stroke-linecap='round', stroke-dasharray='100', stroke-dashoffset='100')
path.cross(d='M 80 20 L 20 80', fill='none', stroke-width='10', stroke-linecap='round', stroke-dasharray='100', stroke-dashoffset='100')
input(type='checkbox', id=`o--${x}`, style=`--col: ${x % 3}; --row: ${Math.floor(x / 3)}`)
span(style=`--col: ${x % 3}; --row: ${Math.floor(x / 3)}`)
svg.o
circle.naught(cx='50', cy='50', r='30', fill='none', stroke-width='10', stroke-dasharray='200', stroke-dashoffset='200', stroke-linecap='round')
- for(let y = 0; y < 9; y++)
label(for=`x--${y}`, style=`--col: ${y % 3}; --row: ${Math.floor(y / 3)}`)
label(for=`o--${y}`, style=`--col: ${y % 3}; --row: ${Math.floor(y / 3)}`)
.board__result.board__result--x.result
dl.result__content
dt.result__title Winner!
dd.result__details
svg.x.result__icon
path.cross(d='M 20 20 L 80 80', fill='none', stroke-width='10', stroke-linecap='round', stroke-dasharray='100', stroke-dashoffset='100')
path.cross(d='M 80 20 L 20 80', fill='none', stroke-width='10', stroke-linecap='round', stroke-dasharray='100', stroke-dashoffset='100')
button.result__reset(type='reset') Play again
//- .result__shield
- for (let c = 0; c < 30; c++)
.result__firework-icon(style=`--y: ${Math.floor(Math.random() * 300) + 200}; --r: ${Math.floor(Math.random() * 360)}`) 🎉
.board__result.board__result--o.result
dl.result__content
dt.result__title Winner!
dd.result__details
svg.o.result__icon
circle.naught(cx='50', cy='50', r='30', fill='none', stroke-width='10', stroke-dasharray='200', stroke-dashoffset='200', stroke-linecap='round')
button.result__reset(type='reset') Play again
//- .result__shield
- for (let c = 0; c < 30; c++)
.result__firework-icon(style=`--y: ${Math.floor(Math.random() * 300) + 200}; --r: ${Math.floor(Math.random() * 360)}`) 🎉
.board__result.board__result--draw
dl.result__content
dt.result__title Draw!
dd.result__details
.result__emoji 😭
button.result__reset(type='reset') Play again
*
box-sizing border-box
$bg = #1f3a93
$naught = #f62459
$cross = #36dbd7
$line = #fafafa
$size = 300px
$animDuration = .5s
body
html
align-items center
background-color $bg
color #fafafa
display flex
flex-direction column
font-family 'Arial', sans-serif
justify-content center
height 100vh
width 100vw
overflow hidden
position relative
svg
height ($size / 3)
width ($size / 3)
// hide all inputs from being visible
input
opacity 0
label
cursor pointer
height ($size / 3)
width ($size / 3)
z-index 2
// position each cells elements using css variables
input
span
label
left "calc(var(--col) * %s)" % ($size / 3)
position absolute
top "calc(var(--row) * %s)" % ($size / 3)
// give svg elements animation properties
circle
path
animation-fill-mode forwards
animation-name draw
circle
animation-duration $animDuration
stroke $naught
path
animation-duration ($animDuration / 2)
stroke $cross
button
cursor pointer
position absolute
bottom 20px
padding 8px 24px
border-radius 4px
border 0
background $bg
font-size .75rem
color #fafafa
filter drop-shadow(0 5px 5px black)
transition transform .1s, filter .1s
&:hover
filter drop-shadow(0 5px 4px black)
transform translateY(1px)
&:active
transform translateY(5px)
filter drop-shadow(0 0 0 transparent)
// span displays which player is occupying a cell
span
display none
transform translate3d(0, 0, 3px)
// only once an input is checked for a cell, show the appropriate span
input:checked + span
display block
.o
transform rotateY(180deg) rotate(-35deg)
.x
path:nth-of-type(2)
animation-delay ($animDuration / 2)
.board
height $size
left 50%
position absolute
top 50%
transform translate(-50%, -50%)
transform-style preserve-3d
width $size
&__line
height 10px
left "calc(var(--x) * %s)" % ($size / 3)
position absolute
top "calc(var(--y) * %s)" % ($size / 3)
width $size
path
stroke $line
&:nth-of-type(1)
&:nth-of-type(2)
transform rotate(90deg) translate(-5px, 0)
transform-origin left center
&:nth-of-type(3)
&:nth-of-type(4)
transform translate(0, -50%)
for $line in (1..4)
&:nth-of-type({$line}) path
animation-delay ($line * ($animDuration / 2))
&__result
animation fadeBg .25s $animDuration
animation-fill-mode backwards
background rgba(31, 58, 147, 1)
display none
height 100%
left 0
margin 0
padding 0
position absolute
top 0
transform translate3d(0, 0, 4px)
width 100%
.result
&__content
align-items center
animation flyIn .25s (.25s + $animDuration)
animation-fill-mode backwards
background #fafafa
border-radius 10px
color $bg
display flex
filter drop-shadow(0 10px 10px black)
flex-direction column
height 100%
justify-content center
left 0
margin 0
position absolute
top 0
width 100%
z-index 3
&__title
font-size 1.5rem
font-weight bolder
text-transform uppercase
&__details
margin 0
&__shield
animation fadeBg .25s $animDuration
animation-fill-mode backwards
background rgba(31, 58, 147, 1)
height 100%
left 0
position absolute
top 0
width 100%
z-index 2
&__emoji
font-size 5rem
&__icon
path:nth-of-type(1)
animation-delay (.5s + $animDuration)
path:nth-of-type(2)
animation-delay (0.9s + $animDuration)
circle
animation-delay (.5s + $animDuration)
&__firework-icon
animation fly .75s (.5s + $animDuration)
animation-fill-mode backwards
animation-timing-function cubic-bezier(0,.64,.25,1.01)
font-size 2rem
height 2rem
left 50%
margin-left -1rem
margin-top -1rem
opacity 0
position absolute
top 50%
transform rotate(calc(var(--r) * 1deg)) translateY(calc(var(--y) * -1px))
width 2rem
// animations
@keyframes draw
to
stroke-dashoffset 0
@keyframes fadeBg
from
background rgba(31, 58, 147, 0)
@keyframes fly
0%
opacity 0
transform translateY(0)
5%, 50%
opacity 1
100%
opacity 0
@keyframes flyIn
from
transform translateY(100%) scale(0)
// there are 9 moves to make alternating between even and odd labels showing
:checked ~ label:nth-of-type(odd)
:checked ~ :checked ~ :checked ~ label:nth-of-type(odd)
:checked ~ :checked ~ :checked ~ :checked ~ :checked ~ label:nth-of-type(odd)
:checked ~ :checked ~ :checked ~ :checked ~ :checked ~ :checked ~ :checked ~ label:nth-of-type(odd)
:checked ~ :checked ~ label:nth-of-type(even)
:checked ~ :checked ~ :checked ~ :checked ~ label:nth-of-type(even)
:checked ~ :checked ~ :checked ~ :checked ~ :checked ~ :checked ~ label:nth-of-type(even)
:checked ~ :checked ~ :checked ~ :checked ~ :checked ~ :checked ~ :checked ~ :checked ~ label:nth-of-type(even)
transform translate3d(0, 0, 2px)
:checked ~ label:nth-of-type(even)
:checked ~ :checked ~ :checked ~ label:nth-of-type(even)
:checked ~ :checked ~ :checked ~ :checked ~ :checked ~ label:nth-of-type(even)
:checked ~ :checked ~ :checked ~ :checked ~ :checked ~ :checked ~ :checked ~ label:nth-of-type(even)
:checked ~ :checked ~ label:nth-of-type(odd)
:checked ~ :checked ~ :checked ~ :checked ~ label:nth-of-type(odd)
:checked ~ :checked ~ :checked ~ :checked ~ :checked ~ :checked ~ label:nth-of-type(odd)
:checked ~ :checked ~ :checked ~ :checked ~ :checked ~ :checked ~ :checked ~ :checked ~ label:nth-of-type(odd)
transform translate3d(0, 0, -1px)
:checked ~ :checked ~ :checked ~ :checked ~ :checked ~ :checked ~ :checked ~ :checked ~ :checked
// if the last move is played and there isn't a winner, show a draw
~ .board__result--draw
display block
// winning combos
#x--0:checked ~ #x--1:checked ~ #x--2:checked ~ .board__result--x
#x--3:checked ~ #x--4:checked ~ #x--5:checked ~ .board__result--x
#x--6:checked ~ #x--7:checked ~ #x--8:checked ~ .board__result--x
#x--0:checked ~ #x--3:checked ~ #x--6:checked ~ .board__result--x
#x--1:checked ~ #x--4:checked ~ #x--7:checked ~ .board__result--x
#x--2:checked ~ #x--5:checked ~ #x--8:checked ~ .board__result--x
#x--0:checked ~ #x--4:checked ~ #x--8:checked ~ .board__result--x
#x--2:checked ~ #x--4:checked ~ #x--6:checked ~ .board__result--x
#o--0:checked ~ #o--1:checked ~ #o--2:checked ~ .board__result--o
#o--3:checked ~ #o--4:checked ~ #o--5:checked ~ .board__result--o
#o--6:checked ~ #o--7:checked ~ #o--8:checked ~ .board__result--o
#o--0:checked ~ #o--3:checked ~ #o--6:checked ~ .board__result--o
#o--1:checked ~ #o--4:checked ~ #o--7:checked ~ .board__result--o
#o--2:checked ~ #o--5:checked ~ #o--8:checked ~ .board__result--o
#o--0:checked ~ #o--4:checked ~ #o--8:checked ~ .board__result--o
#o--2:checked ~ #o--4:checked ~ #o--6:checked ~ .board__result--o
display block
// if there's a winner on the last move, don't show a draw!
~ .board__result--draw
display none
Also see: Tab Triggers