Scroll a clipped overflow container, repaint it, and hit-test the visible content. The HTML input is in the zip and shown below.
dotnet add package RecursiveAI.OneRender.ImageRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
Save this as input.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.
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");
scrolled.png writtenUnzip the example, then dotnet run.
The full program
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:
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.
Variations
Clamp to the bottom
Request a vertical offset of 10000, then read the clamped position. Use ui and pane from the full program.
ui.ScrollElementTo(pane, 0, 10000); Console.WriteLine($"scrollTop: {ui.Layout.ScrollTop(pane)}");
scrollTop: 60Scroll the whole viewport
This example scrolls the document instead of an inner container. Two 100-pixel blocks extend beyond a 100-pixel-tall viewport.
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")}");
changed: True viewport scrollTop: 100 at (10,10): #second
Related recipes
- Load local CSS and images for HTML to PDF in C#
- Render a CSS grid to PNG in C#
- Render responsive flexbox to PNG in C#
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 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 document their return values and clamping behavior.