search
HomeWeb Front-endCSS TutorialTeach you step by step how to use CSS to customize beautiful scroll bar styles!

Customized scroll bars are becoming more and more popular now and are worth studying. The following article will take you to understand the components of the scroll bar and introduce how to use CSS to customize the scroll bar style.

Teach you step by step how to use CSS to customize beautiful scroll bar styles!

(Learning video sharing: css video tutorial)

Why do you need to customize scrolling? The browser's default scroll bars make the UI look inconsistent across multiple operating systems. Using defined scrolling we can unify the style.

I've always been interested in how to customize scrollbars in CSS, but never had the chance to do so. Today, I will record my learning process.

Teach you step by step how to use CSS to customize beautiful scroll bar styles!

Introduction

First of all, we need to introduce the components of the scroll bar. The scroll bar contains track and thumb, as shown in the following figure:

Teach you step by step how to use CSS to customize beautiful scroll bar styles!

track is the scroll bar Basics, where thumb is the user dragging to support scrolling within the page or chapter.

Another important thing to remember is that scroll bars can work horizontally or vertically, depending on the design. Also, this changes when working on a multilingual website that works in both left-to-right (LTR) and right-to-left (RTL) directions.

Teach you step by step how to use CSS to customize beautiful scroll bar styles!

Custom scroll bar design

Having a custom scroll bar used to be a webkit patent, so Firefox and IE were Excluded from the game. We have a new syntax, only used in Firefox, that will make our jobs easier when it is fully supported. Let's look at the old Webkit syntax first, and then introduce the new syntax.

1. Old syntax

Scroll bar width

First, we need to define the size of the scroll bar. This can be the width of a vertical scrollbar or the height of a horizontal scrollbar.

.section::-webkit-scrollbar {
    width: 10px;
}

With this setting, we can set the style of the scroll bar itself.

Scroll bar track

This represents the basis of the scroll bar. We can style it by adding background, shadows, border-radius and border.

.section::-webkit-scrollbar-track {
    background-color: darkgrey;
}

Scroll bar thumb

After preparing the foundation of the scroll bar, we need to style the thumb of the scroll bar. This is important because the user may drag this thumb to interact with the scrollbar.

.section::-webkit-scrollbar-thumb {
    box-shadow: inset 0 0 6px rgba(0, 0, 0, 0.3);
}

So far, we have introduced the old method of customizing scroll bars in CSS. Let's explore the new syntax.

2. New syntax

Scrollbar Width

As it says, this definition The width of the scroll bar has two values: auto and thin. The bad thing is that we can't define a specific number like webkit's syntax.

.section {
  scrollbar-width: thin;
}

Scrollbar Color

With this property we can define pairs of values ​​for the scrollbar track and thumb s color.

.section {
    scrollbar-color: #6969dd #e0e0e0;
    scrollbar-width: thin;
}

Although this new syntax is simple, it has limitations. We can only add color. We can't add shadows`, gradients, rounded, or anything like that, all we are allowed to customize is the color.

Specify the range of custom scroll bars

An important question to know is where to customize the scroll bar. Do you want the style to be universal and valid for all scrollbars on the site? Or do you want it to be used only for a specific section?

Using the old syntax we can write selectors without having to attach them to elements and they will apply to all scrollable elements.

::-webkit-scrollbar {
    width: 10px;
}

::-webkit-scrollbar-track {
    background-color: darkgrey;
}

::-webkit-scrollbar-thumb {
    box-shadow: inset 0 0 6px rgba(0, 0, 0, 0.3);
}

If you want to apply only to a specific section, you need to append the element before the selector.

.section::-webkit-scrollbar {
    width: 10px;
}

.section::-webkit-scrollbar-track {
    background-color: darkgrey;
}

.section::-webkit-scrollbar-thumb {
    box-shadow: inset 0 0 6px rgba(0, 0, 0, 0.3);
}

With the new syntax, it's almost the same. What I noticed is that if you want a universal style, it should be applied to the element, not the .

html {
    scrollbar-color: #6969dd #e0e0e0;
    scrollbar-width: thin;
}

I tried adding the above for but it didn't work as expected.

Now that we know how the old and new syntax works, we can start customizing some scroll bar designs.

Custom scroll bar design

Example 1

Teach you step by step how to use CSS to customize beautiful scroll bar styles!

在研究定制滚动条之前,值得讨论一下Mac OS中的默认样式。下面是它的外观。

  • 滚动条track 的左右两边都有边框,背景色为纯色。
  • 滚动条thumb是圆形的,左右两边都有空间。

对于Windows,它有点不同。

Teach you step by step how to use CSS to customize beautiful scroll bar styles!

下面是我们根据上面的模拟图来定制滚动条。

.section::-webkit-scrollbar {
    width: 16px;
}
 
.section::-webkit-scrollbar-track {
    background-color: #e4e4e4;
    border-radius: 100px;
}
 
.section::-webkit-scrollbar-thumb {
    background-color: #d4aa70;
    border-radius: 100px;
}

trackthumb添加border-radius是必要的,因为它在::webkit-scrollbar上不起作用。

在新的语法中,我们不能调整滚动条的宽度,唯一能做的的是改变 trackthumb的背景颜色。

.section {
    scrollbar-color: #D4AA70 #e4e4e4;
}

例2

对于这个例子,设计有点重,因为它包含渐变和阴影。我们可以应用内部阴影和渐变来模仿这种效果。来看看怎么做!

Teach you step by step how to use CSS to customize beautiful scroll bar styles!

.section::-webkit-scrollbar-thumb {
    background-image: linear-gradient(180deg, #D0368A 0%, #708AD4 99%);
    box-shadow: inset 2px 2px 5px 0 rgba(#fff, 0.5);
    border-radius: 100px;
}

示例地址:https://codepen.io/shadeed/pen/VwpOReG

例3

我们还可以为 thumbtrack添加边框,这可以帮助我们处理一些棘手的设计。

Teach you step by step how to use CSS to customize beautiful scroll bar styles!

.section::-webkit-scrollbar-thumb {
    border-radius: 100px;
    background: #8070D4;
    border: 6px solid rgba(0,0,0,0.2);
}

基于同样的例子,我们可以重置顶部和底部边界为零,这样thumb获得一个有趣的效果。注意thumb顶部和底部的那些小元素。

Teach you step by step how to use CSS to customize beautiful scroll bar styles!

示例地址:https://codepen.io/shadeed/pen/qBrGvOx

可以添加悬停效果吗?

我们可以为新旧语法的滚动条thumb添加悬停效果。

/* 旧语法 */
.section::-webkit-scrollbar-thumb:hover {
    background-color: #5749d2;
}

/* 新语法 */
.section {
    scrollbar-color: #d4aa70 #e4e4e4;
    transition: scrollbar-color 0.3s ease-out;
}

.section:hover {
    scrollbar-color: #5749d2;
}

需要时显示滚动条

创建一个可滚动的元素是可以通过给overflow属性添加一个除visible以外的值。建议使用auto关键字,因为它只在内容超过其容器时才会显示滚动条。

Teach you step by step how to use CSS to customize beautiful scroll bar styles!

.section {
    overflow-y: auto;
}

可访问性问题

在定制滚动条设计时,请记住在 thumbtrack之间要有良好的对比,这样它就容易被用户注意。

考虑一下下面这个自定义滚动条的 "坏 "例子。

Teach you step by step how to use CSS to customize beautiful scroll bar styles!

thumb 的颜色几乎看不出来。这对用户来说不是好事,因为如果他们习惯于通过thumb 滚动,这将增加他们的难度。

英文原文地址:https://ishadeed.com/article/custom-scrollbars-css/

作者:ishadeed

更多编程相关知识,请访问:编程入门!!

The above is the detailed content of Teach you step by step how to use CSS to customize beautiful scroll bar styles!. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:segmentfault. If there is any infringement, please contact admin@php.cn delete
利用CSS怎么创建渐变色边框?5种方法分享利用CSS怎么创建渐变色边框?5种方法分享Oct 13, 2021 am 10:19 AM

利用CSS怎么创建渐变色边框?下面本篇文章给大家分享CSS实现渐变色边框的5种方法,希望对大家有所帮助!

css ul标签怎么去掉圆点css ul标签怎么去掉圆点Apr 25, 2022 pm 05:55 PM

在css中,可用list-style-type属性来去掉ul的圆点标记,语法为“ul{list-style-type:none}”;list-style-type属性可设置列表项标记的类型,当值为“none”可不定义标记,也可去除已有标记。

css与xml的区别是什么css与xml的区别是什么Apr 24, 2022 am 11:21 AM

区别是:css是层叠样式表单,是将样式信息与网页内容分离的一种标记语言,主要用来设计网页的样式,还可以对网页各元素进行格式化;xml是可扩展标记语言,是一种数据存储语言,用于使用简单的标记描述数据,将文档分成许多部件并对这些部件加以标识。

css3怎么实现鼠标隐藏效果css3怎么实现鼠标隐藏效果Apr 27, 2022 pm 05:20 PM

在css中,可以利用cursor属性实现鼠标隐藏效果,该属性用于定义鼠标指针放在一个元素边界范围内时所用的光标形状,当属性值设置为none时,就可以实现鼠标隐藏效果,语法为“元素{cursor:none}”。

rtl在css是什么意思rtl在css是什么意思Apr 24, 2022 am 11:07 AM

在css中,rtl是“right-to-left”的缩写,是从右往左的意思,指的是内联内容从右往左依次排布,是direction属性的一个属性值;该属性规定了文本的方向和书写方向,语法为“元素{direction:rtl}”。

css怎么实现英文小写转为大写css怎么实现英文小写转为大写Apr 25, 2022 pm 06:35 PM

转换方法:1、给英文元素添加“text-transform: uppercase;”样式,可将所有的英文字母都变成大写;2、给英文元素添加“text-transform:capitalize;”样式,可将英文文本中每个单词的首字母变为大写。

css怎么设置i不是斜体css怎么设置i不是斜体Apr 20, 2022 am 10:36 AM

在css中,可以利用“font-style”属性设置i元素不是斜体样式,该属性用于指定文本的字体样式,当属性值设置为“normal”时,会显示元素的标准字体样式,语法为“i元素{font-style:normal}”。

怎么设置rotate在css3的旋转中心点怎么设置rotate在css3的旋转中心点Apr 24, 2022 am 10:50 AM

在css3中,可以用“transform-origin”属性设置rotate的旋转中心点,该属性可更改转换元素的位置,第一个参数设置x轴的旋转位置,第二个参数设置y轴旋转位置,语法为“transform-origin:x轴位置 y轴位置”。

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft