乐闻世界logo
搜索文章和话题

How to using css to customized scroll bar in div

4个答案

1
2
3
4

In CSS, we can customize the scrollbar styles of HTML elements by using pseudo-elements and pseudo-classes. For a div element, we can target ::-webkit-scrollbar and its related pseudo-elements to control various parts of the scrollbar, such as the scrollbar itself, the scrollbar track, and the scrollbar thumb.

Here is a simple example demonstrating how to customize a div's scrollbar:

css
/* Selector targeting the div's scrollbar */ div::-webkit-scrollbar { width: 12px; /* Set scrollbar width */ background-color: #F5F5F5; /* Set scrollbar background color */ } /* Scrollbar track */ div::-webkit-scrollbar-track { box-shadow: inset 0 0 5px grey; /* Add inner shadow effect */ border-radius: 10px; /* Set track rounded corners */ } /* Scrollbar thumb */ div::-webkit-scrollbar-thumb { background: #888; /* Thumb color */ border-radius: 10px; /* Thumb rounded corners */ } /* Thumb hover effect */ div::-webkit-scrollbar-thumb:hover { background: #555; /* Color change on hover */ }

In the above example, the div element's scrollbar width is set to 12 pixels with a gray background. The scrollbar track features an inner shadow and rounded corners, while the scrollbar thumb has distinct background colors and rounded corners. When hovering over the thumb, its color shifts to a darker gray.

Note that these styles primarily apply to WebKit-based browsers like Chrome and Safari. For other browsers, such as Firefox, you should use the pseudo-elements scrollbar-width and scrollbar-color:

css
/* Firefox scrollbar styles */ div { scrollbar-width: thin; /* Options: 'auto', 'thin', or 'none' */ scrollbar-color: #888 #F5F5F5; /* Thumb color and track color */ }

In practical projects, considering browser compatibility, you may need to combine the WebKit-specific selectors mentioned earlier with CSS properties for other browsers to ensure the customized scrollbar styles are visible to as many users as possible. Finally, as web standards evolve, methods for customizing scrollbars may change over time. Therefore, in real-world applications, always refer to the latest CSS standards and browser support.

2024年6月29日 12:07 回复

CSS cannot directly customize scrollbars; you need some JavaScript magic.

Some browsers support non-standard CSS rules, such as ::-webkit-scrollbar in WebKit, but it's not ideal because it only works in WebKit.

Internet Explorer also had similar features, but I believe they no longer support it.

2024年6月29日 12:07 回复

Please check this link: CSS Scrollbar

Working Example

css
#style-1::-webkit-scrollbar-track { -webkit-box-shadow: inset 0 0 6px rgba(0,0,0,0.3); border-radius: 10px; background-color: #F5F5F5; } #style-1::-webkit-scrollbar { width: 12px; background-color: #F5F5F5; } #style-1::-webkit-scrollbar-thumb { border-radius: 10px; -webkit-box-shadow: inset 0 0 6px rgba(0,0,0,.3); background-color: #555; }
2024年6月29日 12:07 回复

I find it helpful to integrate the latest information on scrollbars, CSS, and browser compatibility.

CSS Support for Scrollbars

Currently, there is no standardized CSS for cross-browser scrollbars. The W3C article I referenced earlier (W3C article) states and was recently updated (October 10, 2014):

Some browsers (IE, Konqueror) support non-standard properties such as 'scrollbar-shadow-color' and 'scrollbar-track-color'. However, these properties are invalid: they are not defined in any CSS specification and are not marked as proprietary (by prefixing them with '-vendor-').

Microsoft

As others have mentioned, Microsoft supports scrollbar styling, but only for IE8 and later versions.

Example:

css
.TA { scrollbar-3dlight-color: gold; scrollbar-arrow-color: blue; scrollbar-base-color:; scrollbar-darkshadow-color: blue; scrollbar-face-color:; scrollbar-highlight-color:; scrollbar-shadow-color: }

Chrome and Safari (WebKit)

Similarly, WebKit now has its own version:

Firefox (Gecko)

Starting from version 64, Firefox supports scrollbar styling through the properties scrollbar-color (part of the W3C draft) and scrollbar-width (W3C draft). Useful implementation details can be found in this answer.

Cross-Browser Scrollbar Styling

JavaScript libraries and plugins can provide cross-browser solutions. Many options are available.

This list can be extended. The best choice is to search, research, and test available solutions. I believe you will find something suitable for your needs.

Preventing Invalid Scrollbar Styling

In case you want to prevent invalid scrollbar styling that lacks the proper '-vendor-' prefix, this W3C article provides basic instructions. Essentially, you need to add the following CSS to your browser's user stylesheet. These definitions will override any invalid scrollbar styles on the pages you visit.

css
body, html { scrollbar-face-color: ThreeDFace !important; scrollbar-shadow-color: ThreeDDarkShadow !important; scrollbar-highlight-color: ThreeDHighlight !important; scrollbar-3dlight-color: ThreeDLightShadow !important; scrollbar-darkshadow-color: ThreeDDarkShadow !important; scrollbar-track-color: Scrollbar !important; scrollbar-arrow-color: ButtonText !important; }

Related Questions or Unlinked Sources

Note: This answer incorporates information from various sources. If sources are used, they are linked within this answer.

2024年6月29日 12:07 回复

你的答案