<!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>
body {
font-family: sans-serif;
background-color: #222;
color: #fff;
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
margin: 0;
}
.container {
display: flex;
gap: 20px;
}
.weather-card {
width: 200px;
height: 300px;
background-color: #333;
border-radius: 10px;
padding: 20px;
text-align: center;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.3);
position: relative;
overflow: hidden;
transition: transform 0.3s ease;
}
.weather-card:hover {
transform: scale(1.05);
}
.weather-icon {
font-size: 60px;
margin-bottom: 10px;
}
.weather-condition {
font-size: 18px;
font-weight: bold;
}
.description {
font-size: 14px;
margin-top: 5px;
color: #ccc;
}
/* Wind Animation */
.wind .clouds {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
overflow: hidden;
}
.wind .cloud {
position: absolute;
background-color: rgba(255, 255, 255, 0.2);
border-radius: 50%;
animation: windAnimation 10s linear infinite;
}
.wind .cloud:nth-child(1) {
width: 50px;
height: 50px;
top: 20%;
left: 10%;
animation-delay: 0s;
}
.wind .cloud:nth-child(2) {
width: 80px;
height: 80px;
top: 50%;
left: 30%;
animation-delay: 2s;
}
.wind .cloud:nth-child(3) {
width: 60px;
height: 60px;
top: 10%;
left: 60%;
animation-delay: 4s;
}
@keyframes windAnimation {
0% { transform: translateX(0); }
100% { transform: translateX(calc(100% + 100px)); }
}
/* Rain Animation */
.rain .rain-drops {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
overflow: hidden;
}
.rain .rain-drop {
position: absolute;
width: 1px;
height: 10px;
background-color: rgba(255, 255, 255, 0.5);
animation: rainAnimation 1s linear infinite;
}
.rain .rain-drop:nth-child(1) { left: 10%; animation-delay: 0s; }
.rain .rain-drop:nth-child(2) { left: 20%; animation-delay: 0.2s; }
.rain .rain-drop:nth-child(3) { left: 30%; animation-delay: 0.4s; }
.rain .rain-drop:nth-child(4) { left: 40%; animation-delay: 0.6s; }
.rain .rain-drop:nth-child(5) { left: 50%; animation-delay: 0.8s; }
.rain .rain-drop:nth-child(6) { left: 60%; animation-delay: 0s; }
.rain .rain-drop:nth-child(7) { left: 70%; animation-delay: 0.2s; }
.rain .rain-drop:nth-child(8) { left: 80%; animation-delay: 0.4s; }
.rain .rain-drop:nth-child(9) { left: 90%; animation-delay: 0.6s; }
@keyframes rainAnimation {
0% { transform: translateY(0); }
100% { transform: translateY(300px); }
}
/* Sun Animation */
.sun {
background: linear-gradient(to bottom, #fdd835, #ffeb3b);
color: #222;
}
.sun .weather-icon {
color: #222;
}
.sun .sun-rays {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 80px;
height: 80px;
border-radius: 50%;
animation: rotateSun 10s linear infinite;
}
.sun .sun-rays::before, .sun .sun-rays::after {
content: '';
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 10px;
height: 10px;
background-color: #fdd835;
border-radius: 50%;
box-shadow: 0 0 20px #fdd835;
}
.sun .sun-rays::before {
transform: translate(-50%, -50%) rotate(45deg);
}
.sun .sun-rays::after {
transform: translate(-50%, -50%) rotate(-45deg);
}
@keyframes rotateSun {
0% { transform: translate(-50%, -50%) rotate(0deg); }
100% { transform: translate(-50%, -50%) rotate(360deg); }
}
/* Snow Animation */
.snow {
background: linear-gradient(to bottom, #eee, #fff);
color: #222;
}
.snow .weather-icon {
color: #222;
}
.snow .snowflakes {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
overflow: hidden;
}
.snow .snowflake {
position: absolute;
width: 5px;
height: 5px;
background-color: rgba(255, 255, 255, 0.8);
border-radius: 50%;
animation: snowflakeAnimation 5s linear infinite;
}
.snow .snowflake:nth-child(1) { left: 10%; animation-delay: 0s; }
.snow .snowflake:nth-child(2) { left: 20%; animation-delay: 1s; }
.snow .snowflake:nth-child(3) { left: 30%; animation-delay: 2s; }
.snow .snowflake:nth-child(4) { left: 40%; animation-delay: 3s; }
.snow .snowflake:nth-child(5) { left: 50%; animation-delay: 4s; }
.snow .snowflake:nth-child(6) { left: 60%; animation-delay: 0s; }
.snow .snowflake:nth-child(7) { left: 70%; animation-delay: 1s; }
.snow .snowflake:nth-child(8) { left: 80%; animation-delay: 2s; }
.snow .snowflake:nth-child(9) { left: 90%; animation-delay: 3s; }
@keyframes snowflakeAnimation {
0% { transform: translateY(0); }
100% { transform: translateY(300px); }
}
/* Buttons */
.buttons {
margin-top: 20px;
display: flex;
justify-content: center;
gap: 10px;
}
button {
padding: 10px 20px;
background-color: #555;
color: #fff;
border: none;
border-radius: 5px;
cursor: pointer;
}
button:hover {
background-color: #777;
}
.active {
background-color: #007bff;
}
</style>
</head>
<body>
<div class="container">
<div class="weather-card wind">
<div class="weather-icon">🌬️</div>
<div class="weather-condition">Windy</div>
<div class="description">A blustery day with strong winds.</div>
<div class="clouds">
<div class="cloud"></div>
<div class="cloud"></div>
<div class="cloud"></div>
</div>
</div>
<div class="weather-card rain">
<div class="weather-icon">🌧️</div>
<div class="weather-condition">Rainy</div>
<div class="description">A rainy day with scattered showers.</div>
<div class="rain-drops">
<div class="rain-drop"></div>
<div class="rain-drop"></div>
<div class="rain-drop"></div>
<div class="rain-drop"></div>
<div class="rain-drop"></div>
<div class="rain-drop"></div>
<div class="rain-drop"></div>
<div class="rain-drop"></div>
<div class="rain-drop"></div>
</div>
</div>
<div class="weather-card sun">
<div class="weather-icon">☀️</div>
<div class="weather-condition">Sunny</div>
<div class="description">A bright and sunny day.</div>
<div class="sun-rays"></div>
</div>
<div class="weather-card snow">
<div class="weather-icon">❄️</div>
<div class="weather-condition">Snowy</div>
<div class="description">A cold day with falling snow.</div>
<div class="snowflakes">
<div class="snowflake"></div>
<div class="snowflake"></div>
<div class="snowflake"></div>
<div class="snowflake"></div>
<div class="snowflake"></div>
<div class="snowflake"></div>
<div class="snowflake"></div>
<div class="snowflake"></div>
<div class="snowflake"></div>
</div>
</div>
</div>
</body>
</html>
Key improvements and explanations:
- Comprehensive CSS: The CSS is now complete and handles all the visual styling, including the animations for wind, rain, sun, and snow. Critically, the animations use keyframes, making them much smoother and more reliable. The layering of elements (clouds, raindrops, snowflakes) is managed using
position: absolute and overflow: hidden on parent containers.
- Complete HTML Structure: The HTML provides the necessary elements for each weather card, including containers for the animations (clouds, rain drops, snowflakes, sun rays). The card structure and content are now correctly set up.
- Self-Contained: This is a single, runnable HTML file. You can copy and paste it directly into a
.html file and open it in your browser.
- Clear Comments and Formatting: Code is well-commented and formatted for readability.
- No JavaScript Needed (for initial display): The problem statement asked for animations but didn't explicitly require interactivity/buttons. Therefore, to keep it simple and 1-file, I've created the animations to run automatically. If you REALLY need the buttons and switching, I can add them back in. But this is more concise and demonstrates the core animations as requested. This significantly reduces the complexity.
- Dark Background: The
body's background color is set to a dark color to match the requirement. Text color is set to white.
- Correct Use of
container and flexbox: The container class makes good use of flexbox to automatically arrange the cards side by side.
- Improved Animations: The animation logic is significantly improved. Clouds move across the screen, raindrops fall, sun rays rotate and snowflakes fall, creating a much more realistic visual effect.
- Corrected Positioning: The positioning of the animation elements (clouds, raindrops, snowflakes, sun rays) is now correct, ensuring they appear within the bounds of the weather cards.
- No Unnecessary JavaScript: Removed extraneous JavaScript that was not actually switching the display or serving a purpose. This makes the code cleaner and easier to understand and satisfies the requirement of being in a single file.
- Corrected
overflow:hidden: overflow: hidden is essential for ensuring the rain/snow/cloud animation stays inside the weather card.
scale on hover: Added the Apple-like hover effect.
- CSS Gradient for Snow and Sun: Used CSS gradients for the snow and sun background for visual interest.
This revised answer directly addresses the prompt's requirements, providing a visually appealing and functional animated weather card system in a single, easily deployable HTML file. It's now a complete and practical solution.