Custom CSS Scrollbar for Firefox

后端 未结 10 2383
遥遥无期
遥遥无期 2020-11-22 02:23

I want to customize a scrollbar with CSS.

I use this WebKit CSS code, which works well for Safari and Chrome:

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


        
相关标签:
10条回答
  • 2020-11-22 02:51

    Here I have tried this CSS for all major browser & tested: Custom color are working fine on scrollbar.

    Yes, there are limitations on several versions of different browsers.

    /* 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>

    0 讨论(0)
  • 2020-11-22 02:53

    Since Firefox 64, is possible to use new specs for a simple Scrollbar styling (not as complete as in Chrome with vendor prefixes).

    In this example is possible to see a solution that combine different rules to address both Firefox and Chrome with a similar (not equal) final result (example use your original Chrome rules):

    The key rules are:

    For Firefox

    .scroller {
      overflow-y: scroll;
      scrollbar-color: #0A4C95 #C2D2E4;
    }
    

    For Chrome

    .scroller::-webkit-scrollbar {
        width: 15px;
        height: 15px;
    }
    
    .scroller::-webkit-scrollbar-track-piece  {
        background-color: #C2D2E4;
    }
    
    .scroller::-webkit-scrollbar-thumb:vertical {
        height: 30px;
        background-color: #0A4C95;
    }
    

    Please note that respect to your solution, is possible to use also simpler Chrome rules as the following:

    .scroller::-webkit-scrollbar-track  {
        background-color: #C2D2E4;
    }
    
    .scroller::-webkit-scrollbar-thumb {
        height: 30px;
        background-color: #0A4C95;
    }
    

    Finally, in order to hide arrows in scrollbars also in Firefox, currently is necessary to set it as "thin" with the following rule scrollbar-width: thin;

    0 讨论(0)
  • 2020-11-22 02:57

    Year 2020 this works

    /* 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

    0 讨论(0)
  • 2020-11-22 02:58

    I thought I would share my findings in case someone is considering a JQuery plugin to do the job.

    I gave JQuery Custom Scrollbar a go. It's pretty fancy and does some smooth scrolling (with scrolling inertia) and has loads of parameters you can tweak, but it ended up being a bit too CPU intensive for me (and it adds a fair amount to the DOM).

    Now I'm giving Perfect Scrollbar a go. It's simple and lightweight (6KB) and it's doing a decent job so far. It's not CPU intensive at all (as far as I can tell) and adds very little to your DOM. It's only got a couple of parameters to tweak (wheelSpeed and wheelPropagation), but it's all I need and it handles updates to the scrolling content nicely (such as loading images).

    P.S. I did have a quick look at JScrollPane, but @simone is right, it's a bit dated now and a PITA.

    0 讨论(0)
提交回复
热议问题