Close Menu
Trendswave MediaTrendswave Media
  • Home
  • About Us
    • Contact Us
  • Services
  • Influencer Marketing
  • Marketing
  • SEO
  • Social Media
  • Web Design
  • Shop

Subscribe to Updates

Get the latest creative news from Trendswave about Marketing, SEO & Web Design.

Please enable JavaScript in your browser to complete this form.
Loading
What's Hot

Daily Search Forum Recap: January 23, 2026

January 23, 2026

The Rise Of Agentic AI And User-Centric Design — Smashing Magazine

January 23, 2026

Enhance Your Videos Ranking & Views -TWH

January 22, 2026
Facebook X (Twitter) Instagram
Trendswave MediaTrendswave Media
  • Home
  • About Us
    • Contact Us
  • Services
  • Influencer Marketing
  • Marketing
  • SEO
  • Social Media
  • Web Design
  • Shop
Trendswave MediaTrendswave Media
Home»Web Design»How to create animated snow on a website (with CSS and JavaScript)
Web Design

How to create animated snow on a website (with CSS and JavaScript)

adminBy adminDecember 29, 2024No Comments5 Mins Read
Facebook Twitter Pinterest LinkedIn Tumblr WhatsApp VKontakte Email
How to create animated snow on a website (with CSS and JavaScript)
Share
Facebook Twitter LinkedIn Pinterest Email


‘Tis the season to be jolly so let it snow, let it snow, let it snow!

1. Markup with holiday HTML

For our markup, we’ll have two primary containers: a main element containing the content of our page and a snow-container element that will display the snow.

1
2
  Happy Holidays!
3

4

id="snow-container">

5

2. Animated snow styling with CSS

In this demo, our main content has a gradient background colour to give it a night sky effect. We achieve this using the CSS linear-gradient function. 

For simplicity, let’s say we’re building this animated snow in CodePen, so the markup goes in the HTML tab, the styles in the CSS tab, etc.

1
main {
2
  background: linear-gradient(to bottom, #2d91c2 0%, #1e528e 100%);
3
  display: flex;
4
  align-items: center;
5
  justify-content: center;
6
  font-family: "Pacifico", cursive;
7
  height: 100vh;
8
  padding: 20px;
9
  text-align: center;
10
}
11

12
h1 {
13
  color: white;
14
}

Here’s what our banner looks like now:

A blue gradient banner with text Happy HolidaysA blue gradient banner with text Happy HolidaysA blue gradient banner with text Happy Holidays

We’ll also use CSS to handle the animation of each snow element. Each snowflake will have two animations: a fall animation to handle the snowflake moving from the top to the bottom of the screen and a sway animation to handle moving the snowflake slowly side to side.

1
@keyframes fall {
2
  0% {
3
    opacity: 0;
4
  }
5
  50% {
6
    opacity: 1;
7
  }
8
  100% {
9
    top: 100vh;
10
    opacity: 1;
11
  }
12
}
13

14
@keyframes sway {
15
  0% {
16
    margin-left: 0;
17
  }
18
  25% {
19
    margin-left: 50px;
20
  }
21
  50% {
22
    margin-left: -50px;
23
  }
24
  75% {
25
    margin-left: 50px;
26
  }
27
  100% {
28
    margin-left: 0;
29
  }
30
}

We’ll also style our snow container and the snow element we’ll be creating in JavaScript.

1
#snow-container {  
2
  height: 100vh;
3
  overflow: hidden;
4
  position: absolute;
5
  top: 0;
6
  transition: opacity 500ms;
7
  width: 100%;
8
}
9

10
.snow {
11
  animation: fall ease-in infinite, sway ease-in-out infinite;
12
  color: skyblue;
13
  position: absolute;
14
}

The animation property in this demo takes three values:

  • name: This is the value of the animation we created under keyframes.
  • timing: This determines how the animation progresses.
  • iteration: This is the number of times the animation happens. We use infinite to constantly repeat the animation of the snowflakes.

3. Fa la la la la functionality ♬

Now we can work on the fun part: making it snow! First, in the JS tab in CodePen, let’s assign a value to our container element:

1
const snowContainer = document.getElementById("snow-container");

For our snowflake content, we’ll be using HTML symbols (❄ ❅ ❆):

Snowflake characters using HTML codeSnowflake characters using HTML codeSnowflake characters using HTML code

We’ll create a snowContent array to contain the symbol codes

1
const snowContent = ['❄', '❅', '❆']

In order to create a falling snow effect, we’ll need to randomise the appearance and animation of each snowflake.

We’ll use the Math.random() and Math.floor() functions to handle generating the random values for our animated snow elements.

1
const random = (num) => {
2
  return Math.floor(Math.random() * num);
3
}

Now we can create a function to generate a random style for each snowflake. We’ll generate a random position, size and animation duration.

1
const getRandomStyles = () => {
2
  const top = random(100);
3
  const left = random(100);
4
  const dur = random(10) + 10;
5
  const size = random(25) + 25;
6
  return `
7
    top: -${top}%;
8
    left: ${left}%;
9
    font-size: ${size}px;
10
    animation-duration: ${dur}s;
11
  `;
12
}

For our duration and size constant, we add a fixed number value to ensure the random generated number has a minimum value of the number being added (i.e. the lowest number for the animation duration of any snowflake is 10s)

For our top constant, we assign a negative value so the snowflakes starts from any percentage above the visible screen.

Next, we’ll handle appending the snowflakes to the snow container.

1
const createSnow = (num) => {
2
  for (var i = num; i > 0; i--) {
3
    var snow = document.createElement("div");
4
    snow.className = "snow";
5
    snow.style.cssText = getRandomStyles();
6
    snow.innerHTML = snowContent[random(2)]
7
    snowContainer.append(snow);
8
  }
9
}

In this demo, we use a for loop to create a fixed number of snowflakes and append them to the container. We also randomly assign the innerHTML as any of the values in our snowContent array.

Then, we’ll call the createSnow function once our page loads using the load event listener.

1
window.addEventListener("load", () => {
2
  createSnow(30)
3
});

And it’s snowing! Click Rerun to see the animated snow:

Removing snowflakes

We can also choose to add a function to remove the snowflakes.

1
const removeSnow = () => {
2
  snowContainer.style.opacity = "0";
3
  setTimeout(() => {
4
    snowContainer.remove()
5
  }, 500)
6
}

In this function, we assign our snowContainer an opacity of 0 to allow it fade out smoothly and then, after half a second, we remove the container using the .remove() method.

We can then choose to call this function however we want. We can remove the snow when the user clicks anywhere on the page:

1
window.addEventListener("click", () => {
2
  removeSnow()
3
});

Or we can include it in setTimeout() function so it disappears after a certain amount of time:

1
window.addEventListener("load", () => {
2
  createSnow(30)
3
  setTimeout(removeSnow, 10000)
4
});

Switching out the snowflakes

Since we’ve built this from scratch, we can also update the element content and animation to create other fun effects, like floating balloons:

or even animated Nyan Cat:

I hope you enjoyed this animated snow tutorial, and it’s made you feel nice and festive!

More wintry and animated snow tutorials



Source link

animated Create CSS JavaScript snow Website
Share. Facebook Twitter Pinterest LinkedIn Tumblr WhatsApp Email
Previous ArticleThe creator economy in 2025: 9 experts on how to survive and thrive
Next Article Crawl Budget Optimization: A Complete Guide
admin
  • Website

Related Posts

The Rise Of Agentic AI And User-Centric Design — Smashing Magazine

January 23, 2026

Rethinking “Pixel Perfect” Web Design — Smashing Magazine

January 21, 2026

Theming Animations Using CSS Relative Colour — Smashing Magazine

January 15, 2026

UX And Product Designer’s Career Paths In 2026 — Smashing Magazine

January 13, 2026
Leave A Reply Cancel Reply

  • Facebook
  • Twitter
  • Pinterest
  • Instagram
  • YouTube
  • Vimeo
Don't Miss
SEO

Daily Search Forum Recap: January 23, 2026

By adminJanuary 23, 20260

Barry Schwartz is the CEO of RustyBrick and a technologist, a New York Web service…

The Rise Of Agentic AI And User-Centric Design — Smashing Magazine

January 23, 2026

Enhance Your Videos Ranking & Views -TWH

January 22, 2026

With Crossplay, The New York Times Gets Serious About Games

January 22, 2026

Subscribe to Updates

Get the latest creative news from Trendswave about Marketing, SEO & Web Design.

Please enable JavaScript in your browser to complete this form.
Loading
About Us

Trendswave is an Influencer Marketing Agency with access to one of the largest influencer networks in the Poland, connecting brands and agencies to only the best influencers and social media thought leaders.

Our Picks

Daily Search Forum Recap: January 23, 2026

January 23, 2026

The Rise Of Agentic AI And User-Centric Design — Smashing Magazine

January 23, 2026
Quicklinks
  • Influencer Marketing
  • Marketing
  • SEO
  • Social Media
  • Web Design
  • About Us
  • Contact Us
  • Disclaimer
  • Privacy Policy
  • Terms and Conditions
© 2026 Trendswave.All Rights Reserved

Type above and press Enter to search. Press Esc to cancel.