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.
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Memory Game</title>
<link rel="stylesheet" href="main.css">
</head>
<body>
<div class="wrapper">
<h1>Memory Game for the Disaster Distress Helpline</h1>
<p>(Not optimized for mobile phones)</p>
<div class="result">
<p>Result</p>
<span>(Less is better!)</span>
<p class="counter">0</p>
</div>
<div class="table">
<div class="field" id="1">
<div class="inner-wrap">
<div class="front"></div>
<div class="back"></div>
</div>
</div>
<div class="field" id="2">
<div class="inner-wrap">
<div class="front"></div>
<div class="back"></div>
</div>
</div>
<div class="field" id="3">
<div class="inner-wrap">
<div class="front"></div>
<div class="back"></div>
</div>
</div>
<div class="field" id="4">
<div class="inner-wrap">
<div class="front"></div>
<div class="back"></div>
</div>
</div>
<div class="field" id="5">
<div class="inner-wrap">
<div class="front"></div>
<div class="back"></div>
</div>
</div>
<div class="field" id="6">
<div class="inner-wrap">
<div class="front"></div>
<div class="back"></div>
</div>
</div>
<div class="field" id="7">
<div class="inner-wrap">
<div class="front"></div>
<div class="back"></div>
</div>
</div>
<div class="field" id="8">
<div class="inner-wrap">
<div class="front"></div>
<div class="back"></div>
</div>
</div>
<div class="field" id="9">
<div class="inner-wrap">
<div class="front"></div>
<div class="back"></div>
</div>
</div>
<div class="field" id="10">
<div class="inner-wrap">
<div class="front"></div>
<div class="back"></div>
</div>
</div>
<div class="field" id="11">
<div class="inner-wrap">
<div class="front"></div>
<div class="back"></div>
</div>
</div>
<div class="field" id="12">
<div class="inner-wrap">
<div class="front"></div>
<div class="back"></div>
</div>
</div>
<div class="field" id="13">
<div class="inner-wrap">
<div class="front"></div>
<div class="back"></div>
</div>
</div>
<div class="field" id="14">
<div class="inner-wrap">
<div class="front"></div>
<div class="back"></div>
</div>
</div>
<div class="field" id="15">
<div class="inner-wrap">
<div class="front"></div>
<div class="back"></div>
</div>
</div>
<div class="field" id="16">
<div class="inner-wrap">
<div class="front"></div>
<div class="back"></div>
</div>
</div>
<div class="field" id="17">
<div class="inner-wrap">
<div class="front"></div>
<div class="back"></div>
</div>
</div>
<div class="field" id="18">
<div class="inner-wrap">
<div class="front"></div>
<div class="back"></div>
</div>
</div>
<div class="field" id="19">
<div class="inner-wrap">
<div class="front"></div>
<div class="back"></div>
</div>
</div>
<div class="field" id="20">
<div class="inner-wrap">
<div class="front"></div>
<div class="back"></div>
</div>
</div>
<div class="field" id="21">
<div class="inner-wrap">
<div class="front"></div>
<div class="back"></div>
</div>
</div>
<div class="field" id="22">
<div class="inner-wrap">
<div class="front"></div>
<div class="back"></div>
</div>
</div>
<div class="field" id="23">
<div class="inner-wrap">
<div class="front"></div>
<div class="back"></div>
</div>
</div>
<div class="field" id="24">
<div class="inner-wrap">
<div class="front"></div>
<div class="back"></div>
</div>
</div>
</div>
</div>
</body>
</html>
* {
margin: 0;
padding: 0;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
body {
background-color: #2b4046;
}
.wrapper {
max-width: 900px;
width: 100%;
margin: 0 auto;
margin-top: 10px;
position: relative;
}
h1 {
text-align: center;
color: #e0e0e0;
}
img {
max-width: 86%;
height: auto;
margin-top: 20px;
}
.table:after {
content: "";
display: block;
clear: both;
}
.field {
width: 22%;
margin: 1% 1%;
height: 270px;
border: 1px solid #000;
border-radius: 8px;
float: left;
cursor: pointer;
}
.inner-wrap {
width: 100%;
height: 100%;
transition: all 0.6s;.
-webkit-transition: all 0.6s;
transform-style: preserve-3d;
position: relative;
}
.front {
background: url(https://strengthafterdisaster.org/wp-content/uploads/2022/10/Card-Back.png) no-repeat top center;
width: 100%;
height: 100%;
border-radius: 8px;
z-index: 2;
transform: rotateY(0deg);
-webkit-transform: rotateY(0deg);
-moz-transform: rotateY(0deg);
-o-transform: rotateY(0deg);
}
.flipped {
transform: rotateY(180deg);
-webkit-transform: rotateY(180deg);
-moz-transform: rotateY(180deg);
-o-transform: rotateY(180deg);
}
.front, .back {
backface-visibility: hidden;
-webkit-backface-visibility: hidden;
position: absolute;
top: 0;
left: 0;
}
.back {
transform: rotateY(180deg);
-webkit-transform: rotateY(180deg);
-moz-transform: rotateY(180deg);
-o-transform: rotateY(180deg);
text-align: center;
}
.result p {
font-size: 46px;
color: #e0e0e0;
}
.result {
position: absolute;
right: -185px;
top: 58px;
}
p.counter {
text-align: center;
}
span {
text-align: center;
display: block;
color: #e0e1f0
}
.wrapper > p {
text-align: center;
margin-bottom: 20px;
}
$(document).ready(function(){
var checkArray = []; // checking if both clicked fields are the same fruit
var idCheck = []; // helper array for storing clicked fields IDs so i can remove "flipped" class if they are different
var counter = 0;
var end = 0; // for detecting if all fields are done
var fields = document.querySelectorAll(".back");
var natureSound = new Audio("http://k003.kiwi6.com/hotlink/2ai7iwz2j6/nature.mp3");
var spark = new Audio("http://k003.kiwi6.com/hotlink/qdpr7bioht/spark.mp3");
var win = new Audio("http://k003.kiwi6.com/hotlink/eptlrqspgk/win.mp3");
var images = [
"https://strengthafterdisaster.org/wp-content/uploads/2022/10/Hearts-A.png",
"https://strengthafterdisaster.org/wp-content/uploads/2022/10/Hearts-K.png",
"https://strengthafterdisaster.org/wp-content/uploads/2022/10/Hearts-Q.png",
"https://strengthafterdisaster.org/wp-content/uploads/2022/10/Hearts-10.png",
"https://strengthafterdisaster.org/wp-content/uploads/2022/10/Hearts-9.png",
"https://strengthafterdisaster.org/wp-content/uploads/2022/10/Hearts-8.png",
"https://strengthafterdisaster.org/wp-content/uploads/2022/10/Hearts-7.png",
"https://strengthafterdisaster.org/wp-content/uploads/2022/10/Hearts-6.png",
"https://strengthafterdisaster.org/wp-content/uploads/2022/10/Hearts-5.png",
"https://strengthafterdisaster.org/wp-content/uploads/2022/10/Hearts-4.png",
"https://strengthafterdisaster.org/wp-content/uploads/2022/10/Hearts-3.png",
"https://strengthafterdisaster.org/wp-content/uploads/2022/10/Hearts-2.png",
"https://strengthafterdisaster.org/wp-content/uploads/2022/10/Hearts-A.png",
"https://strengthafterdisaster.org/wp-content/uploads/2022/10/Hearts-K.png",
"https://strengthafterdisaster.org/wp-content/uploads/2022/10/Hearts-Q.png",
"https://strengthafterdisaster.org/wp-content/uploads/2022/10/Hearts-10.png",
"https://strengthafterdisaster.org/wp-content/uploads/2022/10/Hearts-9.png",
"https://strengthafterdisaster.org/wp-content/uploads/2022/10/Hearts-8.png",
"https://strengthafterdisaster.org/wp-content/uploads/2022/10/Hearts-7.png",
"https://strengthafterdisaster.org/wp-content/uploads/2022/10/Hearts-6.png",
"https://strengthafterdisaster.org/wp-content/uploads/2022/10/Hearts-5.png",
"https://strengthafterdisaster.org/wp-content/uploads/2022/10/Hearts-4.png",
"https://strengthafterdisaster.org/wp-content/uploads/2022/10/Hearts-3.png",
"https://strengthafterdisaster.org/wp-content/uploads/2022/10/Hearts-2.png",
];
function clicked() { // clicked function so i can unbind click event to prevet shit like clicking more then 2 fields at one try
if ($(this).find(".inner-wrap").hasClass("flipped")) {
return;
}
$(this).find(".inner-wrap").toggleClass("flipped");
checkArray.push($(this).find("img").attr("src"));
idCheck.push($(this).attr("id"));
check();
}
$(".field").on("click", clicked);
function restart() {
$(".back").find("img").remove(); //remove all current images from the field
$(".field .inner-wrap").removeClass("flipped"); // remove flipped class so they can flip back again at the starting position
checkArray = []; // empty check array
idCheck = []; // empty IDs check array
counter = 0; // reset counter
end = 0; // reset ending variable
startGame();
}
function checkEnd() {
if (end === 24) { //if all 24 fields are uncovered
win.play();
alert("Game is over! Your score is " + counter);
restart();
}
}
function shuffleArray(array) { // shuffle array with images
for (var i = array.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var temp = array[i];
array[i] = array[j];
array[j] = temp;
}
return array;
}
function startGame() {
natureSound.play(); // play the background music
var arr = shuffleArray(images); // stores the array of shuffled images
for (var i = 0; i < fields.length; i++) { // appending those images to the div with class "back"
var img = document.createElement("img");
img.src = arr[i];
fields[i].appendChild(img);
}
}
function check() {
if (checkArray.length === 2) { // if fields are clicked 2 times we are doing check
$(".field").off("click", clicked); // disabling click event to prevet shit
setTimeout(function(){
if (checkArray[0] !== checkArray[1]) { // if there is no match
$("#" + idCheck[0]).find(".inner-wrap").removeClass("flipped"); // flip the field back
$("#" + idCheck[1]).find(".inner-wrap").removeClass("flipped"); // second one flip back as well
counter++;
checkArray = []; //empty checking array for the next 2 clicks
idCheck = []; // same with this one
$(".field").on("click", clicked); // bind the click back again
} else {
spark.play();
counter++;
end += 2; // if there is a match "end" is raised by 2 as 2 fields are uncovered
checkArray = []; // empty array for the next try
idCheck = []; // this one as well
checkEnd(); // check if game has eneded
$(".field").on("click", clicked); // bind click again
}
document.querySelector(".counter").innerHTML = counter;
}, 800);
}
}
startGame();
});
Also see: Tab Triggers