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.
<header>
<h1>MINESWEEPER</h1>
</header>
<div id="modal">
<div class="modal-box">
<p class="msg">Hello !</p><br>
<p class="play">PLAY</p>
</div>
</div>
<section>
<div class="mine-head">
<span class="flag"><i class="fa fa-flag" aria-hidden="true"></i> <span class="flagN">10</span></span>
<span class="smile"><i class="fa fa-smile-o fa-2x" aria-hidden="true"></i></span>
<span class="time"><span class="timeN">0</span> <i class="fa fa-clock-o" aria-hidden="true"></i></span>
</div>
<table class="mine-body">
<tr>
<td class="box backgrounded"></td>
<td class="box backgrounded"></td>
<td class="box backgrounded"></td>
<td class="box backgrounded"></td>
<td class="box backgrounded"></td>
<td class="box backgrounded"></td>
<td class="box backgrounded"></td>
<td class="box backgrounded"></td>
<td class="box backgrounded"></td>
</tr>
<tr>
<td class="box backgrounded"></td>
<td class="box backgrounded"></td>
<td class="box backgrounded"></td>
<td class="box backgrounded"></td>
<td class="box backgrounded"></td>
<td class="box backgrounded"></td>
<td class="box backgrounded"></td>
<td class="box backgrounded"></td>
<td class="box backgrounded"></td>
</tr>
<tr>
<td class="box backgrounded"></td>
<td class="box backgrounded"></td>
<td class="box backgrounded"></td>
<td class="box backgrounded"></td>
<td class="box backgrounded"></td>
<td class="box backgrounded"></td>
<td class="box backgrounded"></td>
<td class="box backgrounded"></td>
<td class="box backgrounded"></td>
</tr>
<tr>
<td class="box backgrounded"></td>
<td class="box backgrounded"></td>
<td class="box backgrounded"></td>
<td class="box backgrounded"></td>
<td class="box backgrounded"></td>
<td class="box backgrounded"></td>
<td class="box backgrounded"></td>
<td class="box backgrounded"></td>
<td class="box backgrounded"></td>
</tr>
<tr>
<td class="box backgrounded"></td>
<td class="box backgrounded"></td>
<td class="box backgrounded"></td>
<td class="box backgrounded"></td>
<td class="box backgrounded"></td>
<td class="box backgrounded"></td>
<td class="box backgrounded"></td>
<td class="box backgrounded"></td>
<td class="box backgrounded"></td>
</tr>
<tr>
<td class="box backgrounded"></td>
<td class="box backgrounded"></td>
<td class="box backgrounded"></td>
<td class="box backgrounded"></td>
<td class="box backgrounded"></td>
<td class="box backgrounded"></td>
<td class="box backgrounded"></td>
<td class="box backgrounded"></td>
<td class="box backgrounded"></td>
</tr>
<tr>
<td class="box backgrounded"></td>
<td class="box backgrounded"></td>
<td class="box backgrounded"></td>
<td class="box backgrounded"></td>
<td class="box backgrounded"></td>
<td class="box backgrounded"></td>
<td class="box backgrounded"></td>
<td class="box backgrounded"></td>
<td class="box backgrounded"></td>
</tr>
<tr>
<td class="box backgrounded"></td>
<td class="box backgrounded"></td>
<td class="box backgrounded"></td>
<td class="box backgrounded"></td>
<td class="box backgrounded"></td>
<td class="box backgrounded"></td>
<td class="box backgrounded"></td>
<td class="box backgrounded"></td>
<td class="box backgrounded"></td>
</tr>
<tr>
<td class="box backgrounded"></td>
<td class="box backgrounded"></td>
<td class="box backgrounded"></td>
<td class="box backgrounded"></td>
<td class="box backgrounded"></td>
<td class="box backgrounded"></td>
<td class="box backgrounded"></td>
<td class="box backgrounded"></td>
<td class="box backgrounded"></td>
</tr>
</table>
</section>
<footer>
<p>Created by <a href="https://remybeumier.be" target="_blank">Rémy Beumier</a></p>
</footer>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
body {
display: flex;
flex-direction: column;
justify-content: space-between;
align-items: center;
min-height: 100vh;
font-family: arial;
background: #2980b9;
background-image: linear-gradient(to top left, #2980b9, #1abc9c);
position: relative;
text-align: center;
}
header {
letter-spacing: 0.2em;
text-align: center;
padding: 20px 5px;
color: whitesmoke;
}
/* modal box style */
#modal {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
position: fixed;
z-index: 10;
top: 0;
left: 0;
height: 100%;
width: 100%;
background: rgba(0, 0, 0, 0.5);
text-align: center;
}
.modal-box {
padding: 30px;
margin: auto;
width: 240px;
background: whitesmoke;
border-radius: 2px;
text-align: center;
box-shadow: 0 0 2px 3px rgba(0,0,0,0.3);
}
.modal-box p {
color: #2980b9;
font-size: 24px;
letter-spacing: 0.1em;
}
.play {
width: 160px;
margin: auto;
padding: 10px;
border: solid 1px #2980b9;
border-radius: 2px;
cursor: pointer;
}
.play:hover {
color: whitesmoke;
background: #2980b9;
}
/* table style */
section {
width: 316px;
height: 360px;
background: whitesmoke;
margin: auto;
text-align: center;
}
/* mine head style */
.mine-head {
display: flex;
justify-content: space-between;
width: 316px;
height: 50px;
background: whitesmoke;
border-bottom: solid 2px #333;
padding: 5px;
text-align: center;
}
.flag {
line-height: 40px;
width: 100px;
text-align: left;
padding-left: 5px;
font-size: 20px;
color: #333;
}
.smile {
color: #f39c12;
margin-top: 2.5px;
text-align: center;
}
.time {
line-height: 40px;
width: 100px;
text-align: right;
padding-right: 5px;
font-size: 20px;
color: #333;
}
/* mine body style */
.mine-body {
width: 316px;
height: 316px;
background: whitesmoke;
padding: 5px;
}
.box {
width: 10%;
height: 10%;
cursor: pointer;
text-align: center;
border: solid #2980b9 1px;
color: #333;
}
.box.backgrounded {
background: #2980b9;
color: transparent;
border-color: transparent;
}
.box.backgrounded:hover {
background: #3498db;
}
.box.flagged {
background: #e74c3c !important;
color: transparent;
border-color: transparent;
}
/* fix bomb icon to take full width; to uncomment when fix found for click */
.box i {
/* width: 100%;
height: 100%; */
}
.hidden {
display: none !important;
}
footer {
text-align: center;
padding: 20px 10px;
color: whitesmoke;
font-size: 14px;
}
footer a {
color: #333;
text-decoration: none;
}
footer a:hover {
color: whitesmoke;
text-decoration: underline;
}
// find a way to flag on mobile
// add options ? bigger table and/or more bombs
var bomb = '<i class="fa fa-bomb" aria-hidden="true"></i>';
var smile = '<i class="fa fa-smile-o fa-2x" aria-hidden="true"></i>';
var frown = '<i class="fa fa-frown-o fa-2x" aria-hidden="true"></i>';
var rowIndex = 0;
var colIndex = 0;
var count = 0;
var time = 0;
var flagCount = 10;
var bombCount = 0;
var boxElts = document.getElementsByClassName("box");
var backgroundedElts = document.getElementsByClassName("backgrounded");
var modalElt = document.querySelector("#modal");
var playElt = document.querySelector(".play");
var msgElt = document.querySelector(".msg");
var mineBody = document.querySelector(".mine-body");
var flagElt = document.querySelector(".flagN");
var smileElt = document.querySelector(".smile");
var timeElt = document.querySelector(".timeN");
// start of game
playElt.addEventListener("click", startGame);
function startGame() {
// clearInterval(checkPageInterval);
addBombs(10);
// click and right click handler
for (var i=0; i<boxElts.length; i++) {
boxElts[i].addEventListener("click", checkBoxes);
boxElts[i].addEventListener("contextmenu", manageFlag);
if (boxElts[i].innerHTML !== bomb) {
addNumbers(boxElts[i], boxElts[i].parentNode.rowIndex, boxElts[i].cellIndex);
}
}
modalElt.classList.add("hidden");
// run time
timer = setInterval(function() {
document.querySelector(".timeN").innerHTML = time++;
}, 1000);
}
function addBombs(number) {
for (var i=0; i<number; i++) {
var rand = Math.floor(Math.random() * boxElts.length);
while (boxElts[rand].innerHTML === bomb) {
rand = Math.floor(Math.random() * boxElts.length);
}
boxElts[rand].innerHTML = bomb;
}
}
function checkBoxes(e) {
var curr = e.currentTarget;
if (!curr.classList.contains("flagged")) {
curr.classList.remove("backgrounded");
// click on bomb
if (curr.innerHTML === bomb) {
endGame("lose");
}
// win click
else if (curr.innerHTML !== bomb && backgroundedElts.length == 10) {
endGame("win");
}
// click on number -> reveal this box only (prevent next else to shoot checkSides)
else if (curr.innerHTML>0 && curr.innerHTML<9) {}
// click on empty -> reveal this box + reveal linked area until number
else {
checkSides(curr, curr.parentNode.rowIndex, curr.cellIndex);
}
}
}
function manageFlag(e) {
e.preventDefault();
var curr = e.currentTarget;
if (!curr.classList.contains("flagged") && flagCount > 0) {
flagCount--;
curr.classList.add("flagged");
}
else if (curr.classList.contains("flagged") && flagCount >= 0) {
flagCount++;
curr.classList.remove("flagged");
}
flagElt.innerHTML = flagCount;
}
function addNumbers(curr, rowI, colI) {
var bombCount = 0;
// top row
if (mineBody.rows[rowI-1]) {
if (mineBody.rows[rowI-1].cells[colI-1] &&
mineBody.rows[rowI-1].cells[colI-1].innerHTML === bomb) {
bombCount++;
}
if (mineBody.rows[rowI-1].cells[colI] &&
mineBody.rows[rowI-1].cells[colI].innerHTML === bomb) {
bombCount++;
}
if (mineBody.rows[rowI-1].cells[colI+1] &&
mineBody.rows[rowI-1].cells[colI+1].innerHTML === bomb) {
bombCount++;
}
}
// same row
if (mineBody.rows[rowI]) {
if (mineBody.rows[rowI].cells[colI-1] &&
mineBody.rows[rowI].cells[colI-1].innerHTML === bomb) {
bombCount++;
}
if (mineBody.rows[rowI].cells[colI] &&
mineBody.rows[rowI].cells[colI].innerHTML === bomb) {
bombCount++;
}
if (mineBody.rows[rowI].cells[colI+1] &&
mineBody.rows[rowI].cells[colI+1].innerHTML === bomb) {
bombCount++;
}
}
// bottom row
if (mineBody.rows[rowI+1]) {
if (mineBody.rows[rowI+1].cells[colI-1] &&
mineBody.rows[rowI+1].cells[colI-1].innerHTML === bomb) {
bombCount++;
}
if (mineBody.rows[rowI+1].cells[colI] &&
mineBody.rows[rowI+1].cells[colI].innerHTML === bomb) {
bombCount++;
}
if (mineBody.rows[rowI+1].cells[colI+1] &&
mineBody.rows[rowI+1].cells[colI+1].innerHTML === bomb) {
bombCount++;
}
}
if (bombCount>0) {curr.innerHTML = bombCount;}
}
function checkSides(curr, rowI, colI) {
if (!curr.classList.contains("backgrounded")) {
// top row
if (mineBody.rows[rowI-1]) {
checkRowCell(curr, rowI-1, colI-1);
checkRowCell(curr, rowI-1, colI);
checkRowCell(curr, rowI-1, colI+1);
}
// same row
if (mineBody.rows[rowI]) {
checkRowCell(curr, rowI, colI-1);
// checkRowCell(curr, rowI, colI);
checkRowCell(curr, rowI, colI+1);
}
// bottom row
if (mineBody.rows[rowI+1]) {
checkRowCell(curr, rowI+1, colI-1);
checkRowCell(curr, rowI+1, colI);
checkRowCell(curr, rowI+1, colI+1);
}
}
}
function checkRowCell(curr, roww, coll) {
if (mineBody.rows[roww].cells[coll] &&
mineBody.rows[roww].cells[coll].classList.contains("backgrounded")) {
mineBody.rows[roww].cells[coll].classList.remove("backgrounded");
// func callback with new row-col if box is empty
if (mineBody.rows[roww].cells[coll].innerHTML === "") {
checkSides(curr, mineBody.rows[roww].cells[coll].parentNode.rowIndex, mineBody.rows[roww].cells[coll].cellIndex);
}
}
}
function endGame(result) {
clearInterval(timer);
if (result === "lose") {
for (var i=0; i<boxElts.length; i++) {boxElts[i].classList.remove("backgrounded");}
smileElt.innerHTML = frown;
msgElt.innerHTML = "You lose !";
}
else if (result === "win") {
msgElt.innerHTML = "You win in " + time + " sec!";
}
setTimeout(function() {
resetGame();
}, 1200);
}
function resetGame() {
for (var i=0; i<boxElts.length; i++) {
boxElts[i].classList.add("backgrounded");
boxElts[i].classList.remove("flagged");
boxElts[i].innerHTML = "";
}
modalElt.classList.remove("hidden");
playElt.innerHTML = "REPLAY";
smileElt.innerHTML = smile;
time = 0;
flagCount = 10;
flagElt.innerHTML = flagCount;
timeElt.innerHTML = time;
count = 0;
}
// handle focus of the page
// function checkPageFocus() {
// if (document.hasFocus()) {
// modalElt.classList.remove("hidden");
// }
// else {
// modalElt.classList.add("hidden");
// }
// }
// var checkPageInterval = setInterval(checkPageFocus, 300);
Also see: Tab Triggers