Course contentsShow
← Web Development
Lesson 251 of 2,9824. CSS Layout FoundationsFree lesson

Scrollbar styling with ::-webkit-scrollbar

Customize scrollbar appearance using vendor-specific pseudo-elements where supported.

Scrollbar Styling with ::-webkit-scrollbar

You'll learn how to customize the appearance of scrollbars using special browser-specific pseudo-elements.

What This Means

By default, browsers display scrollbars using their operating system's native styles. But you can override this default look using ::-webkit-scrollbar and related pseudo-elements—at least in browsers that support them (Chrome, Edge, Safari, and other WebKit/Blink-based browsers). This lets you match scrollbar colors and sizes to your site's design.

How It Works

The ::-webkit-scrollbar family includes several pseudo-elements that target different parts of the scrollbar:

  • ::-webkit-scrollbar — the entire scrollbar track and thumb
  • ::-webkit-scrollbar-track — the background "track" the thumb slides along
  • ::-webkit-scrollbar-thumb — the draggable handle users click and drag
  • ::-webkit-scrollbar-button — the arrow buttons (if shown)

You style these just like regular elements, using properties like background, border-radius, and width (for vertical scrollbars) or height (for horizontal ones).

/* Style a scrollable container's scrollbar */
.scrollable-box {
  overflow: auto;
  height: 300px;
}

.scrollable-box::-webkit-scrollbar {
  width: 12px; /* width of vertical scrollbar */
}

.scrollable-box::-webkit-scrollbar-track {
  background: #f1f1f1;
}

.scrollable-box::-webkit-scrollbar-thumb {
  background: #888;
  border-radius: 6px;
}

.scrollable-box::-webkit-scrollbar-thumb:hover {
  background: #555;
}

Important Limitation

These pseudo-elements only work in WebKit/Blink browsers. Firefox and older browsers ignore them, so always ensure your design remains usable with default scrollbars.

Key Takeaway: Use ::-webkit-scrollbar pseudo-elements to customize scrollbar appearance in supported browsers, but remember this is vendor-specific and won't work everywhere.