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 class="container">
<div class="board">
<div class="square" id="0"></div>
<div class="square" id="1"></div>
<div class="square" id="2"></div>
<div class="square" id="3"></div>
<div class="square" id="4"></div>
<div class="square" id="5"></div>
<div class="square" id="6"></div>
<div class="square" id="7"></div>
<div class="square" id="8"></div>
</div>
<footer>
<span>For FreeCodeCamp by <a href="https://github.com/alexisnotalexa">@alexisnotalexa</a><span>
</footer>
</div>
<div class="modal start-game">
<span>Choose wisely:</span>
<div class="modal__selector">
<button class="selector" id="x">
<i class="fa-inverse fa fa-times fa-5x"></i>
</button>
<button class="selector" id="o">
<i class="fa-inverse fa fa-circle-o fa-5x"></i>
</button>
</div>
</div>
<div class="modal end-game" style="display:none;">
<span id="emoji"></span>
<span id="winner"></span>
<button id="restart-game">Play Again</button>
</div>
<div class="modal-backdrop"></div>
@import url('https://fonts.googleapis.com/css?family=Open+Sans:300,700');
html,
body {
margin: 0;
padding: 0;
background-color: #c3e9e2;
font-family: 'Open Sans', sans-serif;
}
a, a:link, a:hover,
a:visited {
color: white;
font-size: 11px;
letter-spacing: 1px;
}
i {
text-shadow: 0 0 15px rgba(0,0,0,0.1);
}
footer {
color: white;
font-size: 11px;
text-align: center;
margin: .5rem;
}
.board {
width: 500px;
height: 500px;
background-color: white;
border-radius: 1rem;
display: grid;
grid-template: repeat(3, 1fr) / repeat(3, 1fr);
grid-gap: .5rem;
padding: 1rem;
}
.container {
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}
.square {
background-color: gainsboro;
border-radius: .5rem;
display: flex;
justify-content: center;
align-items: center;
transition: background-color .3s linear;
}
.modal {
background-color: white;
border-radius: .5rem;
box-shadow: 0 0 20px rgba(0,0,0,0.2);
position: absolute;
z-index: 2;
padding: 3rem;
width: 200px;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
display: flex;
flex-direction: column;
text-align: center;
}
.modal > span {
font-weight: 700;
text-transform: uppercase;
font-size: 1.5rem;
}
.modal-backdrop {
background-color: rgba(0,0,0,0.2);
position: absolute;
z-index: 1;
width: 100%;
height: 100%;
opacity: 1;
animation: fadein .5s ease-in;
}
.modal__selector {
display: flex;
justify-content: space-around;
padding-top: 1rem;
}
.selector, .selector:focus {
border: 0;
border-radius: .5rem;
cursor: pointer;
width: 5rem;
height: 5rem;
outline: none;
}
.start-game {
animation: slide-in .5s ease-in-out 1;
}
.symbol {
animation: pop-in .5s ease-in-out 1;
}
#restart-game, #restart-game:focus {
background-color: gold;
border: 0;
border-radius: 1rem;
box-shadow: 0px 2px 0 rgba(0,0,0,0.05);
font-size: .5rem;
padding: .5rem;
text-transform: uppercase;
outline: none;
}
#restart-game:hover {
background-color: #f2ce0d;
}
#winner {
margin-bottom: 1.5rem;
}
#emoji {
font-size: 3rem;
animation: wave 1s ease-in-out 1;
}
#x {
background-color: coral;
transition: background-color 0.5s ease;
}
#o {
background-color: cornflowerblue;
transition: background-color 0.5s ease;
}
#x:hover { background-color: #e7593f; }
#o:hover { background-color: #5984d3; }
.red { background-color: coral; }
.blue { background-color: cornflowerblue; }
/*CSS ANIMATIONS*/
@keyframes pop-in {
0% {transform: scale(0);}
75% {transform: scale(1.3);}
100% {transform: scale(1);}
}
@keyframes fadein {
from { opacity: 0; }
to { opacity: 1; }
}
@keyframes slide-in {
0% { margin-top: 30%; }
25% { margin-top: -5%; }
75% { margin-top: 2%; }
100% { margin-top: 0; }
}
@keyframes wave {
0% { transform: rotate(0deg); }
25% { transform: rotate(-20deg); }
50% { transform: rotate(0deg); }
75% { transform: rotate(20deg); }
100% { transform: rotate(0deg); }
}
/*
Tic Tac Toe Game by Alexis Okamura
*/
var tictactoe = (function() {
var compMove; // stores the index of the computer's move
var player = { id: 1 }, comp = { id: -1 };
var board = [0, 0, 0, 0, 0, 0, 0, 0, 0];
var winningLines = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8],
[0, 4, 8],
[2, 4, 6]
];
/* Assigns player + comp's symbol according to what symbol they click */
$('#x').on('click', function() {
player.symbol = '<i class="fa-inverse fa fa-times fa-5x symbol"></i>';
comp.symbol = '<i class="fa-inverse fa fa-circle-o fa-5x symbol"></i>';
player.color = 'red';
comp.color = 'blue';
$('.modal-backdrop').hide();
$('.start-game').hide();
startGame();
});
/* Assigns player + comp's symbol according to what symbol they click */
$('#o').on('click', function() {
player.symbol = '<i class="fa-inverse fa fa-circle-o fa-5x symbol"></i>';
comp.symbol = '<i class="fa-inverse fa fa-times fa-5x symbol"></i>';
player.color = 'blue';
comp.color = 'red';
$('.modal-backdrop').hide();
$('.start-game').hide();
startGame();
});
function startGame() {
resetGame();
playersTurn();
}
/* Checks the current state of the board passed in */
function check(state) {
// counts how many moves have been played already
var moves = state.reduce(function(prev, cur) {
return Math.abs(prev) + Math.abs(cur);
});
// totals up and finds the current highest score
var result = parseInt(winningLines.map(function(lines) {
return lines.map(function(line) { // get value in each square
return state[line];
}).reduce(function(prev, cur) { // add up values on each line
return prev + cur;
});
}).filter(function(total) { // find + return winning score
return Math.abs(total) === 3;
}).join());
// checks moves + result
if(result === 3) {
return 'win';
} else if(result === -3) {
return 'lose';
} else if(moves === 9) {
return 'tie';
} else {
return false;
}
}
/* Displays the outcome of the game */
function gameOver(winner) {
if(winner === 'win') {
$('#winner').html('You won!');
$('#emoji').html('🎉');
} else if(winner === 'lose') {
$('#winner').html('You lost!');
$('#emoji').html('👎');
} else {
$('#winner').html('It was a tie');
$('#emoji').text('🤷');
}
$('.modal-backdrop').show();
$('.end-game').show();
$('#restart-game').on('click', function() {
$('.modal-backdrop').hide();
$('.modal').hide();
startGame();
});
}
/* Resets the board to its original state */
function resetGame() {
board = [0, 0, 0, 0, 0, 0, 0, 0, 0];
// clears anything in each square
$('.square').each(function() {
$(this).empty();
$(this).removeClass('blue red');
});
}
function playersTurn() {
$('.square').on('click', function(e) {
if(board[e.target.id] === 0) {
board[e.target.id] = player.id;
$(this).addClass(player.color).html(player.symbol);
check(board) ? gameOver(check(board)) : setTimeout(computersTurn, 300);
}
});
}
function computersTurn() {
minmax(board, 'comp'); // runs minmax to find optimal move
board[compMove] = comp.id;
$(`#${compMove}`).addClass(comp.color).html(comp.symbol);
check(board) ? gameOver(check(board)) : playersTurn();
}
/* Returns an array of available/empty squares */
function emptySquares(state) {
// return indexes of empty squares
return state.map(function(square, index) {
return square === 0 ? index : false;
}).filter(function(index) {
// filter through the indexes and only return valid ones
return index !== false;
});
}
// referenced min-max algorithm from here:
// https://www.neverstopbuilding.com/blog/2013/12/13/tic-tac-toe-understanding-the-minimax-algorithm13/
// and big help from: https://codepen.io/thepeted/full/qOJVyK/
function minmax(state, player) {
var result = check(state);
if(result === 'lose') {
return 10;
} else if(result === 'win') {
return -10;
} else if(result === 'tie') {
return 0;
}
var moves = [], scores = [];
emptySquares(state).forEach(function(index) {
state[index] = player === 'comp' ? -1 : 1;
scores.push(minmax(state, player === 'comp' ? 'player' : 'comp'));
moves.push(index);
state[index] = 0;
});
// return the best move
if(player === 'comp') {
compMove = moves[scores.indexOf(Math.max.apply(Math, scores))];
return Math.max.apply(Math, scores);
} else {
compMove = moves[scores.indexOf(Math.min.apply(Math, scores))];
return Math.min.apply(Math, scores);
}
}
})();
Also see: Tab Triggers