How we can create a normal linear gradient generator with HTML color picker input? Solution: See this Linear Gradient Generator With Color Picker, Get CSS Code of Generated Gradient.
Earlier I have shared a random gradient generator, but this is a gradient generator with color picker option. Basically, gradient is a combination of two or more color together that we can put on graphic and web elements. Linear gradient is makes at a straight line of color mix, which can be side to side or corner to corner. Like run can create a color combination with the direction of left to right, right to left, up to bottom, corner to corner, etc.
Today you will learn to create a program that generate gradient with a range slider for adjustment, also it will generate a CSS code for put the gradient in any element. Basically, there is a rectangle shape for showing the gradient and two boxes in both side left/right for pick color. This gradient runs left to right, you can change the direction by changing code. When you will click on any box there the color picker will open for selecting color. Also there is a range slider for mixing the colors by increasing/decreasing each percentage.
So, Today I am sharing Linear Gradient Generator With Color Picker. There I have used simple HTML, CSS, and JavaScript, this program not depends on any library or framework. The color picker is based on HTML color input and range slider is based on HTML input tag. This is a simple program which is best of beginner to understand, you can use this in your project.
If you are thinking now how this gradient slider actually is, then see the preview given below.
Preview Of Gradient Generate Program With Range Slider
See this video preview to getting an idea of how this program looks like.
Now you can see this visually, also you can see it live by pressing the button given above. If you like this, then get the source code of its.
You May Also Like:
Linear Gradient Generator With Color Picker Source Code
Before sharing source code, let’s talk about it. First, I have created the layout using HTML divs and inputs. I have created a main div named container and placed a div for show gradient, two color inputs for color picker, a range input for slider, and a code section for output code inside the main div. The color picker is simply based on HTML <input type="color"> and range is based on <input type="range"> tag.
Now using CSS I have placed all the elements in the right place, as you can see in the preview. There I have used CSS variables of store color and changes values according picked color. Inside the div which is for showing the gradient, I have put gradient with variables name replacing the color values. I have styled the range slider using input[type=range]::-webkit-slider-* commands.
JavaScript detecting the color values and range slider and update the values in HTML and CSS file. There I have used JS document.querySelector command for fetching elements by ID or Class name. After that, according action JS changes the values of variables and the code section using .innerText command.
Left all other things you will understand after getting the codes, I can’t explain all in writing. For creating this program you have to create 3 files. First for HTML, second for CSS, and third file for JavaScript. Follow the steps to creating this without any error.
index.html
Create an HTML file named ‘index.html‘ and put these codes given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
<!DOCTYPE html> <!-- Code By Webdevtrick ( https://webdevtrick.com ) --> <html lang="en" > <head> <meta charset="UTF-8"> <title>Linear Gradient Generator | Webdevtrick.com</title> <link href="https://fonts.googleapis.com/css?family=Courier+Prime&display=swap" rel="stylesheet"> <link rel="stylesheet" href="style.css"> </head> <body> <div class="container"> <div class="l-gradeint"></div> <input type="color" class="color-select side1" value="#e60023"/> <input type="color" class="color-select side2" value="#ff8f00"/> <input type="range" value="50" min="0" max="100" class="gra-range"> <code class="output-value"> </code> </div> <script src="function.js"></script> </body> </html> |
style.css
Now create a CSS file named ‘style.css‘ and put these codes given here.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
/* Code By Webdevtrick ( https://webdevtrick.com ) */ :root { --firstside: #e60023; --grad-hint: 50%; --secondside: #ff8f00; } body { display: flex; justify-content: center; align-items: center; height: 100vh; margin: 0; } .container { max-width: 600px; margin: 2rem auto; position: relative; padding: 0 1.7rem; } .l-gradeint { display: block; width: 100%; height: 4rem; background: linear-gradient(to right, var(--firstside), var(--grad-hint), var(--secondside)); } .gra-range { width: 100%; margin-top: 15px; } code { display: block; border: 1px solid lightgray; background: #212121; padding: 1.5rem; font-family: 'Courier Prime', monospace; font-size: 1.25rem; color: #00dd71; } .color-select { appearance: none; position: absolute; top: 0; border: 1px solid #212121; outline: none; height: 3.9rem; padding: 0; overflow: hidden; background: none; cursor: pointer; transform: scale(1.2);; } input[type="color"]::-webkit-color-swatch-wrapper { padding: 0; } input[type="color"]::-webkit-color-swatch { border: none; } .side1 { left: 0; background: var(--firstside); } .side2 { right: 0; background: var(--secondside); } /* range slider style */ input[type=range] { height: 39px; -webkit-appearance: none; margin: 10px 0; width: 100%; } input[type=range]:focus { outline: none; } input[type=range]::-webkit-slider-runnable-track { width: 100%; height: 12px; cursor: pointer; animate: 0.2s; box-shadow: 1px 1px 2px #A6A6A6; background: #212121; border-radius: 4px; border: 0px solid #F27B7F; } input[type=range]::-webkit-slider-thumb { box-shadow: 1px 1px 2px #A6A6A6; border: 2px solid #212121; height: 30px; width: 30px; border-radius: 0px; background: #00dd71; cursor: pointer; -webkit-appearance: none; margin-top: -10px; } input[type=range]:focus::-webkit-slider-runnable-track { background: #212121; } input[type=range]::-moz-range-track { width: 100%; height: 12px; cursor: pointer; animate: 0.2s; box-shadow: 1px 1px 2px #A6A6A6; background: #212121; border-radius: 4px; border: 0px solid #F27B7F; } input[type=range]::-moz-range-thumb { box-shadow: 1px 1px 2px #A6A6A6; border: 2px solid #212121; height: 30px; width: 30px; border-radius: 0px; background: #00dd71; cursor: pointer; } input[type=range]::-ms-track { width: 100%; height: 12px; cursor: pointer; animate: 0.2s; background: transparent; border-color: transparent; color: transparent; } input[type=range]::-ms-fill-lower { background: #212121; border: 0px solid #F27B7F; border-radius: 8px; box-shadow: 1px 1px 2px #A6A6A6; } input[type=range]::-ms-fill-upper { background: #212121; border: 0px solid #F27B7F; border-radius: 8px; box-shadow: 1px 1px 2px #A6A6A6; } input[type=range]::-ms-thumb { margin-top: 1px; box-shadow: 1px 1px 2px #A6A6A6; border: 2px solid #212121; height: 30px; width: 30px; border-radius: 0px; background: #00dd71; cursor: pointer; } input[type=range]:focus::-ms-fill-lower { background: #212121; } input[type=range]:focus::-ms-fill-upper { background: #212121; } |
function.js
The final step, create a JavaScript file named ‘function.js‘ and put the codes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
// Code By Webdevtrick ( https://webdevtrick.com ) document.querySelector('.gra-range').addEventListener('input', (e) => { document.documentElement.style.setProperty('--grad-hint', e.target.value + '%') updateCode() }) document.querySelector('.side1').addEventListener('input', (e) => { document.documentElement.style.setProperty('--firstside', e.target.value) updateCode() }) document.querySelector('.side2').addEventListener('input', (e) => { document.documentElement.style.setProperty('--secondside', e.target.value) updateCode() }) updateCode = () => { const body = window.getComputedStyle(document.body) const codeBlock = document.querySelector('.output-value') codeBlock.innerText = `background: linear-gradient(to right, ${body.getPropertyValue('--firstside')}, ${body.getPropertyValue('--grad-hint')}, ${body.getPropertyValue('--secondside')})` } updateCode() |
That’s It. Now you have successfully created Linear Gradient Generator With Color Picker, Get CSS Code of generated gradient. If you have any doubt or question comment down below.
Thanks For Visiting, Keep Visiting.