search
HomeWeb Front-endCSS TutorialHow to Create a Custom Range Slider Using CSS

Pure CSS creates cool custom range sliders: no JavaScript required, both accessibility

This article will demonstrate how to create custom scope sliders using only CSS and native HTML <input type="range"> elements without relying on JavaScript while ensuring accessibility. Tutorials cover ways to customize input elements, including resetting and disabling browser default styles, setting slider styles, and creating sliding gradient effects using border-image. In addition, it will be explained how to add subtle animations to enhance user interaction, such as converting the slider from a border-only circle to a full circle when clicked, and darkening the color when hovered. This technology retains native features and supports keyboard navigation, providing a versatile and easy-to-access solution for custom range sliders.

How to Create a Custom Range Slider Using CSS

Key points:

  • Create a custom scope slider using pure CSS and native HTML <input type="range"> elements.
  • The
  • tutorial contains steps to customize input elements: reset and disable browser default styles, set slider styles, and create sliding gradient effects using border-image.
  • Add subtle animations to enhance the user experience, such as changing the slider shape when clicked and changing the color when hovered.
  • Keep native functionality and support keyboard navigation to ensure accessibility.

The default range slider style is not beautiful. The following figure shows the display effect of the default range slider in Chrome, Firefox and Safari browsers:

How to Create a Custom Range Slider Using CSS

However, <input type="range"> elements are difficult to style. Most online solutions rely on JavaScript and verbose code. Worse, some technologies can also destroy the accessibility of elements. So let's see how to better utilize pure CSS to achieve without affecting accessibility. The following CodePen demonstration shows what we are going to build: CodePen link

Structure of range input element

Let's analyze the structure of the range input element first. It is a native element and each browser has its own implementation. There are two main implementations: one for Webkit and Blink browsers (such as Chrome, Edge, Safari and Opera):

<input type="range" min="0" max="100" step="1" value="20">

How to Create a Custom Range Slider Using CSS

Another one for Firefox:

<input type="range" min="0" max="100" step="1" value="20">

How to Create a Custom Range Slider Using CSS

IE also has a third implementation, but luckily, this browser is almost gone now! This inconsistency between browsers makes tasks difficult because we need to provide different styles for each implementation. I won't go into this in depth as this post will never be finished, but I highly recommend reading this post by Ana Tudor for a more in-depth exploration (post links should be inserted here). You just need to remember that no matter the implementation, the "slider" (thumb) is always a common component.

How to Create a Custom Range Slider Using CSS

I will only style this element, which will make my custom scope slider easy to customize. Let's jump straight into the code and see what's amazing.

Custom input element

The first step is to use appearance: none and some other common properties to reset and disable all browser default styles:

<input type="range" min="0" max="100" step="1" value="20">

In more complex cases, if other default styles are applied to our elements, we may need to add more code. Just make sure we have a "naked" element without any visual style. Let's also define some CSS variables so that we can easily create different variants for the range slider:

<input type="range" min="0" max="100" step="1" value="20">

In this step, only the slider and its default style are visible.

Set the slider element style

Let's style the slider element. We will start with the basic settings:

input {
  appearance: none;
  background: none;
  cursor: pointer;
}

Use border-imageAdd some magical effects

Now we will use a magic CSS trick to complete our slider. It involves the use of border-image:

input {
  --c: orange; /* 活动颜色 */
  --g: 8px; /* 间隙 */
  --l: 5px; /* 线粗细 */
  --s: 30px; /* 滑块大小 */

  width: 400px; /* 输入宽度 */
  height: var(--s);
  appearance: none;
  background: none;
  cursor: pointer;
}

The hallucination is perfect by adding overflow: hidden to the input element and using larger values.

Add some animations

Can we add some subtle animations when interacting with the slider? It doesn't require a lot of code and will enhance the UX of the slider. First, we convert the slider from a border-only circle to a full circle when clicked. To do this, we add the box-shadow value of spread.

Conclusion

We've done it and don't have to deal with any complex browser-related implementations! We identified the selector for the slider element and used some CSS tricks to style the entire range of sliders. Don't forget, we only use the <input type="range"> element, so we don't have to worry about any accessibility issues as we keep the native features. The slider supports keyboard navigation without any problem. Here are more examples of sliders created with the same technique: CodePen link

CodePen link

The above is the detailed content of How to Create a Custom Range Slider Using CSS. For more information, please follow other related articles on the PHP Chinese website!

Statement
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
Simulating Mouse MovementSimulating Mouse MovementApr 22, 2025 am 11:45 AM

If you've ever had to display an interactive animation during a live talk or a class, then you may know that it's not always easy to interact with your slides

Powering Search With Astro Actions and Fuse.jsPowering Search With Astro Actions and Fuse.jsApr 22, 2025 am 11:41 AM

With Astro, we can generate most of our site during our build, but have a small bit of server-side code that can handle search functionality using something like Fuse.js. In this demo, we’ll use Fuse to search through a set of personal “bookmarks” th

Undefined: The Third Boolean ValueUndefined: The Third Boolean ValueApr 22, 2025 am 11:38 AM

I wanted to implement a notification message in one of my projects, similar to what you’d see in Google Docs while a document is saving. In other words, a

In Defense of the Ternary StatementIn Defense of the Ternary StatementApr 22, 2025 am 11:25 AM

Some months ago I was on Hacker News (as one does) and I ran across a (now deleted) article about not using if statements. If you’re new to this idea (like I

Using the Web Speech API for Multilingual TranslationsUsing the Web Speech API for Multilingual TranslationsApr 22, 2025 am 11:23 AM

Since the early days of science fiction, we have fantasized about machines that talk to us. Today it is commonplace. Even so, the technology for making

Jetpack Gutenberg BlocksJetpack Gutenberg BlocksApr 22, 2025 am 11:20 AM

I remember when Gutenberg was released into core, because I was at WordCamp US that day. A number of months have gone by now, so I imagine more and more of us

Creating a Reusable Pagination Component in VueCreating a Reusable Pagination Component in VueApr 22, 2025 am 11:17 AM

The idea behind most of web applications is to fetch data from the database and present it to the user in the best possible way. When we deal with data there

Using 'box shadows' and clip-path togetherUsing 'box shadows' and clip-path togetherApr 22, 2025 am 11:13 AM

Let's do a little step-by-step of a situation where you can't quite do what seems to make sense, but you can still get it done with CSS trickery. In this

See all articles

Hot AI Tools

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.

Undress AI Tool

Undress AI Tool

Undress images for free

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

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools