Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Style input range before and after

Tags:

html

css

I was following this method from CSS-Tricks to style an input range and was trying to use the before and after pseudo classes. Below is the code I have tried:

input[type=range]::-webkit-slider-thumb:before {
    background: #fff;
}

This doesn't seem to do the trick. Can someone help me how to style the slider track. I need a pure CSS solution. Essentially I want it to look like this.

I will also paste the code from CSS-Tricks:

input[type=range] {
  -webkit-appearance: none;
  margin: 18px 0;
  width: 100%;
}
input[type=range]:focus {
  outline: none;
}
input[type=range]::-webkit-slider-runnable-track {
  width: 100%;
  height: 8.4px;
  cursor: pointer;
  animate: 0.2s;
  box-shadow: 1px 1px 1px #000000, 0px 0px 1px #0d0d0d;
  background: #3071a9;
  border-radius: 1.3px;
  border: 0.2px solid #010101;
}
input[type=range]::-webkit-slider-thumb {
  box-shadow: 1px 1px 1px #000000, 0px 0px 1px #0d0d0d;
  border: 1px solid #000000;
  height: 36px;
  width: 16px;
  border-radius: 3px;
  background: #ffffff;
  cursor: pointer;
  -webkit-appearance: none;
  margin-top: -14px;
}
input[type=range]:focus::-webkit-slider-runnable-track {
  background: #367ebd;
}
input[type=range]::-moz-range-track {
  width: 100%;
  height: 8.4px;
  cursor: pointer;
  animate: 0.2s;
  box-shadow: 1px 1px 1px #000000, 0px 0px 1px #0d0d0d;
  background: #3071a9;
  border-radius: 1.3px;
  border: 0.2px solid #010101;
}
input[type=range]::-moz-range-thumb {
  box-shadow: 1px 1px 1px #000000, 0px 0px 1px #0d0d0d;
  border: 1px solid #000000;
  height: 36px;
  width: 16px;
  border-radius: 3px;
  background: #ffffff;
  cursor: pointer;
}
input[type=range]::-ms-track {
  width: 100%;
  height: 8.4px;
  cursor: pointer;
  animate: 0.2s;
  background: transparent;
  border-color: transparent;
  border-width: 16px 0;
  color: transparent;
}
input[type=range]::-ms-fill-lower {
  background: #2a6495;
  border: 0.2px solid #010101;
  border-radius: 2.6px;
  box-shadow: 1px 1px 1px #000000, 0px 0px 1px #0d0d0d;
}
input[type=range]::-ms-fill-upper {
  background: #3071a9;
  border: 0.2px solid #010101;
  border-radius: 2.6px;
  box-shadow: 1px 1px 1px #000000, 0px 0px 1px #0d0d0d;
}
input[type=range]::-ms-thumb {
  box-shadow: 1px 1px 1px #000000, 0px 0px 1px #0d0d0d;
  border: 1px solid #000000;
  height: 36px;
  width: 16px;
  border-radius: 3px;
  background: #ffffff;
  cursor: pointer;
}
input[type=range]:focus::-ms-fill-lower {
  background: #3071a9;
}
input[type=range]:focus::-ms-fill-upper {
  background: #367ebd;
}
like image 969
ChaniLastnamé Avatar asked Jan 26 '15 22:01

ChaniLastnamé


People also ask

How do I style a range in CSS?

To style the range input with CSS you'll need to apply styles to two pseudo-elements: ::-webkit-slider-thumb and ::-webkit-slider-runnable-track . Find out how you can apply custom styling and make the range input more functional and appealing. Contents of the article: CSS selectors for the range input.

How do you change the input type range color in HTML?

If the general appearance of the slider is fine, but the default blue color (in Chrome) needs to fit a theme color, apply a filter: hue-rotate(); to the input[type="range"] element. Other filters can be used. Some even change the background color of the slider.

How do I change the color of my range?

1- Color Range SelectionWith the selection radio button active, click around the little preview area or in your image behind the dialog to select the color you want to select. You can then play around with the amount of Fuziness to tweak the range amount of similar colors that should become selected.

How do I style the input range tag?

You will be able to style all aspects of the input range tag and see the changes in the preview box below. Once you are happy with the style, simply copy and paste the generated css code into your project. If you need a bit of help to style the input tag, just select from one of our preset designs to get you started.

Is it possible to apply styles directly to input [type=range]?

Although it is possible to apply styles directly to input [type=range], you may run into problems with older browsers that have yet to implement to correct styles. Because of this we recommend using the vendor pseudo selectors to customize the range thumb and track.

Why is it so hard to style range inputs?

Quick summary ↬ Range inputs have notoriously been a pain to style. Each browser renders the input differently requiring you to use vendor prefixes in order to create a cohesive look and feel.

What does the default HTML range input look like?

Chrome demo of default HTML range input. ( Large preview) Firefox is next up and looks different from the Chrome rendered input. In Firefox, the height of the track is slightly shorter. On the other hand, the height and width of the thumb are larger and do not have the same blue background color that the Chrome version has.


2 Answers

It is possible in Firefox and IE -

// Mozilla    
input[type="range"]::-moz-range-progress {
    background: #cc1a1a;
    height: 12px;
    border-radius: 12px;
}
// IE
input[type="range"]::-ms-fill-lower {
    background: #CC1A1A;
    border: 0 solid #000101;
    border-radius: 50px;
    box-shadow: 0 0 0 #000000, 0 0 0 #0d0d0d;
}
input[type="range"]::-ms-fill-upper {
    background: #c0c0c0;
    border: 0 solid #000101;
    border-radius: 50px;
    box-shadow: 0 0 0 #000000, 0 0 0 #0d0d0d;
}

Only one way was for Chrome :before and :after, isn't supported any more (since March 2016). The best I found - is http://rangeslider.js.org/. Also it works fine with Angular JS - https://github.com/danielcrisp/angular-rangeslider

like image 132
Ihor Avatar answered Oct 10 '22 00:10

Ihor


Bit of a delayed reply here:

A pure CSS solution is not possible cross-browser here - IE is the only one that makes this possible via ::-ms-fill-lower and ::-ms-fill-upper

For a full cross-browser solution you'll need to use js. There's an example here that updates CSS gradients with the input's value to do roughly what you're after: http://codepen.io/ryanttb/pen/fHyEJ

You can also find information about this in this answer to a similar question: Style lower and upper fill in HTML5 range input

like image 24
lpslater Avatar answered Oct 09 '22 22:10

lpslater