Home > Web Front-end > CSS Tutorial > How Can I Style HTML5 Range Inputs with Different Colors on Each Side Using CSS?

How Can I Style HTML5 Range Inputs with Different Colors on Each Side Using CSS?

Barbara Streisand
Release: 2024-12-19 20:05:20
Original
454 people have browsed it

How Can I Style HTML5 Range Inputs with Different Colors on Each Side Using CSS?

Styling HTML5 Range Input with Different Colors on Either Side

Styling HTML5 range inputs to have contrasting colors on the left and right sides is a common request. As the user interacts with the slider, the color changes dynamically, giving visual feedback on the input value. Achieving this effect in pure CSS may seem like a challenge, but it's possible.

For Chrome, the trick lies in hiding the overflow from the input and using a box shadow to fill the remaining space with the desired color. This technique effectively paints the left side of the slider with a custom color.

For IE and Firefox, we can leverage built-in CSS pseudo-elements:

  • In IE: ::-ms-fill-lower allows you to style the lower portion of the slider, enabling the green color.
  • In Firefox: ::-moz-range-progress gives you similar control, allowing you to define the color for the filled portion of the slider.

To illustrate the CSS solution, refer to the following code:

@media screen and (-webkit-min-device-pixel-ratio:0) {
  input[type='range'] {
    overflow: hidden;
    width: 80px;
    -webkit-appearance: none;
    background-color: #9a905d;
  }
  
  input[type='range']::-webkit-slider-runnable-track {
    height: 10px;
    -webkit-appearance: none;
    color: #13bba4;
    margin-top: -1px;
  }
  
  input[type='range']::-webkit-slider-thumb {
    width: 10px;
    -webkit-appearance: none;
    height: 10px;
    cursor: ew-resize;
    background: #434343;
    box-shadow: -80px 0 0 80px #43e5f7;
  }
}

input[type="range"]::-moz-range-progress {
  background-color: #43e5f7; 
}
input[type="range"]::-moz-range-track {  
  background-color: #9a905d;
}

input[type="range"]::-ms-fill-lower {
  background-color: #43e5f7; 
}
input[type="range"]::-ms-fill-upper {  
  background-color: #9a905d;
}
Copy after login

Using this CSS code, you can style your HTML5 range input to display different colors on either side of the slider, creating a visually appealing and user-friendly interface for your web application.

The above is the detailed content of How Can I Style HTML5 Range Inputs with Different Colors on Each Side Using CSS?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template