将 webkit 滚动条样式应用于指定元素
我对以双冒号为前缀的伪元素很陌生。我看到一篇博客文章讨论使用一些仅适用于 webkit 的 css 来设置滚动条的样式。伪元素 CSS 可以应用于单个元素吗?
/* This works by applying style to all scroll bars in window */
::-webkit-scrollbar {
width: 12px;
}
/* This does not apply the scrollbar to anything */
div ::-webkit-scrollbar {
width: 12px;
}
在这个小提琴中,我想自定义 div 的滚动条,但主窗口的滚动条保持默认状态。
I am new to pseudo-elements that are prefixed with a double colon. I came across a blog article discussing styling of scrollbars using some webkit only css. Can the pseudo-element CSS be applied to individual elements?
/* This works by applying style to all scroll bars in window */
::-webkit-scrollbar {
width: 12px;
}
/* This does not apply the scrollbar to anything */
div ::-webkit-scrollbar {
width: 12px;
}
In this fiddle, I would like to make the div's scrollbar customized, but the main window's scrollbar stay at the default.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你的想法是正确的。但是,在
div
后带有空格的符号div ::-webkit-scrollbar
实际上与div *::-webkit-scrollbar
相同;这个选择器的意思是“div::-webkit-scrollbar
。请参阅 http://jsfiddle.net/4xMUB/2/ 处的演示
Your idea was correct. However, the notation
div ::-webkit-scrollbar
with a space afterdiv
is actually the same asdiv *::-webkit-scrollbar
; this selector means "scrollbar of any element inside<div>
". Usediv::-webkit-scrollbar
.See demo at http://jsfiddle.net/4xMUB/2/
我想使用类选择器来使用自定义滚动条。
不知何故
.foo::-webkit
不起作用,但我发现div.foo::-webkit
确实有效!那些##$$*##伪东西....参见http://jsfiddle.net/QcqBM/16/
I want to use a class selector for using a custom scrollbar.
Somehow
.foo::-webkit
doesn't work, but I figured out thatdiv.foo::-webkit
does work! Those ##$$*## pseudo-things....See http://jsfiddle.net/QcqBM/16/
您还可以通过元素的 id 应用这些规则。假设 div 的滚动条必须设置其 id“myDivId”的样式。然后你就可以进行以下操作。这样你就可以为不同元素的滚动条使用不同的样式。
http://jsfiddle.net/QcqBM/516/
You can also apply these rules by id of the element. Let's say scroll bar of a div has to be styled which has an id "myDivId". Then you can do following. This way you can use different styles for scroll bars of different elements.
http://jsfiddle.net/QcqBM/516/
您可以拥有一个 scss 文件并将样式应用于
style.scss
home.html
中的类,我在这里使用了滚动条生成器: https ://w3generator.com/scrollbar
You can have a scss file and apply the style to a class there
style.scss
home.html
I used the scrollbar generator here: https://w3generator.com/scrollbar