Background Color Change Slider with HTML CSS and JavaScript

In this tutorial we will see how to change Background Color with Range Slider using HTML, CSS and JavaScript. We have used input range slider for this with CSS linear-gradient property and JavaScript setProperty method.


Take a look at the HTML code given below, in this we have a range type input element inside the div with class of container.

<div class="container">

<input type="range" min="0" max="100" value="0" class="slider" id="range">    

CSS Code

CSS code is given below, in this code we have changed appearance of our range slider using simple CSS properties.

    margin: 0;
    padding: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 100vh;
    background-color: #000;
    background-color: #fff;
    width: 100%;
    max-width: 250px;
    padding: 20px;
    border-radius: 10px;
    box-shadow: 0px 0px 3px 1px #777;
    width: 100%;
    height: 10px;
    max-width: 400px;
    background-color: #ccc;
    margin: 0;
    padding: 0;
    outline: none;
    border-radius: 10px;
    cursor: pointer;

JavaScript Code

Take a look at the JavaScript Code given below, in this code getElementsByTagName() method is used to select body tag and getElementById() method is used to select input range slider.

Then oninput event is attached with input range slider which will trigger a JavaScript function when the value of range slider is changed.

This value is read in real time and percentage of two colors for the CSS linear-gradient is calculated. Variable bg contains complete value of linear-gradient which is dynamic and changes with the value of input range slider.

In simple words this linear-gradient will change when the position of range slider is changed.

The linear-gradient background is then applied to the body of HTML page using .style javascript property and setProperty() method.

var body = document.getElementsByTagName('BODY')[0];
var sliderValue = document.getElementById('range');

sliderValue.oninput = function()

var color1 = this.value;
var color2 = 100-color1;    

var bg = 'linear-gradient(90deg, red, red '+color1+'%, red '+color1+'%, black '+color1+'% , black '+color2+'%)';'background', bg);    


Video Tutorial

Watch our video tutorial on Background Color Change Slider with HTML CSS and JavaScript.

Social Media Menu with Popup Tooltip using HTML and CSS Horizontal Navigation Menu with HTML and CSS Responsive Sidebar Menu with CSS and JavaScript Cool CSS Button with Hover Animation Pure CSS Custom Radio Buttons Custom Emoji Radio Buttons with CSS Color Picker Design with HTML CSS and JavaScript Custom Radio Buttons with CSS Engraved HTML Buttons With CSS Neumorphic Buttons Design with CSS Cool Profile Card Design with HTML CSS Neumorphic Search Bar Design with HTML and CSS Simple Login Form with Placeholder Animation Pure CSS Button with Folded Corner Background Color Change Slider with HTML CSS and JavaScript 404 Page Not Found Template Design with HTML and CSS Movies Blog Card Design with HTML and CSS Simple CSS Border Animation with before and after Selector Custom Social Select Dropdown Menu with CSS and JavaScript Facebook Offline and Online Notification with CSS and JS New Year 2022 Text Animation with CSS and JS Change Placeholder Color using CSS Custom Select Tag DropDown with CSS and JavaScript Simple Price Range Slider With HTML CSS and JavaScript Profile Card With Hover Effect using HTML and CSS Circle With Two Colors using CSS Simple FAQ Accordion With HTML CSS and JavaScript
Change font-size using JavaScript Get Font Size of any Tag using JavaScript Change Position of HTML Element using JavaScript How to Change Image on Hover with JavaScript How to Disable Button by Class Name in JavaScript How To Change Image Size with JavaScript How to change Image opacity with JavaScript How to Change image src on click with JavaScript How to get the lang attribute value in JavaScript How to Get attribute Value using JavaScript How To Check if Attribute Exists or not with JavaScript How To Count number of links on Page with JavaScript How To Scroll Page to Bottom using JavaScript How To Detect Shift Key Press with JavaScript Change Text Color On Hover with JavaScript Hide and Show div using JavaScript Get Button text with JavaScript Get textarea value with JavaScript Get table row Height with JavaScript Auto Increase width of input field with JavaScript Set Textarea maxLength with JavaScript Set Textarea Value with JavaScript JavaScript Count list items JavaScript set input field value Count Button Clicks with JavaScript Scroll Page to the Top on Click with JavaScript