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 Skypack, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ES6 import
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.board
- for (var turn = 1; turn <= 9; turn++)
- for (var row = 1; row <= 3; row++)
- for (var column = 1; column <= 3; column++)
input(
type="radio" class=
"turn-" + turn
+ " player-" + ((turn%2-1)*-1+1)
+ " c-" + column
+ " r-" + row
+ (row == column ? row+column == 4 ? " d-1 d-2" : " d-1" : row+column == 4 ? " d-2" : ""))
div.overlay
div.message
@include google-font {
@include google-font(Comfortaa, 400);
}
body {
--grid-size: 20vmin;
--grid-gap: 2vmin;
--background: 255, 255, 255;
--tile-background: #aab2bd;
@include dark { //Dark theme
--background: 60, 60, 60;
--tile-background: #232223;
color:white;
}
display: grid;
place-items: center;
grid-template-rows:1fr;
min-height: 100vh;
background: rgb(var(--background));
font-family: Comfortaa, sans-serif;
transition: 0.25s -0.1s;
.overlay {
z-index: 20;
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
display: none;
place-items: center;
background: rgba(var(--background), 0.4);
.message {
font-size: 8vmin;
}
}
%tie,
%player-1,
%player-2 {
display: grid;
animation: fade-in 0.5s;
@keyframes fade-in {
from {
opacity: 0;
}
to {
opacity: 1;
}
}
}
%tie .message:before {
content: "It's a tie!"; //Because of 9 selectors for a tie this takes precidence so I need to make others important even though they are after.
}
%player-1 .message:before {
content: "Player 1 wins!" !important;
}
%player-2 .message:before {
content: "Player 2 wins!" !important;
}
.board {
display: grid;
grid-template-columns: repeat(3, var(--grid-size));
grid-template-rows: repeat(3, var(--grid-size));
grid-template-areas: "_1 _2 _3" "_4 _5 _6" "_7 _8 _9";
grid-gap: var(--grid-gap);
place-items: center;
input {
// ______
// | ___ \
// | |_/ / __ _ ___ ___
// | ___ \/ _` / __|/ _ \
// | |_/ / (_| \__ \ __/
// \____/ \__,_|___/\___|
position: relative;
appearance: none;
width: 100%;
height: 100%;
border-radius: 10%;
border: 0.25vmin solid var(--background); //Removes aliased looking corners when stacked
background: var(--tile-background);
color: white;
outline: none; //With this many radio buttons tabbing isn't great anyway
transition: 0.25s -0.1s;
&:not(.turn-1) {
//Hide all except turn 1. Cont line 151
display: none;
}
&:before {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
font-family: "Font Awesome 5 Pro";
font-weight: 900;
opacity: 0;
}
// _____ _ _ _ _ _
// / __ \ | (_) | (_) | |
// | / \/ ___ _ __ __| |_| |_ _ ___ _ __ __ _| |
// | | / _ \| '_ \ / _` | | __| |/ _ \| '_ \ / _` | |
// | \__/\ (_) | | | | (_| | | |_| | (_) | | | | (_| | |
// \____/\___/|_| |_|\__,_|_|\__|_|\___/|_| |_|\__,_|_|
//Bring selected forwards - these block tiles under
&:checked {
z-index: 10;
}
//Only have pointer cursor for places people can pick
&:not(:checked):hover {
cursor: pointer;
}
//Show icon if hovered or checked
&:not(:checked):hover:before,
&:checked:before {
opacity: 1;
}
//Colours and icons
&.player-1 {
&:hover {
background: #4a89dc;
}
&:checked {
background: #5d9cec;
}
&:before {
content: "\f00d";
font-size: calc(var(--grid-size) / 1.5);
}
}
&.player-2 {
&:hover {
background: #da4453;
}
&:checked {
background: #ed5565;
}
&:before {
content: "\f111";
font-size: calc(var(--grid-size) / 2);
}
}
//Place tiles in the correct place using css grid
@for $i from 1 through 9 {
&:nth-child(9n + #{$i}) {
grid-area: _#{$i};
}
}
// _ _
// | | (_)
// | | ___ __ _ _ ___
// | | / _ \ / _` | |/ __|
// | |___| (_) | (_| | | (__
// \_____/\___/ \__, |_|\___|
// __/ |
// |___/
//Show turns only if a previous turn has been picked
//Any turn-1:checked tile will show turn-2 tiles
@for $i from 1 through 8 {
&.turn-#{$i}:checked ~ .turn-#{$i + 1} {
display: block;
}
}
//If 9 moves have taken place - any 9 radio buttons checked
&:checked
~ input:checked
~ input:checked
~ input:checked
~ input:checked
~ input:checked
~ input:checked
~ input:checked
~ input:checked
~ .overlay {
@extend %tie;
}
//For both players, check for matching 3 same columns, or 3 same rows, or 3 same diagonals - if there are any 3 by the same player that means they won
@for $player from 1 through 2 {
@for $i from 1 through 3 {
&.player-#{$player}.c-#{$i}:checked
~ input.player-#{$player}.c-#{$i}:checked
~ input.player-#{$player}.c-#{$i}:checked,
&.player-#{$player}.r-#{$i}:checked
~ input.player-#{$player}.r-#{$i}:checked
~ input.player-#{$player}.r-#{$i}:checked,
&.player-#{$player}.d-#{$i}:checked
~ input.player-#{$player}.d-#{$i}:checked
~ input.player-#{$player}.d-#{$i}:checked {
~ .overlay {
@extend %player-#{$player};
}
}
}
}
}
}
}
/*
_____ _ _ _
| ___| | | | | (_)
| |____ ___ __ | | __ _ _ __ __ _| |_ _ ___ _ __
| __\ \/ / '_ \| |/ _` | '_ \ / _` | __| |/ _ \| '_ \
| |___> <| |_) | | (_| | | | | (_| | |_| | (_) | | | |
\____/_/\_\ .__/|_|\__,_|_| |_|\__,_|\__|_|\___/|_| |_|
| |
|_|
There are 81 radio buttons split into 9 groups of 9, each group represents a turn and only a specific player can take their turn.
At the start the first 9 radio buttons are displayed, once it has been checked the next set will be overlayed except the checked one will display on top
`.turn-1:checked~.turn-2` => display
`.turn-2:checked~.turn-3` => display
etc...
Each tile is labelled with their column and row number, and also if they are on one of the 2 diagonals
To check if they have won check to see if the same player has checked all of the same type
`.player-1.c-1:checked~.player-1.c-1:checked~.player-1.c-1:checked .overlay` => player-1 has checked all of column 1 and has won and the overlay is selected
×??
×??
×??
`.player-2.d-2:checked~.player-2.d-2:checked~.player-2.d-2:checked .overlay` => player-2 has checked all of diagonal 2 and has won and the overlay is selected
??•
?•?
•??
If 9 are checked it is at least a tie, with wins overriding it
*/
Also see: Tab Triggers