---
title: "Style HTML scrollbars with CSS in C#"
url: https://recursiveai.org/onerender/cookbook/style-html-scrollbars-with-css/
description: "Set scrollbar width and thumb and track colors with CSS, then render the styled HTML panel to a PNG in C# using OneRender’s classic scrollbar mode."
version: "1.6.1"
---

# 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.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneRender.Image) · [GitHub](https://github.com/recursive-ai-oss/onerender-csharp) · [Docs](https://recursiveai.org/onerender/docs/) · [Download example (.zip)](https://recursiveai.org/onerender/cookbook/style-html-scrollbars-with-css/style-html-scrollbars-with-css.zip)

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.

```shell
dotnet add package RecursiveAI.OneRender.Image
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (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](https://recursiveai.org/onerender/cookbook/style-html-scrollbars-with-css/style-html-scrollbars-with-css.zip) is the full program below: unzip it and `dotnet run`.

## The full program

```csharp
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](https://recursiveai.org/onerender/images/style-html-scrollbars-with-css/panel.png)

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;
```

## Related recipes

- [Render HTML to PNG](https://recursiveai.org/onerender/cookbook/html-to-png/)
- [Scroll a rendered HTML container in C#](https://recursiveai.org/onerender/cookbook/scroll-rendered-html-page/)

## 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](https://recursiveai.org/onerender/docs/layout-conventions/#scrollbars) 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](https://developer.mozilla.org/en-US/docs/Web/CSS/Guides/Scrollbars_styling) 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.
