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.
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<body>
<div class = "middle">
<div class = "mainContainer">
<div class = "colorBtn green" id = "green"></div>
<div class = "colorBtn noRadius red" id = "red"></div>
<div class = "colorBtn noRadius yellow" id = "yellow"></div>
<div class = "colorBtn rotateRadius blue" id = "blue"></div>
<div class = "hLine"></div>
<div class = "vLine"></div>
<div class = "midCircle">
<div class = "scoreBoard" id = "scoreBoard">--</div>
<div class = "opBtn" id = "startBtn">
<div class = "startRed"></div>
</div>
<div class = "opBtn" id = "strictBtn" value = "off">
<div class = "startRed strictYellow"></div>
</div>
<div class = "led"></div>
<br>
<div class = "text">START</div>
<div class = "text">STRICT</div>
<br>
<!-- Rectangular switch -->
<label class="switch">
<input type="checkbox" value = "off">
<span class="slider"></span>
</label>
<div class = "textSwitch">OFF</div>
<div class = "textSwitch textOn">ON</div>
<div class = "branding">Simon<span class = "supMod">®</span></div>
</div>
<signature>built by Hieu Lai</signature>
</div>
</div>
</body>
@import url('https://fonts.googleapis.com/css?family=Alfa+Slab+One|VT323');
body {
display: table;
position: absolute;
height: 100%;
width: 100%;
background-image: url("https://wallpaperscraft.com/image/wood_planks_parquet_texture_surface_44962_3840x2400.jpg");
background-position: center;
}
.middle {
display: table-cell;
vertical-align: middle;
}
.mainContainer {
border: 15px solid;
margin-left: auto;
margin-right: auto;
border-radius: 100px 0px 100px 0px;
width: 450px;
height: 450px;
box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
}
.colorBtn {
border: 0px solid transparent;
border-radius: 83px 0px 0px;
width: 210px;
height: 210px;
float: left;
//cursor: pointer;
}
.noRadius {
border-radius: 0px;
}
.rotateRadius {
border-radius: 0px 0px 83px;
}
.hLine {
border-top: 10px solid;
width: 430px;
position: absolute;
margin-top: 205px;
}
.vLine {
border-left: 10px solid;
height: 430px;
position: absolute;
margin-left: 205px;
}
.midCircle {
border: 10px solid;
background: white;
border-radius: 50%;
width: 250px;
height: 250px;
position: absolute;
margin: 85px;
box-shadow: 0 8px 16px 0 rgba(0, 0, 0, 0.4), 0 12px 40px 0 rgba(0, 0, 0, 0.38);
}
.green {
background: #006600;
}
.red {
background: #990000;
}
.blue {
background: #000099;
}
.yellow {
background: #999900;
}
.scoreBoard {
border: 5px solid black;
border-radius: 20px;
width: 120px;
height: 70px;
margin: 20px 55px;
text-align: center;
line-height: 60px;
font-family: "VT323", monospace;
font-size: 60px;
color: #4c0000;
background: #330000;
}
.opBtn {
border: 4px solid;
border-radius: 50px;
width: 30px;
height: 30px;
margin-top: -10px;
margin-left: 56px;
float: left;
box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
background: black;
cursor: pointer;
}
.opBtn:active {
transform: translateY(2px);
}
.startRed {
border-radius: 50px;
width: 20px;
height: 20px;
background: red;
margin: auto;
}
.strictYellow {
background: yellow;
}
.led {
border: 2px solid black;
border-radius: 50px;
width: 10px;
height: 10px;
float: right;
margin-right: 40px;
background: #333333;
}
.text {
margin-left: 52px;
font-size: 12px;
font-weight: bold;
float: left;
}
.switch {
position: absolute;
width: 45px;
height: 25px;
margin-left: 92px;
border-radius: 4px;
box-shadow: 0 2px 4px 0 rgba(0, 0, 0, 0.2), 0 3px 10px 0 rgba(0, 0, 0, 0.19);
}
/* Hide default HTML checkbox */
.switch input {display:none;}
/* The slider */
.slider {
position: absolute;
cursor: pointer;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: #ccc;
-webkit-transition: .4s;
transition: .4s;
}
.slider:before {
position: absolute;
content: "";
height: 19px;
width: 19px;
border-radius: 4px;
left: 3px;
bottom: 3px;
background-color: white;
-webkit-transition: .4s;
transition: .4s;
}
input:checked + .slider {
background-color: #2196F3;
}
input:focus + .slider {
box-shadow: 0 0 1px #2196F3;
}
input:checked + .slider:before {
-webkit-transform: translateX(26px);
-ms-transform: translateX(26px);
transform: translateX(20px);
}
.textSwitch {
font-size: 10px;
font-weight: bold;
position: absolute;
margin-left: 65px;
line-height: 25px;
}
.textOn {
margin-left: 145px;
}
.branding {
font-family: 'Alfa Slab One', cursive;
margin: 27px 67px;
font-weight: bold;
font-size: 28px
}
.supMod {
font: 15px arial, sans-serif;
vertical-align: super;
}
signature {
position: absolute;
margin-top: -12px;
margin-left: -72px;
font-size: 10px;
color: white;
}
$(document).ready(function() {
var stateOnOff = "";
var stateLed = "";
var strictMode = "";
var winLed;
var winColor;
var huTurn = false;
var colorArr = ["green", "red", "blue", "yellow"];
var computeArr = [];
var killSwitch = false;
var lightGreen = "#00cd00";
var darkGreen = "#006600";
var lightRed = "#ff4d4d";
var darkRed = "#990000";
var lightBlue = "#4d4dff";
var darkBlue = "#000099";
var lightYellow = "#ffff4d";
var darkYellow = "#999900";
var soundGreen = new Audio("https://s3.amazonaws.com/freecodecamp/simonSound1.mp3");
var soundRed = new Audio("https://s3.amazonaws.com/freecodecamp/simonSound2.mp3");
var soundBlue = new Audio("https://s3.amazonaws.com/freecodecamp/simonSound3.mp3");
var soundYellow = new Audio("https://s3.amazonaws.com/freecodecamp/simonSound4.mp3");
//Create the light show for the color panels and LED scoreboard
function loadFlash(item) {
if(item == "panel") {
$("#green").css("background", lightGreen);
setTimeout(function() {
$("#red").css("background", lightRed);
}, 200);
setTimeout(function() {
$("#blue").css("background", lightBlue);
}, 400);
setTimeout(function() {
$("#yellow").css("background", lightYellow);
}, 600);
setTimeout(function() {
$("#yellow").css("background", darkYellow);
}, 800);
setTimeout(function() {
$("#blue").css("background", darkBlue);
}, 1000);
setTimeout(function() {
$("#red").css("background", darkRed);
}, 1200);
setTimeout(function() {
$("#green").css("background", darkGreen);
}, 1400);
}
if(item === "scoreLed") {
$("#scoreBoard").css("color", "#4c0000");
setTimeout(function() {
$("#scoreBoard").css("color", "#FF0000");
}, 300);
setTimeout(function() {
$("#scoreBoard").css("color", "#4c0000");
}, 600);
setTimeout(function() {
$("#scoreBoard").css("color", "#FF0000");
}, 900);
}
}
//Chooses a random color
function randColor() {
return colorArr[Math.floor(Math.random() * 4)];
}
//Add zero to the number if below 10
function addZero(num) {
if(num < 10) {
return "0" + num;
}
return num;
}
//Turns on the color that is called, also plays audio
function activateColor(color) {
if(color === "green") {
$("#"+color).css("background", lightGreen);
soundGreen.load();
soundGreen.play();
setTimeout(function() {
$("#"+color).css("background", darkGreen);
}, 700);
}
if(color === "red") {
$("#"+color).css("background", lightRed);
soundRed.load();
soundRed.play();
setTimeout(function() {
$("#"+color).css("background", darkRed);
}, 700);
}
if(color === "blue") {
$("#"+color).css("background", lightBlue);
soundBlue.load();
soundBlue.play();
setTimeout(function() {
$("#"+color).css("background", darkBlue);
}, 700);
}
if(color === "yellow") {
$("#"+color).css("background", lightYellow);
soundYellow.load();
soundYellow.play();
setTimeout(function() {
$("#"+color).css("background", darkYellow);
}, 700);
}
}
//Light show when player wins
function winStatus() {
$("#scoreBoard").text("WIN");
winLed = setInterval(function() {
loadFlash("scoreLed");
loadFlash("panel");
activateColor(randColor());
}, 800);
}
//Main function that alternates between player and computer
function computer(arg) {
var i = 0;
var count = 0;
huTurn = false;
//push random color into array
if(arg === "add") {
computeArr.push(randColor());
}
//activates when player completes round 10
if(computeArr.length == 10) {
return winStatus();
}
autoLight();
$(".colorBtn").off("click");
$(".colorBtn").css("cursor", "");
//computer loops through the array, lighting up the ordered color panels
function autoLight() {
if(killSwitch == true) {return};
activateColor(computeArr[i]);
i++;
$("#scoreBoard").text(addZero(computeArr.length));
if(i < computeArr.length) {
setTimeout(autoLight, 1000);
}
//switch over control to player
if(i == computeArr.length) {
setTimeout(function() {
huTurn = true;
$(".colorBtn").css("cursor", "pointer");
}, 1000);
}
}
//determines if player is clicking correct color panel
$(".colorBtn").on("click", function() {
var color = $(this).attr("id");
if(huTurn == true && color == computeArr[count]) {
activateColor(color);
count++;
if(count === computeArr.length) {
setTimeout(function() {
computer("add");
}, 1500);
}
}
//if mistake is made
else if(huTurn == true && color !== computeArr[count]) {
$("#scoreBoard").text("!!");
activateColor(color);
loadFlash("scoreLed");
//checks strict mode otherwise, return from previous
if(strictMode == "on") {
setTimeout(function() {
restartGame();
}, 1500);
}
else {
setTimeout(function() {
computer();
}, 1500);
}
}
});
}
//Brings the game to the start, level 1
function restartGame() {
killSwitch = true;
clearInterval(winLed);
loadFlash("scoreLed");
$("#scoreBoard").text("--");
setTimeout(function() {
killSwitch = false;
computeArr = [];
computer("add");
}, 1500);
}
//Cosmetic and assignment of strict mode
function strictOption(mode) {
if(mode == "on") {
$(".led").css("background", lightGreen);
strictMode = "on";
$("#strictBtn").attr("value", "on");
}
if(mode == "off") {
$(".led").css("background", "#333333");
strictMode = "off";
$("#strictBtn").attr("value", "off");
}
}
//Activates when player turns on the switch
function startGame() {
$("#startBtn").on("click", restartGame);
$("#strictBtn").on("click", function() {
stateLed = $(this).attr("value");
if(stateLed == "off") {
strictOption("on");
}
else if(stateLed == "on") {
strictOption("off");
}
})
}
//On-Off switch, returns intial game state
$("input").on("click", function() {
stateOnOff = $(this).attr("value");
if(stateOnOff == "off") {
$("#scoreBoard").css("color", "#FF0000");
$(this).attr("value", "on");
loadFlash("panel");
startGame();
killSwitch = false;
}
else if(stateOnOff == "on") {
$("#scoreBoard").css("color", "#4c0000");
killSwitch = true;
$(this).attr("value", "off");
$(".opBtn").off("click");
$("#scoreBoard").text("--");
$(".colorBtn").off("click");
$(".colorBtn").css("cursor", "");
strictOption("off");
clearInterval(winLed);
}
})
});
Also see: Tab Triggers