Search code examples
htmlcssstyling

Why does custom CSS styling not apply due to vertical slider?


#slider1 {
  -webkit-appearance: slider-vertical; /* This makes ... */
  background-color: black;
  width: 1px;
  height: 100px;
}

#slider2 {
  -webkit-appearance: none; /* the difference. */
  background-color: black;
  width: 100px;
  height: 1px;
}

#slider1::-webkit-slider-thumb,
#slider2::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 25px;
  height: 25px;
  background: #04aa6d;
  cursor: pointer;
}

#slider1::-webkit-slider-thumb:hover,
#slider2::-webkit-slider-thumb:hover {
  width: 20px;
  height: 20px;
}
<input type="range" id="slider1" />
<input type="range" id="slider2" />

As seen here, the CSS styling was almost same for both sliders, except for the -webkit-appearance property. But the horizontal slider (which is the default slider) accepts the styling while the vertical slider rejects it. I am on Chrome. How to make it work? Thanks!


Solution

  • It seems like there is no work around available to design vertical range, what you can do is to rotate the default one.

    .wrap {
      display: flex;
    }
    
    .slid1 {
      display: flex;
      justify-content: center;
      align-items: center;
      height: 100px;
      width: 30px;
    }
    
    #slider1,
    #slider2 {
      -webkit-appearance: none;
      /* the difference. */
      background-color: black;
      width: 100px;
      height: 1px;
    }
    
    #slider1 {
      -webkit-appearance: none;
      /* This makes ... */
      transform: rotate(270deg);
      transform-origin: center;
    }
    
    #slider1::-webkit-slider-thumb,
    #slider2::-webkit-slider-thumb {
      -webkit-appearance: none;
      appearance: none;
      width: 25px;
      height: 25px;
      background: #04aa6d;
      cursor: pointer;
    }
    
    #slider1::-webkit-slider-thumb:hover,
    #slider2::-webkit-slider-thumb:hover {
      width: 20px;
      height: 20px;
    }
    <div class="wrap">
      <div class="slid1">
        <input type="range" id="slider1" />
      </div>
      <div class="slid2">
        <input type="range" id="slider2" />
      </div>
    </div>

    Source

    Edit: As per your comment, I have wrapped sliders into flex containers to align them.