Search code examples
htmlcssrangeslider

Range slider get disappear inconsistently?


I have range slider, when zoom-out the browser the range slider will get disappear inconsistently from the screen.

.slidecontainer {
  width: 100%;
}

.slider {
  -webkit-appearance: none;
  width: 100%;
  height: 1px;
  background: #d3d3d3;
  outline: none;
  opacity: 0.7;
  -webkit-transition: .2s;
  transition: opacity .2s;
}

.slider:hover {
  opacity: 1;
}

.slider::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 25px;
  height: 25px;
  background: #4CAF50;
  cursor: pointer;
}
<div class="slidecontainer">
  <p>Custom range slider:</p>
  <input type="range" min="1" max="100" value="50" class="slider" id="myRange">
</div>

Please suggest why its happening? Thanks!


Solution

  • The reason this is happening is due to subpixel rendering, for a good explanation of that have a look here: https://stackoverflow.com/a/14242055/1650337

    In your case you can work around this by using a border below your input, rather than a background colour. This leaves you with a 2 pixel high element (One transparent pixel, one grey) which is a lot more reliable when scaled:

    .slidecontainer {
      width: 100%;
    }
    
    .slider {
      -webkit-appearance: none;
      width: 100%;
      height: 1px;
      
      /* Remove the background colour */
      /* background: #d3d3d3; */ 
      
      /* Add a border at the bottom to act as your line */
      border-bottom: #d3d3d3 1px solid;
      
      outline: none;
      opacity: 0.7;
      -webkit-transition: .2s;
      transition: opacity .2s;
    }
    
    .slider:hover {
      opacity: 1;
    }
    
    .slider::-webkit-slider-thumb {
      -webkit-appearance: none;
      appearance: none;
      width: 25px;
      height: 25px;
      background: #4CAF50;
      cursor: pointer;
    }
    <div class="slidecontainer">
      <p>Custom range slider:</p>
      <input type="range" min="1" max="100" value="50" class="slider" id="myRange">
    </div>