Of course! Here is a single HTML file that contains all the necessary CSS and JavaScript to create four animated weather cards displayed side-by-side. Each card demonstrates a different weather condition: Sunny, Rainy, Windy, and Snowy.
html Copy code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Animated Weather Cards</title>
<style>
/* --- Global Styles & Layout --- */
:root {
--card-width: 220px;
--card-height: 300px;
}
body {
margin: 0;
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background-color: #121212;
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
padding: 2rem;
color: #fff;
}
.container {
display: flex;
flex-wrap: wrap;
gap: 2rem;
justify-content: center;
}
/* --- Base Weather Card Style --- */
.weather-card {
width: var(--card-width);
height: var(--card-height);
background: linear-gradient(160deg, #2c3e50, #2c3e50);
border-radius: 20px;
position: relative;
overflow: hidden;
box-shadow: 0 10px 25px rgba(0, 0, 0, 0.4);
display: flex;
flex-direction: column;
justify-content: flex-end;
padding: 20px;
box-sizing: border-box;
}
.card-content {
z-index: 10;
text-align: center;
text-shadow: 0 2px 5px rgba(0,0,0,0.5);
}
.card-content h2 {
margin: 0;
font-size: 1.8rem;
}
.card-content p {
margin: 5px 0 0;
font-size: 2.5rem;
font-weight: bold;
}
/* --- 1. Sunny Card --- */
.sunny {
background: linear-gradient(160deg, #ffc300, #ff5733);
}
.sun {
position: absolute;
top: 40px;
left: 50%;
transform: translateX(-50%);
width: 80px;
height: 80px;
background-color: #ffd700;
border-radius: 50%;
box-shadow: 0 0 40px #ffd700, 0 0 60px #ffc300;
animation: pulse 4s infinite ease-in-out;
}
.sun-ray {
position: absolute;
top: 50%;
left: 50%;
width: 2px;
height: 120px;
background: #ffd700;
transform-origin: top;
animation: spin 10s linear infinite;
}
.sun-ray:nth-child(1) { transform: rotate(0deg) translateY(-60px); }
.sun-ray:nth-child(2) { transform: rotate(45deg) translateY(-60px); }
.sun-ray:nth-child(3) { transform: rotate(90deg) translateY(-60px); }
.sun-ray:nth-child(4) { transform: rotate(135deg) translateY(-60px); }
@keyframes pulse {
0%, 100% { transform: translateX(-50%) scale(1); box-shadow: 0 0 40px #ffd700, 0 0 60px #ffc300; }
50% { transform: translateX(-50%) scale(1.1); box-shadow: 0 0 50px #fff, 0 0 80px #ffd700; }
}
@keyframes spin {
from { transform: rotate(0deg) translateY(-60px); }
to { transform: rotate(360deg) translateY(-60px); }
}
/* --- 2. Rainy Card --- */
.rainy {
background: linear-gradient(160deg, #485563, #29323c);
}
.raindrop {
position: absolute;
bottom: 100%;
width: 1px;
height: 50px;
background: linear-gradient(to top, rgba(255,255,255,0) 0%, rgba(255,255,255,0.4) 100%);
animation: fall 0.5s linear infinite;
}
.puddle {
position: absolute;
bottom: 0;
left: 0;
width: 100%;
height: 10px; /* Initial height */
background-color: rgba(72, 85, 99, 0.5);
border-top: 1px solid rgba(255, 255, 255, 0.2);
animation: fill-puddle 10s ease-out forwards;
}
@keyframes fall {
to {
transform: translateY(calc(var(--card-height) + 50px));
}
}
@keyframes fill-puddle {
to {
height: 30px;
}
}
/* --- 3. Windy Card --- */
.windy {
background: linear-gradient(160deg, #757f9a, #d7dde8);
}
.cloud {
position: absolute;
background: #fff;
border-radius: 50%;
z-index: 5;
animation: move-cloud 15s linear infinite;
}
.cloud-1 {
top: 50px;
left: -100px;
width: 100px;
height: 30px;
animation-duration: 15s;
}
.cloud-2 {
top: 90px;
left: -150px;
width: 120px;
height: 40px;
opacity: 0.8;
animation-duration: 12s;
animation-delay: -5s;
}
.tree {
position: absolute;
bottom: 20px;
left: 50%;
transform: translateX(-50%);
width: 10px;
height: 100px;
z-index: 6;
transform-origin: bottom center;
animation: sway 3s ease-in-out infinite alternate;
}
.trunk {
width: 10px;
height: 50px;
background-color: #5D4037;
position: absolute;
bottom: 0;
}
.leaves {
width: 60px;
height: 60px;
background-color: #2E7D32;
border-radius: 50%;
position: absolute;
bottom: 45px;
left: -25px;
}
@keyframes move-cloud {
from { left: -150px; }
to { left: calc(var(--card-width) + 50px); }
}
@keyframes sway {
from { transform: translateX(-50%) rotate(-5deg); }
to { transform: translateX(-50%) rotate(5deg); }
}
/* --- 4. Snowy Card --- */
.snowy {
background: linear-gradient(160deg, #3a506b, #1c2541);
}
.snowflake {
position: absolute;
top: -10px;
width: 8px;
height: 8px;
background: #fff;
border-radius: 50%;
opacity: 0;
animation: fall-snow linear infinite;
}
.snow-ground {
position: absolute;
bottom: 0;
left: 0;
width: 100%;
height: 0px;
background-color: #f0f0f0;
animation: accumulate-snow 20s ease-out forwards;
border-top: 2px solid #ddd;
}
@keyframes fall-snow {
0% {
transform: translate(0, 0);
opacity: 0;
}
10% {
opacity: 1;
}
100% {
transform: translate(20px, var(--card-height));
opacity: 0.5;
}
}
@keyframes accumulate-snow {
to {
height: 40px;
}
}
</style>
</head>
<body>
<div class="container">
<!-- Sunny Card -->
<div class="weather-card sunny">
<div class="sun"></div>
<div class="sun-rays">
<span class="sun-ray"></span>
<span class="sun-ray"></span>
<span class="sun-ray"></span>
<span class="sun-ray"></span>
</div>
<div class="card-content">
<h2>Sunny</h2>
<p>28°C</p>
</div>
</div>
<!-- Rainy Card -->
<div class="weather-card rainy" id="rainy-card">
<!-- Raindrops will be generated by JS here -->
<div class="puddle"></div>
<div class="card-content">
<h2>Rainy</h2>
<p>12°C</p>
</div>
</div>
<!-- Windy Card -->
<div class="weather-card windy">
<div class="cloud cloud-1"></div>
<div class="cloud cloud-2"></div>
<div class="tree">
<div class="trunk"></div>
<div class="leaves"></div>
</div>
<div class="card-content">
<h2>Windy</h2>
<p>15°C</p>
</div>
</div>
<!-- Snowy Card -->
<div class="weather-card snowy" id="snowy-card">
<!-- Snowflakes will be generated by JS here -->
<div class="snow-ground"></div>
<div class="card-content">
<h2>Snowy</h2>
<p>-2°C</p>
</div>
</div>
</div>
<script>
document.addEventListener('DOMContentLoaded', function() {
const cardWidth = 220; // Needs to match --card-width in CSS
// --- Generate Raindrops for the Rainy Card ---
const rainyCard = document.getElementById('rainy-card');
const numberOfRaindrops = 60;
for (let i = 0; i < numberOfRaindrops; i++) {
let raindrop = document.createElement('div');
raindrop.className = 'raindrop';
// Random horizontal position
raindrop.style.left = `${Math.random() * cardWidth}px`;
// Random animation delay and duration to make them appear at different times
raindrop.style.animationDelay = `${Math.random() * 4}s`;
raindrop.style.animationDuration = `${0.5 + Math.random() * 0.3}s`;
rainyCard.appendChild(raindrop);
}
// --- Generate Snowflakes for the Snowy Card ---
const snowyCard = document.getElementById('snowy-card');
const numberOfSnowflakes = 80;
for (let i = 0; i < numberOfSnowflakes; i++) {
let snowflake = document.createElement('div');
snowflake.className = 'snowflake';
// Random horizontal position
snowflake.style.left = `${Math.random() * cardWidth}px`;
// Random animation delay and duration
snowflake.style.animationDelay = `${Math.random() * 10}s`;
snowflake.style.animationDuration = `${5 + Math.random() * 5}s`;
// Random size and opacity for variety
const size = `${2 + Math.random() * 6}px`;
snowflake.style.width = size;
snowflake.style.height = size;
snowflake.style.opacity = Math.random();
// Add slight horizontal drift variation to the animation
// This makes the snow look more natural
snowflake.style.animationName = `fall-snow, drift-snow-${i % 2}`;
// Create unique keyframes for horizontal drift
const styleSheet = document.styleSheets[0];
const driftX = (Math.random() - 0.5) * 40; // a random horizontal drift
const keyframes =
`@keyframes drift-snow-${i % 2} {
from { transform: translateX(0); }
to { transform: translateX(${driftX}px); }
}`;
// Add a second animation for the horizontal drift
snowflake.style.animationTimingFunction = 'linear, ease-in-out';
snowflake.style.animationIterationCount = 'infinite, infinite';
snowflake.style.animationDirection = 'normal, alternate';
// Only add keyframes if they don't exist
let ruleExists = false;
for(let j = 0; j < styleSheet.cssRules.length; j++) {
if(styleSheet.cssRules[j].name === `drift-snow-${i % 2}`) {
ruleExists = true;
break;
}
}
if (!ruleExists) {
styleSheet.insertRule(keyframes, styleSheet.cssRules.length);
}
snowyCard.appendChild(snowflake);
}
});
</script>
</body>
</html>