Home Web Front-end CSS Tutorial How to Create a Custom Range Slider Using CSS

How to Create a Custom Range Slider Using CSS

Feb 09, 2025 pm 12:08 PM

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">
Copy after login
Copy after login
Copy after login
Copy after login

How to Create a Custom Range Slider Using CSS

Another one for Firefox:

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

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">
Copy after login
Copy after login
Copy after login
Copy after login

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">
Copy after login
Copy after login
Copy after login
Copy after login

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;
}
Copy after login

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;
}
Copy after login

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 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

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

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)

Vue 3 Vue 3 Apr 02, 2025 pm 06:32 PM

It&#039;s out! Congrats to the Vue team for getting it done, I know it was a massive effort and a long time coming. All new docs, as well.

Can you get valid CSS property values from the browser? Can you get valid CSS property values from the browser? Apr 02, 2025 pm 06:17 PM

I had someone write in with this very legit question. Lea just blogged about how you can get valid CSS properties themselves from the browser. That&#039;s like this.

A bit on ci/cd A bit on ci/cd Apr 02, 2025 pm 06:21 PM

I&#039;d say "website" fits better than "mobile app" but I like this framing from Max Lynch:

Stacked Cards with Sticky Positioning and a Dash of Sass Stacked Cards with Sticky Positioning and a Dash of Sass Apr 03, 2025 am 10:30 AM

The other day, I spotted this particularly lovely bit from Corey Ginnivan’s website where a collection of cards stack on top of one another as you scroll.

Using Markdown and Localization in the WordPress Block Editor Using Markdown and Localization in the WordPress Block Editor Apr 02, 2025 am 04:27 AM

If we need to show documentation to the user directly in the WordPress editor, what is the best way to do it?

Comparing Browsers for Responsive Design Comparing Browsers for Responsive Design Apr 02, 2025 pm 06:25 PM

There are a number of these desktop apps where the goal is showing your site at different dimensions all at the same time. So you can, for example, be writing

Why are the purple slashed areas in the Flex layout mistakenly considered 'overflow space'? Why are the purple slashed areas in the Flex layout mistakenly considered 'overflow space'? Apr 05, 2025 pm 05:51 PM

Questions about purple slash areas in Flex layouts When using Flex layouts, you may encounter some confusing phenomena, such as in the developer tools (d...

How to Use CSS Grid for Sticky Headers and Footers How to Use CSS Grid for Sticky Headers and Footers Apr 02, 2025 pm 06:29 PM

CSS Grid is a collection of properties designed to make layout easier than it’s ever been. Like anything, there&#039;s a bit of a learning curve, but Grid is

See all articles