我想用CSS自定义一个滚动条。

我使用这个WebKit CSS代码,它适用于Safari和Chrome:

::-webkit-scrollbar {
  width: 15px;
  height: 15px;
}

::-webkit-scrollbar-track-piece {
  background-color: #c2d2e4;
}

::-webkit-scrollbar-thumb:vertical {
  height: 30px;
  background-color: #0a4c95;
}

我如何在Firefox中做同样的事情?

我知道我可以很容易地用jQuery来做,但我更喜欢用纯CSS来做,如果它是可行的。


当前回答

2020年,这个可行

/* Thin Scrollbar */
:root{
  scrollbar-color: rgb(210,210,210) rgb(46,54,69) !important;
  scrollbar-width: thin !important;
}

https://github.com/Aris-t2/CustomCSSforFx/issues/160

其他回答

2022年

使用最新的Firefox和Chrome版本进行测试

下面的代码片段将在Chrome和Firefox上显示相同的滚动条样式,请尝试一下。

html { /*对于Firefox */ overflow-y:滚动; 滚动条-color: #008de4 #0d3b97; scrollbar-width:薄; } /*用于Chrome和除Firefox之外的其他浏览器*/ 身体:-webkit-scrollbar { 宽度:0.5 em; background - color: # 0 d3b97; } 身体:-webkit-scrollbar-thumb { background - color: # 008 de4; } < html > <body style="height: 600px"></body> . < / html >

还可以使用以下方法自定义滚动条跟踪(但不适用于Firefox)

::-webkit-scrollbar-track

在这里,我已经尝试了所有主要浏览器的CSS和测试:自定义颜色在滚动条上工作良好。

是的,不同浏览器的几个版本都有限制。

/* Only Chrome */ html::-webkit-scrollbar {width: 17px;} html::-webkit-scrollbar-thumb {background-color: #0064a7; background-clip: padding-box; border: 1px solid #8ea5b5;} html::-webkit-scrollbar-track {background-color: #8ea5b5; } ::-webkit-scrollbar-button {background-color: #8ea5b5;} /* Only IE */ html {scrollbar-face-color: #0064a7; scrollbar-shadow-color: #8ea5b5; scrollbar-highlight-color: #8ea5b5;} /* Only FireFox */ html {scrollbar-color: #0064a7 #8ea5b5;} /* View Scrollbar */ html {overflow-y: scroll;overflow-x: hidden;} <!doctype html> <html lang="en" class="no-js"> <head> <meta charset="utf-8"> <meta http-equiv="x-ua-compatible" content="ie=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body> <header> <div id="logo"><img src="/logo.png">HTML5&nbsp;Layout</div> <nav> <ul> <li><a href="/">Home</a> <li><a href="https://html-css-js.com/">HTML</a> <li><a href="https://html-css-js.com/css/code/">CSS</a> <li><a href="https://htmlcheatsheet.com/js/">JS</a> </ul> </nav> </header> <section> <strong>Demonstration of a simple page layout using HTML5 tags: header, nav, section, main, article, aside, footer, address.</strong> </section> <section id="pageContent"> <main role="main"> <article> <h2>Stet facilis ius te</h2> <p>Lorem ipsum dolor sit amet, nonumes voluptatum mel ea, cu case ceteros cum. Novum commodo malorum vix ut. Dolores consequuntur in ius, sale electram dissentiunt quo te. Cu duo omnes invidunt, eos eu mucius fabellas. Stet facilis ius te, quando voluptatibus eos in. Ad vix mundi alterum, integre urbanitas intellegam vix in.</p> </article> <article> <h2>Illud mollis moderatius</h2> <p>Eum facete intellegat ei, ut mazim melius usu. Has elit simul primis ne, regione minimum id cum. Sea deleniti dissentiet ea. Illud mollis moderatius ut per, at qui ubique populo. Eum ad cibo legimus, vim ei quidam fastidii.</p> </article> <article> <h2>Ex ignota epicurei quo</h2> <p>Quo debet vivendo ex. Qui ut admodum senserit partiendo. Id adipiscing disputando eam, sea id magna pertinax concludaturque. Ex ignota epicurei quo, his ex doctus delenit fabellas, erat timeam cotidieque sit in. Vel eu soleat voluptatibus, cum cu exerci mediocritatem. Malis legere at per, has brute putant animal et, in consul utamur usu.</p> </article> <article> <h2>His at autem inani volutpat</h2> <p>Te has amet modo perfecto, te eum mucius conclusionemque, mel te erat deterruisset. Duo ceteros phaedrum id, ornatus postulant in sea. His at autem inani volutpat. Tollit possit in pri, platonem persecuti ad vix, vel nisl albucius gloriatur no.</p> </article> </main> <aside> <div>Sidebar 1</div> <div>Sidebar 2</div> <div>Sidebar 3</div> </aside> </section> <footer> <p>&copy; You can copy, edit and publish this template but please leave a link to our website | <a href="https://html5-templates.com/" target="_blank" rel="nofollow">HTML5 Templates</a></p> <address> Contact: <a href="mailto:me@example.com">Mail me</a> </address> </footer> </body> </html>

关键字值 scrollbar-width: |汽车|薄没有;

全局值 scrollbar-width:继承初始| |回复| revert-layer; scrollbar-width:设置;

Firefox 84于2020年12月15日发布。已删除的属性如下:

删除了专有的-moz-default-appearance属性值scrollbar-small(使用scrollbar-width: thin代替)和scrollbar(仅macOS;使用水平滚动条和垂直滚动条)(bug 1673132)。

这个特性还处于实验阶段,我想Mozilla还有很多工作要做,直到它对每个页面上的每个人都可用为止。我已经测试了许多解决方案,但下面的代码工作得很完美。

CSS

:root {
  scrollbar-color: #333333 #999999 !important;
  scrollbar-width: thin !important;
}

OR

#ClassName {
  scrollbar-color: #333333 #999999 !important;
  scrollbar-width: thin !important;
}

参考: 链接1 链接2

.mat-tab-header {
   overflow-x: scroll !important;
   // For Firefox
   scrollbar-color: transparent transparent;
   border-bottom: none; 
}
    
.mat-tab-header::-webkit-scrollbar { // TO Remove horizontal scrollbar in tabs
   display: none !important;
}

2021

火狐

.nav{
scrollbar-width: 0px;
scrollbar-width: none;}

::-webkit-scrollbar {
  height: 0;  /* Remove scrollbar space */
  background: transparent;  
/* Optional: just make scrollbar invisible */
}

对于垂直和水平滚动条,更改宽度或高度属性