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="countdown-container">
<h1>Birthday Countdown</h1>
<div id="confetti" class="container"></div>
<div class="input-container">
<input type="date" id="birthday-input">
<button id="start-countdown">Start Countdown</button>
</div>
<div class="countdown" id="countdown">
<div class="countdown-item">
<span id="days">00</span>
<p>Days</p>
</div>
<div class="countdown-item">
<span id="hours">00</span>
<p>Hours</p>
</div>
<div class="countdown-item">
<span id="minutes">00</span>
<p>Minutes</p>
</div>
<div class="countdown-item">
<span id="seconds">00</span>
<p>Seconds</p>
</div>
</div>
</div>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
overflow: hidden;
}
.countdown-container {
text-align: center;
background-color: #fff;
padding: 20px;
border-radius: 10px;
box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.2);
max-width: 500px;
width: 90%;
}
h1 {
font-size: 2rem;
margin-bottom: 20px;
}
.input-container {
margin-bottom: 20px;
}
input[type="date"] {
padding: 8px;
font-size: 1rem;
}
button {
background-color: #f44336;
color: #fff;
border: none;
padding: 10px 20px;
font-size: 1rem;
cursor: pointer;
}
button:hover {
background-color: #d32f2f;
}
.countdown {
display: flex;
justify-content: center;
align-items: center;
}
.countdown-item {
margin: 10px;
font-size: 2rem;
flex: 1;
text-align: center;
}
.countdown-item span {
font-size: 3rem;
font-weight: bold;
color: #f44336;
}
.container {
width: 100vw;
}
.confetti {
position: absolute;
z-index: 1;
top: -10px;
border-radius: 0%;
}
.confetti--animation-slow {
animation: confetti-slow 2.25s linear 1 forwards;
}
.confetti--animation-medium {
animation: confetti-medium 1.75s linear 1 forwards;
}
.confetti--animation-fast {
animation: confetti-fast 1.25s linear 1 forwards;
}
@keyframes confetti-slow {
0% {
transform: translate3d(0, 0, 0) rotateX(0) rotateY(0);
}
100% {
transform: translate3d(25px, 105vh, 0) rotateX(360deg) rotateY(180deg);
}
}
@keyframes confetti-medium {
0% {
transform: translate3d(0, 0, 0) rotateX(0) rotateY(0);
}
100% {
transform: translate3d(100px, 105vh, 0) rotateX(100deg) rotateY(360deg);
}
}
@keyframes confetti-fast {
0% {
transform: translate3d(0, 0, 0) rotateX(0) rotateY(0);
}
100% {
transform: translate3d(-50px, 105vh, 0) rotateX(10deg) rotateY(250deg);
}
}
/* Media Queries for Responsive Design */
@media (max-width: 600px) {
.countdown-item {
font-size: 1.5rem;
}
.countdown-item span {
font-size: 2.5rem;
}
}
let birthdayDate;
document.getElementById("start-countdown").addEventListener("click", function () {
const inputDate = document.getElementById("birthday-input").value;
if (!isValidDate(inputDate)) {
alert("Please enter a valid date (YYYY-MM-DD).");
return;
}
birthdayDate = new Date(inputDate + "T14:03:00").getTime();
document.querySelector(".input-container").style.display = "none"; // Hide input fields
updateCountdown();
});
function updateCountdown() {
const countdownElement = document.getElementById("countdown");
const currentDate = new Date().getTime();
const timeLeft = birthdayDate - currentDate;
if (timeLeft <= 0) {
// Birthday has passed
countdownElement.innerHTML = "<h2>Happy Birthday!</h2>";
// Trigger confetti effect
confettiEffect();
return;
}
const days = Math.floor(timeLeft / (1000 * 60 * 60 * 24));
const hours = Math.floor((timeLeft % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
const minutes = Math.floor((timeLeft % (1000 * 60 * 60)) / (1000 * 60));
const seconds = Math.floor((timeLeft % (1000 * 60)) / 1000);
document.getElementById("days").textContent = days.toString().padStart(2, '0');
document.getElementById("hours").textContent = hours.toString().padStart(2, '0');
document.getElementById("minutes").textContent = minutes.toString().padStart(2, '0');
document.getElementById("seconds").textContent = seconds.toString().padStart(2, '0');
// Update the countdown every second
setTimeout(updateCountdown, 1000);
}
// Function to validate the date format
function isValidDate(dateString) {
const regEx = /^\d{4}-\d{2}-\d{2}$/;
if (!dateString.match(regEx)) return false; // Invalid format
const d = new Date(dateString + "T00:00:00");
return !isNaN(d.getTime());
}
function confettiEffect() {
const Confettiful = function (el) {
this.el = el;
this.containerEl = null;
this.confettiFrequency = 3;
this.confettiColors = ['#fce18a', '#ff726d', '#b48def', '#f4306d'];
this.confettiAnimations = ['slow', 'medium', 'fast'];
this._renderConfetti();
};
Confettiful.prototype._renderConfetti = function () {
const confettiContanier = document.getElementById("confetti");
this.confettiInterval = setInterval(() => {
const confettiEl = document.createElement('div');
const confettiSize = Math.floor(Math.random() * 3) + 7 + 'px';
const confettiBackground = this.confettiColors[Math.floor(Math.random() * this.confettiColors.length)];
const confettiLeft = Math.floor(Math.random() * this.el.offsetWidth) + 'px';
const confettiAnimation = this.confettiAnimations[Math.floor(Math.random() * this.confettiAnimations.length)];
confettiEl.classList.add('confetti', 'confetti--animation-' + confettiAnimation);
confettiEl.style.left = confettiLeft;
confettiEl.style.width = confettiSize;
confettiEl.style.height = confettiSize;
confettiEl.style.backgroundColor = confettiBackground;
confettiEl.removeTimeout = setTimeout(function () {
confettiEl.parentNode.removeChild(confettiEl);
}, 3000);
confettiContanier.appendChild(confettiEl);
}, 25);
};
window.confettiful = new Confettiful(document.querySelector('.container'));
}
Also see: Tab Triggers