How to Create a Custom Scrollbar using CSS?
Last Updated :
10 Oct, 2024
Scrollbars are common graphical user interface elements that allow users to navigate content beyond the visible area. By default, web browsers provide basic scrollbars with a predefined style. However, using HTML and CSS, you can customize the scrollbar to match your website's design better.
Scrollbars consist of various elements that can be styled individually, using specific CSS pseudo-elements for WebKit-based browsers (like Chrome, Safari, and Opera) and Firefox. The key pseudo-elements for customizing scrollbars include:
- ::-webkit-scrollbar: The entire scrollbar.
- ::-webkit-scrollbar-track: The track (background) of the scrollbar.
- ::-webkit-scrollbar-thumb: The draggable part of the scrollbar.
- ::-webkit-scrollbar-button: Optional buttons on the scrollbar (not commonly used).
- ::-moz-scrollbar, ::-moz-scrollbar-track, ::-moz-scrollbar-thumb: For customizing Firefox scrollbars.
Note: Custom scrollbars are not fully supported in all browsers, and the level of customization can vary. Providing fallback styles or default behaviors for unsupported browsers is recommended.
Approach to Create a Custom Scrollbar
To create a custom scrollbar using HTML and CSS, you can follow these general steps:
- Create a container element that will hold the content and the scrollbar. This can be a div or any other HTML element.
- Set the height of the container element to a fixed value, and add overflow-y: scroll to enable the scrollbar. This will allow the content to be scrolled vertically.
- Customize the scrollbar's width, color, background, border, and other properties using CSS.
- Add any additional styling, such as hover and active states for the scrollbar elements, to enhance the user experience.
- Finally, test the custom scrollbar in different web browsers to ensure that it works as intended and that it is visually appealing.
Example: This example shows the use of the above-explained approach.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content=
"width=device-width, initial-scale=1.0" />
<title>
How to create a custom scrollbar
using HTML and CSS?
</title>
<style>
.scroll-container {
height: 300px;
overflow-y: scroll;
}
.scroll-container::-webkit-scrollbar {
width: 12px;
}
.scroll-container::-webkit-scrollbar-track {
background: #f1f1f1;
}
.scroll-container::-webkit-scrollbar-thumb {
background: #888;
}
.scroll-container::-webkit-scrollbar-thumb:hover {
background: #555;
}
.scroll-container::-webkit-scrollbar-button {
display: none;
}
</style>
</head>
<body>
<div class="scroll-container">
<!-- Content goes here -->
<strong>What is Data Structure?</strong>
<p>
A data structure is defined as a particular
way of storing and organizing data in our
devices to use the data efficiently and
effectively. The main idea behind using
data structures is to minimize the time
and space complexities. An efficient data
structure takes minimum memory space and
requires minimum time to execute the data.
</p>
<strong>What is Algorithm?</strong>
<p>
Algorithm is defined as a process or set
of well-defined instructions that are
typically used to solve a particular group
of problems or perform a specific type of
calculation. To explain in simpler terms,
it is a set of operations performed in a
step-by-step manner to execute a task.
</p>
<strong>How to start learning DSA?</strong>
<p>
The first and foremost thing is dividing
the total procedure into little pieces
which need to be done sequentially. The
complete process to learn DSA from
scratch can be broken into 4 parts:
</p>
<li>Learn about Time and Space complexities</li>
<li>
Learn the basics of individual
Data Structures
</li>
<li>Learn the basics of Algorithms</li>
<li>Practice Problems on DSA</li>
<strong> 1. Learn about Complexities</strong>
<p>
Here comes one of the interesting and
important topics. The primary motive to
use DSA is to solve a problem effectively
and efficiently. How can you decide if a
program written by you is efficient or
not? This is measured by complexities.
Complexity is of two types:
</p>
<li>
<strong>Time Complexity:</strong> Time
complexity is used to measure the amount
of time required to execute the code.
</li>
<li>
<strong>Space Complexity:</strong>
Space complexity means the amount of
space required to execute successfully
the functionalities of the code. You
will also come across the term Auxiliary
Space very commonly in DSA, which refers
to the extra space used in the program
other than the input data structure.
</li>
<p>
Both of the above complexities are
measured with respect to the input
parameters. But here arises a problem.
The time required for executing a code
depends on several factors, such as:
</p>
<li>
The number of operations performed
in the program
</li>
<li>The speed of the device, and also</li>
<li>
The speed of data transfer if being
executed on an online platform.
</li>
</div>
</body>
</html>
Output:
Supported Browser
Custom scrollbars are supported in several popular web browsers, but their level of support may vary depending on the browser.
- Google Chrome
- Safari
- Opera
- Mozilla Firefox
Similar Reads
JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav
11 min read
Web Development Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De
5 min read
React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications
15+ min read
React Tutorial React is a powerful JavaScript library for building fast, scalable front-end applications. Created by Facebook, it's known for its component-based structure, single-page applications (SPAs), and virtual DOM,enabling efficient UI updates and a seamless user experience.Note: The latest stable version
7 min read
JavaScript Interview Questions and Answers JavaScript is the most used programming language for developing websites, web servers, mobile applications, and many other platforms. In Both Front-end and Back-end Interviews, JavaScript was asked, and its difficulty depends upon the on your profile and company. Here, we compiled 70+ JS Interview q
15+ min read
Domain Name System (DNS) DNS is a hierarchical and distributed naming system that translates domain names into IP addresses. When you type a domain name like www.geeksforgeeks.org into your browser, DNS ensures that the request reaches the correct server by resolving the domain to its corresponding IP address.Without DNS, w
8 min read
NodeJS Interview Questions and Answers NodeJS is one of the most popular runtime environments, known for its efficiency, scalability, and ability to handle asynchronous operations. It is built on Chromeâs V8 JavaScript engine for executing JavaScript code outside of a browser. It is extensively used by top companies such as LinkedIn, Net
15+ min read
HTML Interview Questions and Answers HTML (HyperText Markup Language) is the foundational language for creating web pages and web applications. Whether you're a fresher or an experienced professional, preparing for an HTML interview requires a solid understanding of both basic and advanced concepts. Below is a curated list of 50+ HTML
14 min read
What is an API (Application Programming Interface) In the tech world, APIs (Application Programming Interfaces) are crucial. If you're interested in becoming a web developer or want to understand how websites work, you'll need to familiarize yourself with APIs. Let's break down the concept of an API in simple terms.What is an API?An API is a set of
10 min read
Top 10 Projects For Beginners To Practice HTML and CSS Skills Learning to code is an exciting journey, especially when stepping into the world of programming with HTML and CSSâthe foundation of every website you see today. For most beginners, these two building blocks are the perfect starting point to explore the creative side of web development, designing vis
8 min read