Tillitsdone
down Scroll to discover

CSS scrollbar-color Customizing Scrollbars for Better Design

Explore the CSS scrollbar-color property to customize scrollbar colors for better design and user experience.

Learn about available options and usage.
thumbnail

Introduction

The scrollbar-color property in CSS lets you customize the appearance of scrollbars. By setting the color of the scrollbar thumb (the part you drag) and track (the background), you can improve the visual appeal and user experience of your web pages. This article covers the scrollbar-color property, its syntax, values, accessibility considerations, and practical examples to help you use this feature effectively in your web projects.

Understanding the scrollbar-color Property

The scrollbar-color property allows you to customize the colors of the scrollbar thumb and track. This property can significantly enhance the visual appeal of your web designs when applied to elements with overflow.

Components of the Scrollbar

  • Thumb: The part of the scrollbar that users drag to scroll through content.
  • Track: The background area of the scrollbar.

By customizing these components, you can make your scrollbars more visually appealing and consistent with your design aesthetic.

Syntax

The syntax for the scrollbar-color property is straightforward:

/* Keyword values */
scrollbar-color: auto;
/* <color> values */
scrollbar-color: rebeccapurple green; /* Thumb color is rebeccapurple, track color is green */
/* Global values */
scrollbar-color: inherit;
scrollbar-color: initial;
scrollbar-color: revert;
scrollbar-color: revert-layer;
scrollbar-color: unset;

Explanation of Syntax

  • auto: Lets the browser use its default rendering for the scrollbar.
  • <color> <color>: Sets custom colors for the thumb and track.
  • Global values:
    • inherit: Inherits the color properties from the parent element.
    • initial: Resets the scrollbar color to its initial default value.
    • revert: Reverts the scrollbar color to the browser’s default style.
    • revert-layer: Similar to revert, but respects CSS cascade layers.
    • unset: Acts as a shorthand for initial or inherit.

Property Values

The scrollbar-color property accepts several values that let you customize the appearance of the scrollbar.

Keyword Values

  • auto: Uses the browser’s default rendering.

Color Values

  • <color> <color>: Specifies custom colors for the thumb and track.

Global Values

  • inherit: Inherits colors from the parent element.
  • initial: Resets to the initial default value.
  • revert: Reverts to the browser’s default style.
  • revert-layer: Reverts while considering CSS cascade layers.
  • unset: Shorthand for initial or inherit.

Examples of Property Values

/* Using the auto keyword */
.scrollbar-auto {
scrollbar-color: auto;
}
/* Using custom colors */
.scrollbar-colored {
scrollbar-color: red green; /* Thumb color is red, track color is green */
}
/* Using global values */
.scrollbar-inherit {
scrollbar-color: inherit;
}
.scrollbar-initial {
scrollbar-color: initial;
}
.scrollbar-revert {
scrollbar-color: revert;
}
.scrollbar-revert-layer {
scrollbar-color: revert-layer;
}
.scrollbar-unset {
scrollbar-color: unset;
}

Important Notes

  • Accessibility: Ensure enough contrast between thumb and track colors for accessibility.
  • Browser Compatibility: Check browser compatibility before using this property.

Accessibility

When using the scrollbar-color property, consider accessibility to ensure all users can easily interact with your webpage.

Contrast Ratio

  1. Ensure Sufficient Contrast: Use a contrast ratio of at least 3:1 between the thumb and track colors.
  2. User Agent Responsibilities: For auto, user agents should ensure default colors have enough contrast.

Practical Tips

  • Test Different Color Combinations: Use tools like color contrast checkers to verify accessibility.
  • Dark Mode Considerations: Adjust scrollbar colors for dark mode.
  • High Contrast Mode: Consider how colors will appear in high contrast mode.

Example

.accessible-scrollbar {
scrollbar-color: #007 #bada55; /* Custom scrollbar colors with sufficient contrast */
}

Additional Resources

  • WCAG Guidelines: Refer to [WCAG 2.0 Techniques for G183]WebsiteUrl for detailed information.
  • Color Contrast Checkers: Use online tools like the [WebAIM Color Contrast Checker]WebsiteUrl.

Browser Compatibility

The scrollbar-color property has varying support across browsers. Understanding this can help you make informed decisions about its use.

Current Support

  • Google Chrome: Supported in recent versions.
  • Mozilla Firefox: Supported.
  • Microsoft Edge: Supported.
  • Opera: Supported.
  • Safari: Limited support.

Browser Compatibility Table

BrowserSupport Status
Google ChromeSupported
Mozilla FirefoxSupported
Microsoft EdgeSupported
OperaSupported
SafariLimited Support

Fallbacks and Alternatives

  • Progressive Enhancement: Use scrollbar-color as a progressive enhancement.
  • Feature Detection: Use JavaScript to detect support.
  • CSS Custom Properties: Use variables to manage styles.

Example with Fallback

/* Define custom properties for scrollbar colors */
:root {
--scrollbar-thumb-color: #007;
--scrollbar-track-color: #bada55;
}
/* Apply scrollbar-color property */
.scroller {
width: 300px;
height: 100px;
overflow-y: scroll;
scrollbar-color: var(--scrollbar-thumb-color) var(--scrollbar-track-color);
}
/* Fallback for browsers that do not support scrollbar-color */
.scroller::-webkit-scrollbar-thumb {
background-color: var(--scrollbar-thumb-color);
}
.scroller::-webkit-scrollbar-track {
background-color: var(--scrollbar-track-color);
}

Examples

Example 1: Using scrollbar-color with auto

.scrollbar-auto {
scrollbar-color: auto;
}

Example 2: Using Custom Colors

.scrollbar-colored {
scrollbar-color: rebeccapurple green; /* Thumb color is rebeccapurple, track color is green */
}

Example 3: Using Global Values

.scrollbar-inherit {
scrollbar-color: inherit;
}
.scrollbar-initial {
scrollbar-color: initial;
}
.scrollbar-revert {
scrollbar-color: revert;
}
.scrollbar-revert-layer {
scrollbar-color: revert-layer;
}
.scrollbar-unset {
scrollbar-color: unset;
}

Example 4: Ensuring Accessibility

.accessible-scrollbar {
scrollbar-color: #007 #bada55; /* Custom scrollbar colors with sufficient contrast */
}

Example 5: Example with Fallback

/* Define custom properties for scrollbar colors */
:root {
--scrollbar-thumb-color: #007;
--scrollbar-track-color: #bada55;
}
/* Apply scrollbar-color property */
.scroller {
width: 300px;
height: 100px;
overflow-y: scroll;
scrollbar-color: var(--scrollbar-thumb-color) var(--scrollbar-track-color);
}
/* Fallback for browsers that do not support scrollbar-color */
.scroller::-webkit-scrollbar-thumb {
background-color: var(--scrollbar-thumb-color);
}
.scroller::-webkit-scrollbar-track {
background-color: var(--scrollbar-track-color);
}

By understanding and using the scrollbar-color property effectively, you can enhance the visual appeal and user experience of your web projects. Always consider accessibility and browser compatibility to ensure a consistent and inclusive experience for all users.

icons/css-4.svg CSS Blogs
CSS3 is the latest version of Cascading Style Sheets, offering advanced styling features like animations, transitions, shadows, gradients, and responsive design.
icons/logo-tid.svg

Talk with CEO

Ready to bring your web/app to life or boost your team with expert Thai developers?
Contact us today to discuss your needs, and let’s create tailored solutions to achieve your goals. We’re here to help at every step!
🖐️ Contact us
Let's keep in Touch
Thank you for your interest in Tillitsdone! Whether you have a question about our services, want to discuss a potential project, or simply want to say hello, we're here and ready to assist you.
We'll be right here with you every step of the way.
Contact Information
rick@tillitsdone.com+66824564755
Find All the Ways to Get in Touch with Tillitsdone - We're Just a Click, Call, or Message Away. We'll Be Right Here, Ready to Respond and Start a Conversation About Your Needs.
Address
9 Phahonyothin Rd, Khlong Nueng, Khlong Luang District, Pathum Thani, Bangkok Thailand
Visit Tillitsdone at Our Physical Location - We'd Love to Welcome You to Our Creative Space. We'll Be Right Here, Ready to Show You Around and Discuss Your Ideas in Person.
Social media
Connect with Tillitsdone on Various Social Platforms - Stay Updated and Engage with Our Latest Projects and Insights. We'll Be Right Here, Sharing Our Journey and Ready to Interact with You.
We anticipate your communication and look forward to discussing how we can contribute to your business's success.
We'll be here, prepared to commence this promising collaboration.
Frequently Asked Questions
Explore frequently asked questions about our products and services.
Whether you're curious about features, warranties, or shopping policies, we provide comprehensive answers to assist you.