---
title: "Scroll a rendered HTML container in C#"
url: https://recursiveai.org/onerender/cookbook/scroll-rendered-html-page/
description: "Scroll a clipped HTML container in C#, repaint it to PNG, and check that viewport hit tests select the visible content after scrolling."
version: "1.4.0"
---

# Scroll a rendered HTML container 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/scroll-rendered-html-page/scroll-rendered-html-page.zip)

Scroll a clipped overflow container, repaint it, and hit-test the visible content. The HTML input is in the zip and shown below.

```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 `input.html`:

```html
<style>
body { margin:0 }
#pane { width:180px; height:60px; overflow:hidden }
#first { height:60px; background:#ef4444 }
#second { height:60px; background:#2563eb }
</style><div id="pane"><div id="first">First</div>
<div id="second">Second</div></div>
```

The snippet and full program both use a 200 × 100 viewport.

```csharp
using OneRender;
using OneRender.Image;
using OneRender.Interaction;
using OneRender.Painting;

var ui = InteractivePage.Create(
    File.ReadAllText("input.html"),
    new HtmlPageOptions(FontProviders.CreateDefault())
    { ViewportWidth = 200, ViewportHeight = 100 });
ui.ScrollElementTo(ui.Page.Document.GetElementById("pane"), 0, 60);
var list = new DisplayList();
ui.Page.Paint(list);
File.WriteAllBytes("scrolled.png",
    DisplayListRasterizer.Render(list, 200, 100).ToPng());
Console.WriteLine("scrolled.png written");
```

```text
scrolled.png written
```

Unzip [the example](https://recursiveai.org/onerender/cookbook/scroll-rendered-html-page/scroll-rendered-html-page.zip), then `dotnet run`.

## The full program

```csharp
using OneRender;
using OneRender.Image;
using OneRender.Interaction;
using OneRender.Painting;

string html = File.ReadAllText("input.html");
var ui = InteractivePage.Create(html,
    new HtmlPageOptions(FontProviders.CreateDefault())
    { ViewportWidth = 200, ViewportHeight = 100 });
var pane = ui.Page.Document.GetElementById("pane");
Save("before.png");
Console.WriteLine($"before at (10,10): #{ui.ElementFromPoint(10, 10).GetAttribute("id")}");
Console.WriteLine($"changed: {ui.ScrollElementTo(pane, 0, 60)}");
Save("after.png");
Console.WriteLine($"scrollTop: {ui.Layout.ScrollTop(pane)}");
Console.WriteLine($"after at (10,10): #{ui.ElementFromPoint(10, 10).GetAttribute("id")}");
Console.WriteLine($"outside at (10,70): #{ui.ElementFromPoint(10, 70).GetAttribute("id")}");

void Save(string path)
{
    var list = new DisplayList();
    ui.Page.Paint(list);
    File.WriteAllBytes(path,
        DisplayListRasterizer.Render(list, 200, 100).ToPng());
    Console.WriteLine(path);
}
```

`dotnet run` prints:

```text
before.png
before at (10,10): #first
changed: True
after.png
scrollTop: 60
after at (10,10): #second
outside at (10,70): #
```

`ScrollElementTo` returns false when the element is not a scroll container or is already at the requested clamped offset. The empty id at (10,70) means the hit is outside the pane, not on its clipped child.

![Before scrolling: the red first block fills the clipped pane](data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMgAAABkCAMAAAD0WI85AAABGlBMVEXvRET///+1MzN9JCTLOjqoMDClLy+xMjIAAAB/JCS5NTW3NDSwMjJjHByiLi5LFRVGFBTuRETANzekLy+uMTHsQ0OLJyftQ0PBNzedLS2vMjLTPDysMTG0MzN4IiJ7IyOrMTHFODgdCAhsHx+tMTHdPz/nQkJiHBw0Dw96IyMUBgZkHR1NFhZwICAsDQ3NOjq9NjYDAQFxICCOKChBEhJ0ISEaBwe/NjZaGhpbGhrQOzsEAQFOFhaTKioCAQG8NjbfQECbLCw9EREBAABvHx8LAwO7NTUIAgKUKipVGBhEExN8IyPqQkI5EBAZBweHJiZJFRUMAwODJSXiQEBnHR08ERF3IiJgGxtMFhZRFxfPOzvjQUHkQUHoQkL2LXG2AAABq0lEQVR4nO3av0sCcRzG8c/nvMs6xdQsC5EI+rG0SRhBtfQXtEX177W1NbkYBEFTJbUUhFHUUCBUCqlJQzdYXNBp8H1OntcifF2eN+fJDacSQupzZhna8u8GJsS2vOukbUebptf0we5E9cuL+zMkVTO5LCBLYuI26m9RkSHn+1cTofrZ2d7nx4irKrlOpFVLPOdVtTqjOvRoet7feSGzej2v+tDMqI6rk1Jty/2i3pleF4AlIplCIdmQq4TIkxu/vHCnY+dnCWm5t2G6+S0RSVQqw11HJ46zvH5seFdglojctFqnc11nRxHXHs0aHhaUd4/UD7vOUmXJvL+a3RWY319sISuR1Q2JLxna1BM7XSwXVQ9ExianFqp53dT95JqW0iVJVbZ0z/S+/myv7IhINrdresgv1I/pUb3gY3wYMAQNQ9AwBA1D0DAEDUPQMAQNQ9AwBA1D0DAEDUPQMAQNQ9AwBA1D0DAEDUPQMAQNQ9AwBE0436rxMTBXhCFoGIKGIWgYgoYhaBiChiFoGIKGIWgYgoYhaBiChiFoGIKGIWgYgoYhaBiChiFoPgFaEjjxhwSuZwAAAABJRU5ErkJggg==)

![After scrolling 60 pixels: the blue second block fills the clipped pane](data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMgAAABkCAMAAAD0WI85AAAB71BMVEUlY+v///8jXt4hWdMgVswUNn8QK2YRLm0WOosfVMcAAAAZRKEFDiIbSawiWtUeT7wLHkcYP5caRqUHEy0UN4IdTLYZQp0aRqYGDyQLHUUlYuglY+oZRKITMncWPI8jX+EeUcAVOIUTMnYWPI4gVcseUb8VOIQTNHwYQZogVcoVOIYUNX8iXNoeUL4TMngSMHIHFC4XPZINI1MNI1IgVMgSL3EHEiskYecVOYYGECccTLUdTrkeUcEJGTwPKF8kX+MIFTITM3oWOokKG0EWO4wFDB0bSa0gVssVN4MTM3kDBxECBQsSMXMeUL0hWdIAAQIaR6giWtYBBAkaRaUZQ58CBw8YQZsfU8QAAAEeUL8XPpMCBg0jXt8CBg8XPZEkYOQIFjUBAgQZQ6ACBAobR6kBAgUXPI8ZQ54BAwcdTroK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Request a vertical offset of 10000, then read the clamped position. Use `ui` and `pane` from the full program.

```csharp
ui.ScrollElementTo(pane, 0, 10000);
Console.WriteLine($"scrollTop: {ui.Layout.ScrollTop(pane)}");
```

```text
scrollTop: 60
```

### Scroll the whole viewport

This example scrolls the document instead of an inner container. Two 100-pixel blocks extend beyond a 100-pixel-tall viewport.

```csharp
using OneRender;
using OneRender.Image;
using OneRender.Interaction;

string html = """
<style>body { margin:0 } div { height:100px }</style>
<div id="first">First</div><div id="second">Second</div>
""";
var ui = InteractivePage.Create(html,
    new HtmlPageOptions(FontProviders.CreateDefault())
    { ViewportWidth = 200, ViewportHeight = 100 });
Console.WriteLine($"changed: {ui.ScrollViewportTo(0, 100)}");
Console.WriteLine($"viewport scrollTop: {ui.Page.Scroll.ViewportY}");
Console.WriteLine($"at (10,10): #{ui.ElementFromPoint(10, 10).GetAttribute("id")}");
```

```text
changed: True
viewport scrollTop: 100
at (10,10): #second
```

## Related recipes

- [Load local CSS and images for HTML to PDF in C#](https://recursiveai.org/onerender/cookbook/load-html-render-resources/)
- [Render a CSS grid to PNG in C#](https://recursiveai.org/onerender/cookbook/render-css-grid-to-image/)
- [Render responsive flexbox to PNG in C#](https://recursiveai.org/onerender/cookbook/render-css-flexbox-to-image/)

## The short answer

Create an `InteractivePage`, find the overflow container by its HTML id, and call `ScrollElementTo`. Repaint the page after the offset changes. The page’s scroll state is used both by painting and by `ElementFromPoint`, so hit tests use the visible, scrolled positions rather than the original content coordinates.

The full program saves two images. Before scrolling, the red first block fills the container. After a 60-pixel vertical offset, the blue second block fills it. A hit at (10,10) changes from `first` to `second`. No JavaScript or browser window is involved.

## Does overflow hidden prevent scrolling?

It clips the contents, but it is still a scroll container for OneRender’s programmatic API. That is why this example can call `ScrollElementTo` on a pane with `overflow:hidden`. The pane is 60 pixels tall and its two children are each 60 pixels tall, leaving 60 pixels of vertical travel.

[The overflow docs](https://github.com/recursive-ai-oss/onerender-csharp/blob/d9708ace1863cae70a1b0782b8363ed970e555bb/docs/architecture.md#overflow-and-scrolling) distinguish this from `overflow:clip`: clip is not scrollable. Choose hidden when you need a clipped panel controlled by your host application. Choose a scrollable overflow value when the host also intends to process ordinary scrolling input.

A CSS declaration alone does not drive a frame loop. The host decides when to change the offset and when to paint the next image. The example repaints into a fresh display list after the call, which keeps that sequence explicit.

## How do I get the scroll position of an element?

Read `ui.Layout.ScrollTop(pane)` after the call. The full program prints 60. The return value from `ScrollElementTo` answers a different question: whether the position changed. A false result can mean the element is not a scroll container or it was already at that offset; it is not an exception or a measurement.

Coordinates passed to `ElementFromPoint` are viewport coordinates. Do not subtract the scroll offset yourself. The example queries the same point before and after scrolling and receives different child ids. It also queries (10,70), below the pane’s clipped bottom edge. That result has no id and is not the offscreen child. The scroll state and clipping must agree for a click handler to reach the content the user actually sees.

## How do I scroll an element to the bottom?

Request an offset beyond the content height and let `ScrollElementTo` clamp it. The variation asks for 10000 pixels but `ScrollTop` still reports 60, the available travel in this panel. Repeating a request at the current clamped position need not produce another changed result.

For a whole-document viewport, the separate `ScrollViewportTo` API is the counterpart to a window scroll; `ScrollElementTo` targets a specific container. The [public scrolling methods](https://github.com/recursive-ai-oss/onerender-csharp/blob/d9708ace1863cae70a1b0782b8363ed970e555bb/src/OneRender/Interaction/InteractivePage.Scroll.cs) document their return values and clamping behavior.
