RGBA Color Generator using JavaScript
Hello Guys! In this blog, I'm going to explain to you how to create RGBA Color Generator using JavaScript. This will be a step-by-step guide including HTML and CSS. Let's get started 🚀.
Let's cover the HTML part
HTML is a markup language. We use HTML to make the skeleton of a website.
Now let's import the font awesome CDN in our HTML head tag. fontawesome is a library that is used for icons in a website.
Now let's import the fonts using Google Fonts API. Below is the code for Poppins Font. Paste the below code in head tag.
In the below HTML code, we have a `<div>` with a class name 'card' which holds our sliders and output box. Inside sliders, we have label tags and input tags with type 'range'. `<input>` tag also has min and max limits. In the output box, we have an output which is the RGBA value, and a copy button to copy the RGBA value.
Here is the Final HTML Code
Output Till Now
Let's understand CSS Part
In the below CSS code,
- We declare a * selectors to set the font Poppins that we have imported in our head tag.
- First we have a card that has width, border-radius, padding, etc simple properties.
- Next we have styled output-box>output>copy-btn using simple CSS properties.
Output Till Now
Finally a JavaScript Part
In the below javascript code, we have two functions generateRGBA() and copy().
We have generateRGBA() function to generate a RGBA color. In the function, we have declared 4 variables for color input which are red, green, blue, and alpha. we grab the value of the respective element whenever the function is called, and store it in our variables. Next, we declare the output variable and grab the output div. Next, we set the output text using javascript string literals. Next, we set the background of the output div according to the value entered by the user.