如何使用 CSS 增加滚动条的宽度?

发布于 2024-09-29 23:36:51 字数 193 浏览 6 评论 0原文

是否可以增加放置在 内的

元素上滚动条的宽度?

我不是在谈论浏览器本身的默认滚动条,该页面以全屏模式运行,并且因为浏览器滚动条永远不会出现在图片中,所以内部

元素有自己的滚动条。

Is it possible to increase the width of a scrollbar on a <div> element placed inside the <body>?

I am not talking about the default scrollbar on the browser itself, this page runs in full screen mode and because the browser scrollbar never comes into picture, the inner <div> element has its own scrollbar.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

凌乱心跳 2024-10-06 23:36:51

这可以在基于 WebKit 的浏览器(例如 Chrome 和 Safari)中仅使用 CSS 来完成:

::-webkit-scrollbar {
    width: 2em;
    height: 2em
}
::-webkit-scrollbar-button {
    background: #ccc
}
::-webkit-scrollbar-track-piece {
    background: #888
}
::-webkit-scrollbar-thumb {
    background: #eee
}​

JSFiddle 演示


参考:

This can be done in WebKit-based browsers (such as Chrome and Safari) with only CSS:

::-webkit-scrollbar {
    width: 2em;
    height: 2em
}
::-webkit-scrollbar-button {
    background: #ccc
}
::-webkit-scrollbar-track-piece {
    background: #888
}
::-webkit-scrollbar-thumb {
    background: #eee
}​

JSFiddle Demo


References:

水波映月 2024-10-06 23:36:51

如果您正在谈论使用 overflow:scroll (或 auto)自动出现在 div 上的滚动条,那么不,它仍然是原生的浏览器使用普通操作系统小部件呈现的滚动条,而不是可以设置样式的东西(*)。

虽然您可以按照 Matt 的建议将其替换为由可样式化的 div 和 JavaScript 组成的代理,但我不建议在一般情况下使用它。脚本驱动的滚动条的行为永远不会与真实操作系统滚动条完全相同,从而导致可用性和可访问性问题。

(*:除了 IE 着色样式,我也不推荐这种样式。除了仅适用于 IE 之外,使用它们还迫使 IE 从使用当前 Windows 主题中漂亮的滚动条图像转向丑陋的旧 Win95 样式滚动条.)

If you are talking about the scrollbar that automatically appears on a div with overflow: scroll (or auto), then no, that's still a native scrollbar rendered by the browser using normal OS widgets, and not something that can be styled(*).

Whilst you can replace it with a proxy made out of stylable divs and JavaScript as suggested by Matt, I wouldn't recommend it for the general case. Script-driven scrollbars never quite behave exactly the same as real OS scrollbars, causing usability and accessibility problems.

(*: Except for the IE colouring styles, which I wouldn't really recommend either. Apart from being IE-only, using them forces IE to fall back from using nice scrollbar images from the current Windows theme to ugly old Win95-style scrollbars.)

画▽骨i 2024-10-06 23:36:51

您可以为 div 建立特定的工具栏,

div::-webkit-scrollbar {
width: 12px;
}

div::-webkit-scrollbar-track {
-webkit-box-shadow: inset 0 0 6px rgba(0,0,0,0.3);
border-radius: 10px;
}

请参阅 jsfiddle.net 中的演示

You can stablish specific toolbar for div

div::-webkit-scrollbar {
width: 12px;
}

div::-webkit-scrollbar-track {
-webkit-box-shadow: inset 0 0 6px rgba(0,0,0,0.3);
border-radius: 10px;
}

see demo in jsfiddle.net

一腔孤↑勇 2024-10-06 23:36:51

这设置了滚动条宽度:

::-webkit-scrollbar {
  width: 8px;   // for vertical scroll bar
  height: 8px;  // for horizontal scroll bar
}

// for Firefox add this class as well
.thin_scroll{
  scrollbar-width: thin; // auto | thin | none | <length>;
}

This sets the scrollbar width:

::-webkit-scrollbar {
  width: 8px;   // for vertical scroll bar
  height: 8px;  // for horizontal scroll bar
}

// for Firefox add this class as well
.thin_scroll{
  scrollbar-width: thin; // auto | thin | none | <length>;
}
花之痕靓丽 2024-10-06 23:36:51

是的。

如果滚动条不是浏览器滚动条,那么它将由常规 HTML 元素(可能是 divspan)构建,因此可以设置样式(或者将是 Flash 、Java 等,并且可以根据这些环境进行定制)。

具体细节取决于所使用的 DOM 结构。

Yes.

If the scrollbar is not the browser scrollbar, then it will be built of regular HTML elements (probably divs and spans) and can thus be styled (or will be Flash, Java, etc and can be customized as per those environments).

The specifics depend on the DOM structure used.

我不咬妳我踢妳 2024-10-06 23:36:51

我尝试使用 CSS 修改滚动条的经验是不可以。只有 IE 可以让你这样做。

My experience with trying to use CSS to modify the scroll bars is don't. Only IE will let you do this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文