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.
<div class="game-container">
<h1 class="title">๐ Gift Dash Challenge ๐</h1>
<p class="instructions">Unwrap as many gifts as you can before time runs out! โจ</p>
<div class="score-timer">
<span class="score">Score: <span id="score">0</span> ๐ฏ</span>
<span class="timer">Time Left: <span id="timer">30</span>s โฑ๏ธ</span>
</div>
<div id="game-board" class="game-board"></div>
<button id="start-button" class="start-button">๐
Start the Challenge</button>
<p class="hint">๐ฎ Tip: Click fast and stay sharp, gifts wonโt wait forever!</p>
</div>
/* Global Styles */
body {
font-family: 'Poppins', sans-serif;
background: linear-gradient(135deg, #1e3c72, #2a5298);
margin: 0;
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
color: #fff;
overflow: hidden;
}
.game-container {
text-align: center;
max-width: 650px;
width: 90%;
background: rgba(255, 255, 255, 0.15);
border-radius: 20px;
box-shadow: 0 8px 20px rgba(0, 0, 0, 0.4);
padding: 30px;
backdrop-filter: blur(10px);
animation: fadeIn 1s ease-out;
}
/* Title and Instructions */
.title {
font-size: 3rem;
margin-bottom: 15px;
background: linear-gradient(90deg, #ff9a9e, #fad0c4);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
font-weight: bold;
}
.instructions {
font-size: 1.2rem;
margin-bottom: 20px;
color: #e8e8e8;
font-style: italic;
}
/* Score and Timer Section */
.score-timer {
display: flex;
justify-content: space-between;
margin-bottom: 20px;
font-size: 1.2rem;
font-weight: bold;
text-shadow: 0 2px 4px rgba(0, 0, 0, 0.6);
}
.score, .timer {
background: linear-gradient(90deg, #fad0c4, #ff9a9e);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
}
/* Game Board */
.game-board {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 20px;
justify-items: center;
margin: 20px 0;
padding: 10px;
}
/* Gift Boxes */
.gift {
width: 90px;
height: 90px;
background: linear-gradient(120deg, #f093fb, #f5576c);
border-radius: 15px;
display: flex;
justify-content: center;
align-items: center;
position: relative;
cursor: pointer;
box-shadow: 0 6px 15px rgba(0, 0, 0, 0.3);
animation: bounce 1s infinite;
transform-origin: center;
transition: transform 0.3s ease, box-shadow 0.3s ease;
}
.gift:hover {
transform: scale(1.2) rotate(-5deg);
box-shadow: 0 10px 25px rgba(0, 0, 0, 0.5);
}
.gift::before {
content: '๐';
font-size: 2.5rem;
}
.explosion {
position: absolute;
font-size: 2.5rem;
animation: pop 0.6s forwards;
}
/* Start Button */
.start-button {
background: linear-gradient(120deg, #43cea2, #185a9d);
color: #fff;
font-size: 1.2rem;
font-weight: bold;
padding: 12px 25px;
border: none;
border-radius: 50px;
cursor: pointer;
box-shadow: 0 4px 15px rgba(0, 0, 0, 0.3);
transition: all 0.3s ease;
}
.start-button:hover {
transform: scale(1.1);
box-shadow: 0 8px 20px rgba(0, 0, 0, 0.5);
}
/* Hint Text */
.hint {
font-size: 0.9rem;
color: #d3d3d3;
margin-top: 15px;
font-style: italic;
}
/* Animations */
@keyframes bounce {
0%, 100% {
transform: translateY(0);
}
50% {
transform: translateY(-8px);
}
}
@keyframes pop {
0% {
opacity: 1;
transform: scale(1);
}
100% {
opacity: 0;
transform: scale(1.8);
}
}
@keyframes fadeIn {
from {
opacity: 0;
transform: translateY(-20px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
.share-button {
display: inline-block;
margin-top: 20px;
padding: 12px 20px;
background: linear-gradient(120deg, #1da1f2, #0d8de0); /* Twitter blue gradient */
color: #fff;
font-size: 1rem;
font-weight: bold;
text-decoration: none;
border-radius: 50px;
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.3);
transition: transform 0.3s ease, box-shadow 0.3s ease;
text-align: center;
}
.share-button:hover {
transform: scale(1.1);
box-shadow: 0 8px 20px rgba(0, 0, 0, 0.4);
background: linear-gradient(120deg, #1a91da, #0a74bd); /* Slightly darker blue */
}
.share-button:active {
transform: scale(0.95);
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.5);
}
const gameBoard = document.getElementById("game-board");
const startButton = document.getElementById("start-button");
const scoreDisplay = document.getElementById("score");
const timerDisplay = document.getElementById("timer");
let score = 0;
let timeLeft = 30;
let giftInterval;
let timerInterval;
// Creates a gift with random placement on the game board
function createGift() {
const gift = document.createElement("div");
gift.classList.add("gift");
// Random position within the game board boundaries
const posX = Math.random() * (gameBoard.clientWidth - 80);
const posY = Math.random() * (gameBoard.clientHeight - 80);
gift.style.transform = `translate(${posX}px, ${posY}px)`;
// Add click event to unwrap the gift
gift.addEventListener("click", () => {
if (!gift.classList.contains("clicked")) {
gift.classList.add("clicked");
score++;
scoreDisplay.textContent = score;
// Add explosion effect
const explosion = document.createElement("div");
explosion.classList.add("explosion");
explosion.textContent = "โจ";
gift.appendChild(explosion);
// Remove the gift after the explosion animation
setTimeout(() => gift.remove(), 600);
}
});
// Append the gift to the game board
gameBoard.appendChild(gift);
// Automatically remove the gift if not clicked within 2 seconds
setTimeout(() => {
if (!gift.classList.contains("clicked")) {
gift.remove();
}
}, 2000);
}
// Starts the game
function startGame() {
resetGame();
startButton.disabled = true;
// Controls gift spawn rate and increases difficulty over time
let spawnRate = 800;
giftInterval = setInterval(() => {
if (timeLeft > 0) {
createGift();
if (timeLeft % 10 === 0 && spawnRate > 400) {
clearInterval(giftInterval);
spawnRate -= 100; // Gifts spawn faster as time progresses
giftInterval = setInterval(createGift, spawnRate);
}
}
}, spawnRate);
// Countdown timer
timerInterval = setInterval(() => {
if (timeLeft > 0) {
timeLeft--;
timerDisplay.textContent = timeLeft;
} else {
clearInterval(timerInterval);
clearInterval(giftInterval);
endGame();
}
}, 1000);
}
// Resets the game to its initial state
function resetGame() {
score = 0;
timeLeft = 30;
scoreDisplay.textContent = score;
timerDisplay.textContent = timeLeft;
gameBoard.innerHTML = "";
}
// Ends the game and displays the final score with a share button
// Ends the game and displays the final score with a share button
function endGame() {
startButton.disabled = false;
const tweetText = encodeURIComponent(
`๐ I just scored ${score} points in the Gift Dash Challenge! ๐โจ Can you beat my score? Play now and spread the holiday cheer: https://codepen.io/HanGPIIIErr/pen/emOvJaz #GiftDash #HolidayFun #ChristmasGames`
);
const twitterShareUrl = `https://twitter.com/intent/tweet?text=${tweetText}`;
gameBoard.innerHTML = `
<div class="end-screen">
<h2>๐ Game Over!</h2>
<p>Your Final Score: <strong>${score}</strong></p>
<button id="restart-button" class="start-button">๐ Play Again</button>
<a href="${twitterShareUrl}" target="_blank" class="share-button">๐
Share on Twitter</a>
</div>
`;
const restartButton = document.getElementById("restart-button");
restartButton.addEventListener("click", startGame);
}
// Event listener for the start button
startButton.addEventListener("click", startGame);
Also see: Tab Triggers