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="game-container">
<div class="game">
<div class="section" id="green"></div>
<div class="section" id="red"></div>
<div class="section" id="yellow"></div>
<div class="section" id="blue"></div>
</div>
<div class="game-center">
<span id="round"></span>
</div>
</div>
<div class="controls">
<div class="controls__group">
<div class="switch-container">
<div class="switch"></div>
</div>
<span>OFF/ON</span>
</div>
<div class="controls__group">
<button class="btn red" disabled>
<div class="btn-shine"></div>
</button>
<span>START</span>
</div>
<div class="controls__group">
<button class="btn yellow" disabled>
<div class="btn-shine"></div>
</button>
<span>STRICT</span>
</div>
</div>
</div>
<div class="modal-backdrop">
<div class="modal">
<span>You won! You made it this far, you deserve a cookie.</span>
<img src="http://scibosnian.com/wp-content/uploads/2017/07/Chocolate_chip_cookie.png" style="width:200px">
<button id="restart">play again</button>
</div>
</div>
@import url('https://fonts.googleapis.com/css?family=Roboto+Mono');
body,
html {
font-family: 'Roboto Mono', monospace;
margin: 0;
}
.container {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
}
.container > span {
margin-top: .5rem;
}
/* --- GAME --- */
.game-container {
position: relative;
}
.game-center {
background: white;
border-radius: 50%;
position: absolute;
width: 200px;
height: 200px;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
.game {
display: grid;
grid-template: repeat(2, 1fr) / repeat(2, 1fr);
margin: 1.5rem;
width: 400px;
height: 400px;
}
#green {
background: #2ecc71;
border-top-left-radius: 100%;
}
#red {
background: lightcoral;
border-top-right-radius: 100%;
}
#yellow {
background: gold;
border-bottom-left-radius: 100%;
}
#blue {
background: lightskyblue;
border-bottom-right-radius: 100%;
}
#round {
color: lightgray;
font-size: 3rem;
}
.active:active { animation: .3s animate; }
.animate-section { animation: .5s animate; }
.animate-error { animation: .5s error; }
/*GAME ANIMATIONS*/
@keyframes animate {
0% { transform: scale(1); }
50% { transform: scale(1.1); }
100% { transform: scale(1); }
}
@keyframes error {
25% { transform: translateX(-2%); }
50% { transform: translateX(0); }
75% { transform: translateX(2%); }
100% { transform: translateX(0); }
}
/* --- CONTROLS --- */
.controls {
display: flex;
justify-content: space-around;
align-items: center;
padding: 1rem;
width: 300px;
}
.controls__group {
display: flex;
flex-direction: column;
align-items: center;
}
.controls__group > span {
font-size: 10px;
margin-top: .5rem;
}
/* --- CONTROL BUTTONS --- */
.btn, .btn:focus {
position: relative;
border: 0;
border-radius: 50%;
width: 20px;
height: 20px;
margin-bottom: 6px;
outline: 0;
}
.btn:active {
transform: translateY(-1px);
}
.btn-shine {
background-color: rgba(255,255,255,0.3);
border-radius: 50%;
position: absolute;
top: 3px;
left: 5px;
height: 5px;
width: 10px;
}
.switch-container {
background: #f1f1f1;
border-radius: 1rem;
height: 20px;
width: 50px;
padding: 3px;
}
.switch {
background: lightgray;
border-radius: 50%;
height: 20px;
width: 20px;
}
.move-switch-right {
animation: .5s moveRight forwards;
transition: all .2s ease-in;
}
.move-switch-left {
animation: .5s moveLeft;
transition: all .2s ease-in;
}
/* SWITCH ANIMATIONS */
@keyframes moveRight {
0% {
transform: translateX(0);
background: lightgray;
}
100% {
transform: translateX(150%);
background: #2ecc71;
}
}
@keyframes moveLeft {
0% {
transform: translateX(150%);
background: #2ecc71;
}
100% {
transform: translateX(0%);
background: lightgray;
}
}
/* CONTROL BUTTONS - COLORS */
.red { background: linear-gradient(lightcoral, #ff5050); }
.red:hover { background: #de4646; }
.red--active { box-shadow: 0 0 1rem red; }
.yellow { background: linear-gradient(#ffd700, #f7c500); }
.yellow:hover { background: #e1a900; }
.yellow--active { box-shadow: 0 0 1rem #ff9400; }
/* --- MODAL (WINNING SCREEN) --- */
.modal-backdrop {
display: none;
background-color: rgba(0,0,0,0.25);
position: absolute;
top: 0;
width: 100%;
height: 100%;
z-index: 1;
animation: .5s animate-modal;
}
.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;
align-items: center;
text-align: center;
}
.modal > button, button:focus {
background-color: rgba(0,0,0,0.2);
border: 0;
border-radius: .5rem;
color: white;
font-family: 'Roboto Mono', monospace;
font-size: 1rem;
padding: .5rem;
text-transform: uppercase;
outline: 0;
}
.modal > button:hover {
background-color: rgba(0,0,0,0.25);
}
/* MODAL ANIMATIONS */
@keyframes animate-modal {
0% { opacity: 0; }
100% { opacity: 1; }
}
/*
* SIMON GAME
* for freecodecamp.com
* by Alexis Okamura - 2018
*
*/
const simonGame = (function() {
const MAXROUND = 20, GAMESPEED = 1000;
const sound = {
blue: new Audio('https://s3.amazonaws.com/freecodecamp/simonSound1.mp3'),
red: new Audio('https://s3.amazonaws.com/freecodecamp/simonSound2.mp3'),
yellow: new Audio('https://s3.amazonaws.com/freecodecamp/simonSound3.mp3'),
green: new Audio('https://s3.amazonaws.com/freecodecamp/simonSound4.mp3'),
error: new Audio('http://ia601500.us.archive.org/4/items/simo_game_audio/error.mp3')
};
const game = {
start: false,
strictMode: false,
power: false,
sequence: [],
round: 1
};
function init() {
game.start = true;
reset();
formatRound();
addStep();
playSequence();
}
function reset() {
game.sequence = [];
game.round = 1;
}
/* Randomly adds a new color to sequence */
function addStep() {
const colors = ['blue', 'red', 'yellow', 'green'];
game.sequence.push(colors[Math.floor(Math.random()*4)]);
}
/* Formats + sets current round on UI */
function formatRound() {
if(game.round < 10) {
$('#round').text('0' + game.round);
} else {
$('#round').text(game.round);
}
}
function playersTurn() {
let counter = 0;
$('.section')
.removeClass('animate-section') // removes animation class played during playSequence
.addClass('active'); // adds active class so section is clickable + animated
$('.section').on('click', function() {
let move = $(this).attr('id');
// ERROR CHECKING
if(game.sequence[counter] !== move) {
$('.section').off(); // so player can't make repeated errors
playError();
if(game.strictMode) {
setTimeout(function() {
init();
}, 100);
counter = 0;
} else {
setTimeout(function() {
playSequence();
}, 100);
counter = 0;
}
} else {
// correct move, increase counter
counter++;
}
// GAME CHECKING
if(counter === MAXROUND) {
// player won the game, display win screen
$('.modal-backdrop').show();
counter = 0;
} else if(counter === game.sequence.length) {
// player sucessfully did a correct round
$('.section').off();
game.round++;
formatRound();
addStep();
playSequence();
}
});
}
function playSequence() {
// player cannot make a move while sequence is playing
$('.section').removeClass('active');
let counter = 0;
let intervals = setInterval(function() {
if(!game.start) { // if player suddenly turns off start/power
clearInterval(intervals);
} else if(counter === game.sequence.length) { // sequence is done playing
clearInterval(intervals);
playersTurn();
} else { // continue to play sequence
playCurrent(game.sequence[counter]);
counter++;
}
}, GAMESPEED);
}
function playError() {
$('.game').removeClass('animate-error');
setTimeout(function () {
sound.error.load();
sound.error.play();
$('.game').addClass('animate-error');
}, 100);
}
function playCurrent(color) {
$('.section').removeClass('animate-section');
setTimeout(function() {
switch(color) {
case 'blue':
sound.blue.load();
sound.blue.play();
$('#blue').addClass('animate-section');
break;
case 'red':
sound.red.load();
sound.red.play();
$('#red').addClass('animate-section');
break;
case 'yellow':
sound.yellow.load();
sound.yellow.play();
$('#yellow').addClass('animate-section');
break;
case 'green':
sound.green.load();
sound.green.play();
$('#green').addClass('animate-section');
break;
}
}, 100);
}
function togglePower(power) {
if(power) {
game.power = true;
} else {
game.strictMode = false;
game.start = false;
game.power = false;
reset();
}
}
function toggleStart(start) {
if(start) {
game.start = true;
init();
} else {
game.start = false;
}
}
function toggleStrict(strict) {
game.strictMode = strict ? true : false;
}
return {
init: init,
togglePower: togglePower,
toggleStart: toggleStart,
toggleStrict: toggleStrict
};
})();
$(document).ready(function() {
// POWER BUTTON
$('.switch').on('click', function() {
if($('.switch').hasClass('move-switch-right')) {
simonGame.togglePower(false); // turn off
$('.switch').removeClass('move-switch-right').addClass('move-switch-left');
$('.red, .yellow').removeClass('red--active yellow--active').prop('disabled', true);
$('#round').text('');
} else {
simonGame.togglePower(true); // turn on
$('.switch').removeClass('move-switch-left').addClass('move-switch-right');
$('.red, .yellow').prop('disabled', false);
$('#round').text('--');
}
});
// STRICT BUTTON
$('.yellow').on('click', function() {
if($('.yellow').hasClass('yellow--active')) {
simonGame.toggleStrict(false); // turn off
$('.yellow').removeClass('yellow--active');
} else {
simonGame.toggleStrict(true); // turn on
$('.yellow').addClass('yellow--active');
}
});
// START BUTTON
$('.red').on('click', function() {
if($('.red').hasClass('red--active')) {
simonGame.toggleStart(false); // turn off
$('.red').removeClass('red--active');
$('#round').text('--');
} else {
simonGame.toggleStart(true); // turn on
$('.red').addClass('red--active');
}
});
// RESTART BUTTON
$('#restart').on('click', function() {
$('.modal-backdrop').hide();
simonGame.init();
});
});
Also see: Tab Triggers