Recursive AI

Style HTML scrollbars with CSS in C#

OneRender is a free, open-source HTML and CSS rendering library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

Set scrollbar-width and scrollbar-color on the scrollable element. PNG renders draw no scrollbars unless you set HtmlRenderOptions.Scrollbars to ScrollbarMode.Classic, which draws the bars in a gutter that takes up space inside the box.

Terminal
dotnet add package RecursiveAI.OneRender.Image

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

Save this as panel.html, or use the copy in the zip:

HTML
<style>
  .panel {
    width: 200px;
    height: 100px;
    overflow: auto;
    scrollbar-width: thin;
    scrollbar-color: #164e63 #cffafe;
    border: 1px solid #164e63;
  }
</style>
<div class="panel">
  <div style="height:240px">Notes</div>
</div>

The example is the full program below: unzip it and dotnet run.

The full program

C#
using OneRender.Image;
using OneRender.Layout;
 
var html = File.ReadAllText("panel.html");
var image = HtmlRender.ToImage(html,
    new HtmlRenderOptions
    {
        Width = 220,
        Height = 120,
        Scrollbars = ScrollbarMode.Classic
    });
File.WriteAllBytes("panel.png", image.ToPng());
Console.WriteLine("Wrote panel.png");
Text
Wrote panel.png

Rendered Notes panel with a thin teal thumb on a light teal scrollbar track

If the bar is missing from a PNG, check that Scrollbars is set to ScrollbarMode.Classic and that the content actually overflows the panel.

Variations

Keep the colors but use the regular width. Replace the width declaration in panel.html. The resulting PNG has a 15 px bar instead of 10 px:

CSS
scrollbar-width: auto;

The short answer

Set scrollbar-width: thin and scrollbar-color: #164e63 #cffafe on the box with overflow: auto. The first color is for the thumb (the draggable handle), the second for the track behind it. Interactive pages paint classic scrollbars by default. For a one-shot PNG, opt in with HtmlRenderOptions.Scrollbars = ScrollbarMode.Classic as in the sample. OneRender doesn’t paint scrollbars in paged output or PDFs.

How do I change the width of a CSS scrollbar?

Use auto for the regular width, thin for a narrower bar, or none to hide it without disabling scrolling. scrollbar-width doesn’t take a length such as 10px. In OneRender’s classic mode the gutter, the space set aside for the bar, is 15 px for auto and 10 px for thin. In the example the outer box keeps the same size: the thinner bar leaves 190 px inside the 200 px-wide content area, while the regular width leaves 185 px. That can affect where a line of text wraps.

If content sometimes fits and sometimes overflows, scrollbar-gutter: stable on the panel reserves that space even on the render that doesn’t need a bar. Use stable both-edges to reserve matching space on the opposite side as well. See scrollbar layout in OneRender for how the viewport and element gutters differ.

How do I change the scrollbar thumb and track colors?

Give scrollbar-color two colors in thumb-then-track order. In OneRender the first color also paints the arrows; the second also paints the corner where horizontal and vertical bars meet. scrollbar-color is inherited, so a nested scroll container can pick up the panel’s colors. Set scrollbar-color: auto on that nested box if you want it to use the default colors for its color scheme instead. Pick colors that keep the thumb easy to see against the track and the surrounding panel, as the CSS scrollbars guide recommends.

Why isn’t my CSS scrollbar showing up?

Check the box before the colors. A panel without a height constraint can grow to fit its content, so overflow: auto may have nothing to do. This sample gives the panel 100 px of height and its child 240 px. With overflow: scroll, OneRender paints a bar even if everything fits, but there’s no thumb when there’s nothing to scroll.

Then check the output mode. If bars show in your interactive UI but disappear in an image, the two paths use different defaults. Enable classic bars in the image options; changing the colors alone won’t turn them on. If your stylesheet uses only ::-webkit-scrollbar selectors, use scrollbar-width and scrollbar-color for the OneRender output: OneRender doesn’t style those pseudo-elements.

All onerender recipes