Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="wrapper">
  <header class="header"><h1>Custom Range Input</h1></header>
  <article class="content">
  <div class="rangepresenter">
    <h2>Wide</h2>
<div class="rangewrapper horizontal">
  <div class="sliderfill">
  <input class="customrange" type="range" min="0" max="100" value="50">
  </div>
  <div class="sliderthumb"></div>
  <div class="slidervalue">50</div>
</div>
    <div class="rangewrapper horizontal">
  <div class="sliderfill">
  <input class="customrange" type="range" min="0" max="100" value="50">
  </div>
  <div class="sliderthumb"></div>
  <div class="slidervalue">50</div>
</div>
 <div class="rangewrapper horizontal">
  <div class="sliderfill">
  <input class="customrange" type="range" min="0" max="100" value="50">
  </div>
  <div class="sliderthumb"></div>
  <div class="slidervalue">50</div>
</div>
    </div>
<div class="rangepresenter verticalsliders">
    <h2>Tall</h2>
 <div class="rangewrapper vertical">
   <div class="sliderfill">
  <input class="customrange" type="range" min="0" max="100" value="50">
   </div>
   <div class="sliderthumb"></div>
   <div class="slidervalue">50</div>
</div>
  </div>
    
</article>
  <footer class="footer"><p>This is an update to an <a href="https://codepen.io/sgestrella/pen/pjyzRY" target="_top">old pen I did in 2015.</a><br>The original still works but is much more complex than is necessary today.</p>
    <p>
      This was created for an update to one of <a href="https://css-tricks.com/author/steven-estrella/" target="_blank">my CSS-Tricks articles</a></p>
    <p>Please also enjoy my <a href="https://codepen.io/sgestrella/pen/YMjeRo" target="_blank">CSS-only solution</a> to accessible range inputs.</p>
  </footer>
</div>


              
            
!

CSS

              
                @charset "UTF-8";
/* Modified Meyer Reset based on http://meyerweb.com/eric/tools/css/reset/
   v2.0 | 20110126   License: none (public domain)*/
html, body, div, span, h1, h2, h3, h4, h5, h6, p, 
a, b, em, img, input {
    margin: 0;padding: 0;border: 0;
    font: inherit; vertical-align: baseline;
}
body {line-height: 1.2;}
*, *:before, *:after {box-sizing: border-box;}
/* End Modified Meyer Reset*/

html {font-size:16px;}
:root {
    --headerfont: sans-serif;
    --mainfont: serif;
    --maincolor: #333;
    --accentcolor: rgb(0,128,128);
    --accentcoloralpha: rgba(0,128,128,0.5);
    --maxwidth: 800px;
    --lineheight: 1.3;
    --thumbsize: 40px;
    --tracksize: 300px;
    --trackheight: 28px;
    --trackradius: 6px;
    --innertrackradius: 4px;
}

body {background:var(--accentcolor);
-webkit-tap-highlight-color:rgba(0,0,0,0);-webkit-user-select: none;
}
/*Grid layout - also see media queries below*/
.wrapper {
  display:grid;
  grid-template-columns: 1fr;
  justify-items: center;
}
.header, .footer {
  grid-column: 1 / -1;
}
.content {
  display:grid;
  grid-template-columns: 1fr;
  justify-items: center;
}
@media screen and (min-width: 768px) {
  html {font-size:18px;}
	.content {grid-template-columns: 2.5fr 1fr;}
}
/* #### Small Mobile Phones Portrait #### */
@media screen and (max-device-width: 480px) and (orientation: portrait){
  :root {--tracksize:240px;}
	.content {grid-template-columns: 1fr;}
  .customrange {opacity:0.2;}
  /*Range inputs on some mobile devices don't work well if the opacity  is set too low.*/
}

/* cosmetic styles */
.wrapper {
  margin:0 auto;
  padding:20px;
  background:white;
  max-width:var(--maxwidth);
  height:auto;
  text-align:center;
}

h1, h2 {
  font-weight:bold;
  font-family:var(--headerfont);
  color:var(--accentcolor);
  pointer-events:none;
}
h1 {
  font-size:2rem; 
  margin:0 0 30px 0;
}
h2 {
  font-size:1.5rem; 
  margin:0 0 10px 0;
  padding:0;
  height:30px;
  width:100%;
  text-align:center;
}
.content {
  margin:10px 0 20px 0;
}
.rangepresenter {
  width:var(--tracksize);
  height:auto;
}
.rangepresenter.verticalsliders {
    max-width:calc(var(--tracksize)/2);
    min-height:calc(var(--tracksize) + 60px);
    position:relative;
 }
.rangewrapper {
  line-height:var(--lineheight);
  border:2px solid var(--maincolor);
  border-radius:var(--trackradius);
  margin:20px 0 40px 0;
  padding:0;
  position:relative;
  width:var(--tracksize);
  height:var(--trackheight);
  overflow:visible;
  background-color:#ffc;
}
.rangewrapper.vertical{
  transform-origin: 50% 50%;
  transform: rotate(-90deg);
  position:absolute;
  top:calc( (var(--tracksize)/2) + 30px);
  left:-50%;
  margin:0;
}
.sliderfill {
  border:0 none;
  border-radius:var(--innertrackradius);
  margin:0;
  padding:0;
  height:100%;
}
.sliderthumb {
  width:var(--thumbsize);
  height:var(--thumbsize);
	background-image:url('https://s3-us-west-2.amazonaws.com/s.cdpn.io/358203/thumb.png');
  background-size: 100% 100%;
	background-repeat: no-repeat;
  background-color:transparent;
	position:absolute;
  left:0;
  top:calc(((var(--thumbsize) - var(--trackheight))/-2) - 2px);
  border:0 none;
  padding:0;
	pointer-events:none;
}
.slidervalue {
  width:var(--thumbsize);
  height:var(--thumbsize);
  line-height:var(--thumbsize);
	position:absolute;
  left:calc(50% - (var(--thumbsize)/2));
  top:calc(((var(--thumbsize) - var(--trackheight))/-2) - 2px);
  color:white;
  font-family:var(--mainfont);
  font-size:1.1rem;
  font-weight:normal;
  border:0 none;
	pointer-events:none;
}
.vertical .slidervalue {
  transform:rotate(90deg);
}
.customrange {
  cursor:pointer;
  height:100%;
  width:var(--tracksize);
  opacity:0.05;
}

a:link, a:visited {
  color:var(--accentcolor);
}
a:hover {color:#090;}
              
            
!

JS

              
                let sliders, sliderfills, thumbs, slidervalues;
let initialValue = [38,50,63,88]; //initial values for the sliders

document.addEventListener('DOMContentLoaded', function (e) { init();});

function init(){
  sliders = document.querySelectorAll(".customrange");
  sliderfills = document.querySelectorAll(".sliderfill");
  thumbs = document.querySelectorAll(".sliderthumb");
  slidervalues = document.querySelectorAll(".slidervalue");
  /* We need to change slider appearance to respond to both input and change events. */
  for (let i=0;i<sliders.length;i++){
    sliders[i].addEventListener("input",function(e){updateSlider(i,sliders[i].value);});
    sliders[i].addEventListener("change",function(e){updateSlider(i,sliders[i].value);});
    //set initial values for the sliders
    sliders[i].value = initialValue[i];
    //update each slider
    updateSlider(i,sliders[i].value);
  }
}
function updateSlider(fillindex,val){
  //sets the text display and location for each thumb and the slider fill  
  let min = Number(sliders[fillindex].getAttribute("min"));
  let max = Number(sliders[fillindex].getAttribute("max"));
  let pc = (val/(max-min)) * 100
  setThumbText(slidervalues[fillindex],val,pc);
  setThumb(thumbs[fillindex],pc);
  setSliderFill(sliderfills[fillindex],pc);
}
function setThumbText(elem,val,pc){
  let size = getComputedStyle(elem).getPropertyValue("--thumbsize");
  let newx = `calc(${pc}% - ${parseInt(size)/2}px)`;
  elem.style.left = newx;
  elem.innerHTML = val;
}
function setThumb(elem,val){
  let size = getComputedStyle(elem).getPropertyValue("--thumbsize");
  let newx = `calc(${val}% - ${parseInt(size)/2}px)`;
  elem.style.left = newx;
  let max = 100;
  let degrees = 360 * (val/max);
  let rotation = `rotate(${degrees}deg)`;
	elem.style.transform = rotation;
}
function setSliderFill(elem,val){
  let fillcolor = getComputedStyle(elem).getPropertyValue("--accentcolor");
  let alphafillcolor = getComputedStyle(elem).getPropertyValue("--accentcoloralpha");
  // we create a linear gradient with a color stop based on the slider value
  let gradient = `linear-gradient(to right, ${fillcolor} 0%, 
${alphafillcolor} ${val}%, 
rgba(255,255,255,0.1) ${Number(val) + 1}%, 
rgba(255,255,255,0)  100%)`;
  elem.style.backgroundImage = gradient;
}
              
            
!
999px

Console