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.
<!-- Game screen container -->
<div class="game-screen">
<!-- Number pad section -->
<div class="number-pad">
<div class="overlay"></div>
<!-- Number buttons -->
<div class="number">1</div>
<div class="number">2</div>
<div class="number">3</div>
<div class="number">4</div>
<div class="number">5</div>
<div class="number">6</div>
<div class="number">7</div>
<div class="number">8</div>
<div class="number">9</div>
<div class="number">0</div>
<!-- More number buttons... -->
<!-- Join button -->
<div class="join-btn">JOIN</div>
</div>
<!-- Game container section -->
<div class="game-container">
<!-- Result display section -->
<div class="result">
<!-- Display "Winner" text -->
Winner
<!-- Display the winning number (initially set to "-") -->
<div class="winner-number">-</div>
</div>
<!-- Game status section -->
<div class="game-status">
<!-- Game prize info -->
<div class="game-prize">
<!-- Display prize information -->
You can win
<!-- Display the prize value (initially set to 0 points) -->
<div class="game-prize-value">0 points</div>
</div>
<!-- Game point info -->
<div class="game-point">
<!-- Display points spent information -->
Points spent
<!-- Display the points spent value (initially set to 0 points) -->
<div class="game-point-value">0 points</div>
</div>
<!-- Selected number info -->
<div class="game-key">
<!-- Display selected number information -->
Selected Number
<!-- Display the selected number value (initially set to "--") -->
<div class="game-key-value">--</div>
</div>
</div> <!-- End of game-status -->
</div> <!-- End of game-container -->
</div> <!-- End of game-screen -->
/* Apply transition effects to all elements */
* {
transition: all 0.15s ease-in-out;
}
/* Set background color for the body */
body {
background: #000;
}
/* Styling for the game screen container */
.game-screen {
border: 2px solid black;
display: grid;
grid-template-columns: 30% 70%;
background: linear-gradient(#36f, blue); /* Gradient background */
color: #fff; /* Text color */
}
/* Styling for the number pad section */
.number-pad {
background: #0d0; /* Green background */
}
/* Styling for the result display */
.result {
height: 200px; /* Set height */
border: 2px solid black; /* Border */
}
/* Styling for the game status section */
.game-status {
display: flex;
justify-content: center; /* Center align items */
}
/* Styling for each section within the game status */
.game-status > div {
border: 2px solid black; /* Border */
flex-grow: 1; /* Allow items to grow */
padding: 20px; /* Padding */
text-align: center; /* Center align text */
font-size: 20px; /* Font size */
font-weight: bold; /* Bold text */
font-family: arial; /* Font family */
}
/* Styling for the number pad section */
.number-pad {
display: grid;
grid-template-columns: repeat(3, 1fr); /* 3 columns with equal width */
gap: 10px; /* Gap between grid items */
padding: 10px; /* Padding */
position: relative; /* Relative positioning */
overflow: hidden; /* Hide overflow */
}
/* Styling for each number button */
.number-pad > div {
display: grid;
place-content: center; /* Center align content */
border-radius: 20px; /* Rounded corners */
box-shadow: 0px 5px 10px rgba(0, 0, 0, 0.3), inset 0px 5px 10px rgba(255, 255, 255, 0.6); /* Box shadow */
user-select: none; /* Disable selection */
font-size: 20px; /* Font size */
font-family: arial; /* Font family */
font-weight: bold; /* Bold text */
}
/* Styling for active state of number buttons */
.number-pad > div:active {
box-shadow: 0px 5px 10px rgba(0, 0, 0, 0), inset 0px 5px 10px rgba(255, 255, 255, 0), inset 0px 5px 10px rgba(0, 0, 0, 0.3), 0px 5px 10px rgba(255, 255, 255, 0.6); /* Box shadow */
}
/* Styling for the JOIN button */
.join-btn {
grid-column: 2/4; /* Span columns */
background: black; /* Black background */
z-index: 1; /* Set z-index */
}
/* Styling for the overlay in the number pad */
.number-pad .overlay {
position: absolute; /* Absolute positioning */
width: 100%; /* Full width */
height: 100%; /* Full height */
background: red; /* Red background */
border-radius: 0px; /* No border radius */
opacity: 0; /* Opacity */
box-shadow: none; /* No box shadow */
z-index: 0; /* Set z-index */
}
/* Styling for the result text */
.result {
font-size: 50px; /* Font size */
font-family: arial; /* Font family */
font-weight: 900; /* Bold text */
text-align: center; /* Center align text */
}
/* Styling for the winning number */
.winner-number {
font-size: 100px; /* Font size */
}
// Initialize variables for game state
let isGameStarted = false; // Flag to track if the game is started
let gamepoint = 0; // Points spent by the player
let gamePrize = 0; // Prize the player can win
let gamePrizePercentage = 300; // Percentage of points to be added as prize
let gameKey = ""; // Selected number by the player
let winnerNumber = -1; // Winning number generated randomly
// Select DOM elements
let numberpadOverlay = document.querySelector(".number-pad .overlay");
let numbers = document.querySelectorAll(".number");
let joinBtn = document.querySelector(".join-btn");
let gamepointValue = document.querySelector(".game-point-value");
let gamePrizeValue = document.querySelector(".game-prize-value");
let gameKeyValue = document.querySelector(".game-key-value");
let winnerNumberElement = document.querySelector(".winner-number");
// Event listener for the "JOIN" button to start the game
joinBtn.addEventListener("click", startGame);
// Event listener for each number button
for (let i = 0; i < numbers.length; i++) {
numbers[i].addEventListener("click", () => {
// Set the selected number
gameKey = numbers[i].innerText;
gameKeyValue.innerText = gameKey;
// Enable pointer events on the overlay
numberpadOverlay.style.pointerEvents = "all";
// Wait for a delay to simulate the game process
setTimeout(function() {
// Display the winning number
winnerNumberElement.innerText = winnerNumber;
// Check if the selected number matches the winning number
if (parseInt(winnerNumber) == parseInt(gameKey)) {
// If matched, player wins
setTimeout(function() {
alert("Winner!");
isGameStarted = false;
confirmToRestart();
}, 1000);
} else {
// If not matched, player loses
setTimeout(function() {
alert("You lose!");
isGameStarted = false;
confirmToRestart();
}, 1000);
}
}, 5000); // Wait for 5 seconds
});
}
// Function to confirm if the player wants to restart the game
function confirmToRestart() {
let shouldRestartGame = confirm("Do you want to restart the game?");
if (shouldRestartGame) {
// If player wants to restart, start a new game
startGame();
} else {
// If not, hide the "JOIN" button
joinBtn.style.zIndex = 0;
}
}
// Function to start the game
function startGame() {
// Initialize game state variables
isGameStarted = true;
gamepoint = 0;
gamePrize = 0;
gamePrizePercentage = 500;
// Generate a random winning number
winnerNumber = parseInt(Math.random() * 10);
console.log("w:" + winnerNumber);
// Prompt the player to enter points they want to spend
gamepoint = parseInt(prompt("Enter how points you want to spend?"));
// Calculate the prize based on the points spent
gamePrize = parseInt(gamepoint * gamePrizePercentage / 100) + gamepoint;
// Update the game status display with points and prize information
gamepointValue.innerText = gamepoint + " points";
gamePrizeValue.innerText = gamePrize + " points";
// Wait for a delay to start the game after displaying information
setTimeout(function() {
alert("Now Select a number from left side.");
joinBtn.style.zIndex = -1; // Hide the "JOIN" button
numberpadOverlay.style.pointerEvents = "none"; // Disable pointer events on the overlay
}, 1000);
}
Also see: Tab Triggers