Snowflake On Click With HTML,CSS and JavaScript

Snowflake On Click With HTML,CSS and JavaScript

Snowflake On Click With HTML,CSS and JavaScript

Hello everyone. Welcome to today’s tutorial on Codewithrandom. We’ll learn how to make Generate Snowflake which will generate the snowflake type snowfall as per the click of the user on the project screen it is basically a digital snowfall. In Today’s session, We will use HTML, CSS, and JavaScript to complete this Generate Snowflake Project.

Project Description

Step 1
The HTML (Hypertext Markup Language) will help us to create the structure for the list with some necessary attributes and elements to make Generate Snowflake Project.

Step 2
Then we will use CSS (Cascading Stylesheet) which will help us to style or design the project with suitable padding and alignment in the Generate Snowflake Project.

Step 3
At last we will use JS (JavaScript) which will add a logic to make the Generate Snowflake Project responsive from the user end.

I hope you have got an idea about the project.

HTML Code for Generate Snowflake

First we’ll start with creating the structure of the Generate Snowflake project for that as you can see the above code we have used all the necessary elements & attributes to setup the structure. Let us know code the CSS part to add styling and aligned the tags.

<html lang="en">
  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Snowflake On Click</title>
    <!-- Stylesheet -->
    <link rel="stylesheet" href="style.css" />
  </head>
  <body>
    <!-- Script -->
    <script src="script.js"></script>
  </body>
</html>

50+ HTML, CSS & JavaScript Projects With Source Code

CSS Code for Generate Snowflake

Second comes the CSS code in which we have styled for the structure we have padded as well as aligned the Generate Snowflake project so that it is properly situated and doesn’t get messy with suitable CSS elements. Now lets code the JavaScript part to make responsive.

* {
  margin: 0;
  box-sizing: border-box;
}
body {
  height: 100vh;
  background: linear-gradient(#12a7f9, #008aef);
  overflow: hidden;
}
.snowflake {
  position: absolute;
  background-image: url("https://i.postimg.cc/TPSBWW9m/snowflake-image.png");
  background-size: cover;
  border-radius: 50px;
  animation: fall 2s linear infinite;
}
@keyframes fall {
  0% {
    transform: translate(50%, 50%);
    opacity: 1;
  }
  100% {
    transform: translate(50%, 1000%);
    opacity: 0;
  }
}

JavaScript Code for Generate Snowflake

Last stage of the project the JavaScript in which we have added the logical and coded as per the requirement with some conditions. Let us see the Final Output of the project Generate Snowflake using HTML, CSS & JavaScript (Source Code).

document.addEventListener("click", (e) => {
  //create span for snowflake
  var snowflake = document.createElement("span");
  snowflake.classList.add("snowflake");

  //set position of snowflake to mouse pointer's position
  snowflake.style.left = e.offsetX + "px";
  snowflake.style.top = e.offsetY + "px";

  //select random number between 20 and 100
  var size = Math.random() * (100 - 20 + 1) + 20;

  //set width and height
  snowflake.style.width = size + "px";
  snowflake.style.height = size + "px";
  document.body.appendChild(snowflake);
  setTimeout(() => {
    snowflake.remove();
  }, 1000);
});

Restaurant Website Using HTML and CSS

Final Output


We have Successfully created our Generate Snowflake using HTML, CSS & JavaScript (Source Code). You can use this project for your personal needs and the respective lines of code are given with the code pen link mentioned above.

If you find out this Blog helpful, then make sure to search code with random on google for Front End Projects with Source codes and make sure to Follow the Code with Random Instagram page.

Code Idea – codingartist

Written By – Harsh Sawant

Code By – @harshh9



Leave a Reply