Table of Contents
1. Basic HTML Structure
2. Style the Slider Track (Cross-Browser)
WebKit (Chrome, Safari, Edge)
Firefox
3. Style the Slider Thumb (Handle)
WebKit
4. Add Hover and Active States
5. Optional: Style the Active (Progress) Part of the Track
Example using background gradient:
Better approach: Use a wrapper with pseudo-elements
6. Full Minimal Example
Key Tips
Home Web Front-end CSS Tutorial How to create a custom, styleable range slider with CSS?

How to create a custom, styleable range slider with CSS?

Aug 05, 2025 pm 08:22 PM

To create a customizable range slider, you need to design the <input type="range"> cross-browser style through CSS pseudo-elements and browser-specific selectors; 1. Use standard HTML structure to define the minimum, maximum and default values of the slider; 2. Use ::-webkit-slider-runnable-track and ::-moz-range-track to style the slider track for WebKit and Firefox browsers respectively; 3. Use ::-webkit-slider-thumb and ::-moz-range-thumb to customize the slider handles, and remove the default appearance in WebKit; 4. Add:hover and:active states to improve the interactive experience; 5. Use JavaScript to dynamically update the background gradient to achieve visual feedback on the selected parts of the slider; 6. Integrate all styles and ensure cross-browser compatibility in the full example; end up with a modern, beautiful and functional custom slider, while maintaining accessibility and responsiveness.

How to create a custom, styleable range slider with CSS?

Creating a custom, styleable range slider with CSS involves styling the <input type="range"> element across different browsers, since each renders it differently by default. With a mix of CSS pseudo-elements and browser-specific selectors, you can fully customize its appearance—track, thumb, and active states—while keeping it accessible and functional.

How to create a custom, styleable range slider with CSS?

Here's how to build a clean, modern, and fully styleable range slider using HTML and CSS.


1. Basic HTML Structure

Start with a simple range input:

How to create a custom, styleable range slider with CSS?
 <input type="range" class="slider" min="0" max="100" value="50">

This creates a default slider from 0 to 100, starting at 50.


2. Style the Slider Track (Cross-Browser)

The track is styled differently in WebKit (Chrome, Safari, Edge) and Firefox. You need to target both.

How to create a custom, styleable range slider with CSS?

WebKit (Chrome, Safari, Edge)

Use ::-webkit-slider-runnable-track :

 .slider::-webkit-slider-runnable-track {
  width: 100%;
  height: 8px;
  background: #ddd;
  border-radius: 4px;
  cursor: pointer;
}

Firefox

Use ::-moz-range-track :

 .slider::-moz-range-track {
  width: 100%;
  height: 8px;
  background: #ddd;
  border-radius: 4px;
  cursor: pointer;
}

Note: Firefox uses ::-moz-range-track , while WebKit uses ::-webkit-slider-runnable-track .


3. Style the Slider Thumb (Handle)

The draggable handle also requires browser-specific prefixes.

WebKit

 .slider::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 20px;
  height: 20px;
  background: #4CAF50;
  border-radius: 50%;
  cursor: pointer;
  margin-top: -6px; /* Align vertically with track */
}

-webkit-appearance: none removes default styling so you can apply custom styles.

Firefox

 .slider::-moz-range-thumb {
  width: 20px;
  height: 20px;
  background: #4CAF50;
  border: none;
  border-radius: 50%;
  cursor: pointer;
}

Firefox's ::-moz-range-thumb is simpler—no need for appearance: none .


4. Add Hover and Active States

Make the slider feel interactive:

 .slider:hover::-webkit-slider-thumb {
  background: #45a049;
}

.slider:hover::-moz-range-thumb {
  background: #45a049;
}

.slider:active::-webkit-slider-thumb {
  transform: scale(1.15);
}

.slider:active::-moz-range-thumb {
  transform: scale(1.15);
}

You can also style the track differently when active (eg, change color as the user drags).


5. Optional: Style the Active (Progress) Part of the Track

To show a "filled" portion of the track (left side up to the thumb), you need extra markup or pseudo-elements. Since CSS can't directly style the "before" part of the track in standard inputs, one workaround is using a wrapper and background gradients.

Example using background gradient:

 .slider {
  -webkit-appearance: none;
  width: 100%;
  height: 8px;
  background: linear-gradient(
    to right,
    #4CAF50 0%,
    #4CAF50 50%,
    #ddd 50%,
    #ddd 100%
  );
  outline: none;
  border-radius: 4px;
  opacity: 0.9;
  transition: opacity .2s;
}

But this requires JavaScript to update the gradient based on value.

Better approach: Use a wrapper with pseudo-elements

 
<input type="range" class="slider" min="0" max="100" value="50">

Then use CSS to layer the track and fill:

 .slider-wrapper {
  position: relative;
  width: 100%;
}

.slider {
  -webkit-appearance: none;
  width: 100%;
  height: 8px;
  background: transparent;
  outline: none;
}

.slider::-webkit-slider-runnable-track {
  background: #ddd;
  height: 8px;
  border-radius: 4px;
}

/* Custom fill using pseudo-element (via JS or advanced CSS) */

For dynamic fill, JavaScript is usually required:

 const slider = document.querySelector(&#39;.slider&#39;);
slider.addEventListener(&#39;input&#39;, () => {
  const percent = (slider.value / (slider.max - slider.min)) * 100;
  slider.style.background = `linear-gradient(to right, #4CAF50 ${percent}%, #ddd ${percent}%)`;
});

Then add to CSS:

 .slider {
  background: linear-gradient(to right, #4CAF50 50%, #ddd 50%);
  background-size: 100% 8px;
  background-repeat: no-repeat;
  border-radius: 4px;
}

Now the track shows colored progress dynamically.


6. Full Minimal Example

 <!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <title>Custom Range Slider</title>
  <style>
    .slider {
      -webkit-appearance: none;
      width: 100%;
      height: 8px;
      background: #ddd;
      border-radius: 4px;
      outline: none;
      opacity: 0.9;
      transition: opacity .2s;
    }

    .slider:hover {
      opacity: 1;
    }

    .slider::-webkit-slider-runnable-track {
      background: #ddd;
      height: 8px;
      border-radius: 4px;
    }

    .slider::-webkit-slider-thumb {
      -webkit-appearance: none;
      appearance: none;
      width: 20px;
      height: 20px;
      background: #4CAF50;
      border-radius: 50%;
      cursor: pointer;
      margin-top: -6px;
    }

    .slider::-moz-range-track {
      width: 100%;
      height: 8px;
      background: #ddd;
      border-radius: 4px;
      cursor: pointer;
    }

    .slider::-moz-range-thumb {
      width: 20px;
      height: 20px;
      background: #4CAF50;
      border: none;
      border-radius: 50%;
      cursor: pointer;
    }

    .slider:hover::-webkit-slider-thumb,
    .slider:hover::-moz-range-thumb {
      background: #45a049;
    }

    .slider:active::-webkit-slider-thumb {
      transform: scale(1.15);
    }

    .slider:active::-moz-range-thumb {
      transform: scale(1.15);
    }
  </style>
</head>
<body>
  <input type="range" class="slider" min="0" max="100" value="50" />
</body>
</html>

Key Tips

  • Always test in multiple browsers (Chrome, Firefox, Safari, Edge).
  • Use appearance: none only on the thumb in WebKit.
  • For more advanced visuals (like dual thumbs or tooltips), consider libraries or custom React/Vue components.
  • Accessibility is preserved as long as you don't remove keyboard support or ARIA roles.

That's it. You now have a fully custom, styleable range slider with clean, modern styling and cross-browser support. Basically just override the defaults and get creative with colors, shadows, and transitions.

The above is the detailed content of How to create a custom, styleable range slider with CSS?. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1511
276
What are common CSS browser inconsistencies? What are common CSS browser inconsistencies? Jul 26, 2025 am 07:04 AM

Different browsers have differences in CSS parsing, resulting in inconsistent display effects, mainly including the default style difference, box model calculation method, Flexbox and Grid layout support level, and inconsistent behavior of certain CSS attributes. 1. The default style processing is inconsistent. The solution is to use CSSReset or Normalize.css to unify the initial style; 2. The box model calculation method of the old version of IE is different. It is recommended to use box-sizing:border-box in a unified manner; 3. Flexbox and Grid perform differently in edge cases or in old versions. More tests and use Autoprefixer; 4. Some CSS attribute behaviors are inconsistent. CanIuse must be consulted and downgraded.

What is the accent-color property? What is the accent-color property? Jul 26, 2025 am 09:25 AM

accent-color is an attribute used in CSS to customize the highlight colors of form elements such as checkboxes, radio buttons and sliders; 1. It directly changes the default color of the selected state of the form control, such as changing the blue check mark of the checkbox to red; 2. Supported elements include input boxes of type="checkbox", type="radio" and type="range"; 3. Using accent-color can avoid complex custom styles and extra DOM structures, and maintain native accessibility; 4. It is generally supported by modern browsers, and old browsers need to be downgraded; 5. Set accent-col

How does browser default stylesheet affect rendering? How does browser default stylesheet affect rendering? Jul 19, 2025 am 02:08 AM

Browser default styles ensure basic readability by automatically applying margins, fills, fonts, and form element styles, but can cause inconsistent cross-browser layouts. 1. The default margin and fill change the layout flow, such as the spacing of titles, paragraphs and lists; 2. The default font settings affect readability, such as 16px font size and TimesNewRoman font; 3. The form elements are very different in different browsers, so the appearance needs to be reset; 4. Some tags such as strong and em have default emphasis styles and need to be explicitly overwritten. Workarounds include using Normalize.css, reset styles, or globally clear margins and fills, while customizing fonts and form styles for consistency.

How to style the first letter or first line of a paragraph? How to style the first letter or first line of a paragraph? Jul 19, 2025 am 02:58 AM

To beautify the beginning of a paragraph to enhance visual appeal, a common practice is to use pseudo-elements of CSS or manually style the document. In web development, p::first-letter can be used to set the first letter style, such as enlarging, bolding, and discoloring, but it should be noted that it is only suitable for block-level elements; if you want to highlight the entire first line, use p::first-line to add styles; in document software such as Word, you can manually adjust the first letter format or create style templates, and InDesign has a built-in "first-sinking" function suitable for publishing and design; when applying, you need to pay attention to details, such as avoiding complex styles affecting reading and ensuring compatibility and format consistency.

Describe the `vertical-align` property and its typical use cases Describe the `vertical-align` property and its typical use cases Jul 26, 2025 am 07:35 AM

Thevertical-alignpropertyinCSSalignsinlineortable-cellelementsvertically.1.Itadjustselementslikeimagesorforminputswithintextlinesusingvalueslikebaseline,middle,super,andsub.2.Intablecells,itcontrolscontentalignmentwithtop,middle,orbottomvalues,oftenu

Describe the CSS `counter-reset` and `counter-increment` properties Describe the CSS `counter-reset` and `counter-increment` properties Jul 18, 2025 am 04:00 AM

CSS' counter-reset and counter-increment are used to automatically number HTML elements. 1. Use counter-reset to initialize or reset the counter, for example, section{counter-reset:sub-section;} to create a counter named sub-section; 2. Increment the counter through counter-increment, such as h3{counter-increment:sub-section;} to increment each h3 title number; 3. Use content attribute to combine pseudo-elements to display the counter, such as h3::before{content:

How to purge unused CSS? How to purge unused CSS? Jul 27, 2025 am 02:47 AM

UseautomatedtoolslikePurgeCSSorUnCSStoscanandremoveunusedCSS;2.IntegratepurgingintoyourbuildprocessviaWebpack,Vite,orTailwind’scontentconfiguration;3.AuditCSSusagewithChromeDevToolsCoveragetabbeforepurgingtoavoidremovingneededstyles;4.Safelistdynamic

How to change text color in CSS? How to change text color in CSS? Jul 27, 2025 am 04:25 AM

To change the text color in CSS, you need to use the color attribute; 1. Use the color attribute to set the text foreground color, supporting color names (such as red), hexadecimal codes (such as #ff0000), RGB values (such as rgb(255,0,0)), HSL values (such as hsl(0,100%,50%)), and RGBA or HSLA with transparency (such as rgba(255,0,0,0.5)); 2. You can apply colors to any element containing text, such as h1 to h6 titles, paragraph p, link a (note the color settings of different states of a:link, a:visited, a:hover, a:active), buttons, div, span, etc.; 3. Most

See all articles