Simple Slider with HTML5 Range Input Type
Unlike HTML, HTML5 intoduces a new range type of Input. The range type is used for input fields that have values from a range of numbers. So here in this post we will learn how to create smooth slider using HTML5 Range Input type.
- <input type=“range”>
There are attributes like max, min, step and value by which we can set restrictions on what numbers are accepted.
So from here we will proceed to the real example of HTML5 Range Input by creating a simple Slider step by step.
Step 1: Create HTML5 Mark-up
First, we will create example markup using simple form element and input type
- <form method=“post”>
- <h1> HTML5 Simple Slider using Range Input </h1>
- <input type=“range” name=“slider” min=“0″ max=“10″ step=“1″ value=“”>
- <output name=“sliderresult”> </output>
- </form>
Step 2: Design and CSS
Here in this step we’ll handle design and style. in below css we have used :before and :after attribue to know what are specified min and max values.
- body {
- font-family: ‘Verdana,Arial,Helvetica,sans-serif’;
- text-align: center;
- }
- input { font-size: 16px; font-weight: bold; }
- input[type=range]:before { content: attr(min); padding-right: 10px; }
- input[type=range]:after { content: attr(max); padding-left: 10px;}
- output {
- display: block;
- font-size: 12px;
- font-weight: bold;
- }
In above css code we have create content before and after the range input, and make their values equal to the min and max values.
Step 3: JavaScript
Below is javascript example code that we we’ll use in our simple slider to make it more dynamic.
- (function() {
- var f = document.forms[0],
- slider = f['slider'],
- sliderresult = f['sliderresult'],
- cachedRangeValue = localStorage.rangeValue ? localStorage.rangeValue : 5;
- // range input.
- var o = document.createElement(‘input’);
- o.type = ‘range’;
- if ( o.type === ‘text’ ) alert(‘Sorry. Your browser is not supporting HTML5 new features. Try latest browser version.’);
- // Set initial values of the input
- slider.value = cachedRangeValue;
- sliderresult.value = cachedRangeValue;
- // update local storage when make slider selection.
- slider.addEventListener(“mouseup”, function() {
- localStorage ? (localStorage.rangeValue = slider.value) : alert(“Save data to database or anywhere else.”);
- }, false);
- // Display selected value when sliding.
- slider.addEventListener(“change”, function() {
- sliderresult.value = slider.value;
- }, false);
- })();
Till now all browsers are not support all HTML5 features but it’s fun to play with HTML5 new features and controls.
Follow @phpzag


Thanks for sharing superb informations. Your website is very cool. I am impressed by the details that you have on this blog. It reveals how nicely you perceive this subject. Bookmarked this web page, will come back for more articles. You, my friend, ROCK! I found just the info I already searched everywhere and simply couldn’t come across. What a great site.
http://uggslipper2.WebStarts.com