Render grid rows and columns to an image, then read their cell coordinates. 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 } main { display:grid; grid-template-columns:100px 1fr; grid-auto-rows:40px; gap:10px } div { background:#2563eb; color:white } </style><main><div id="a">A</div><div id="b">B</div> <div id="c">C</div><div id="d">D</div></main>
using OneRender.Image; var image = HtmlRender.ToImage( File.ReadAllText("input.html"), new HtmlRenderOptions { Width = 320, Height = 120 }); File.WriteAllBytes("grid.png", image.ToPng()); Console.WriteLine("grid.png: 320x120");
grid.png: 320x120Unzip 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"); const int width = 320; var ui = InteractivePage.Create(html, new HtmlPageOptions(FontProviders.CreateDefault()) { ViewportWidth = width, ViewportHeight = 120 }); foreach (string id in new[] { "a", "b", "c" }) { var rect = ui.Layout.GetBoundingClientRect( ui.Page.Document.GetElementById(id)); Console.WriteLine($"{width} #{id}: {rect.X},{rect.Y} {rect.Width}x{rect.Height}"); } var list = new DisplayList(); ui.Page.Paint(list); var image = DisplayListRasterizer.Render(list, width, 120); File.WriteAllBytes($"grid-{width}.png", image.ToPng()); Console.WriteLine($"grid-{width}.png: {image.Width}x{image.Height}");
dotnet run prints:
320 #a: 0,0 100x40 320 #b: 110,0 210x40 320 #c: 0,50 100x40 grid-320.png: 320x120
Variations
Use equal columns
Replace the HTML-loading line in the full program:
string html = File.ReadAllText("input.html") .Replace("100px 1fr", "1fr 1fr");
The full program then prints:
320 #a: 0,0 155x40 320 #b: 165,0 155x40 320 #c: 0,50 155x40 grid-320.png: 320x120
Related recipes
- Load local CSS and images for HTML to PDF in C#
- Render responsive flexbox to PNG in C#
- Scroll a rendered HTML container in C#
The short answer
Pass HTML containing display:grid to HtmlRender.ToImage, choose the viewport, and save the returned image with ToPng(). Use InteractivePage when you also need cell geometry: its layout queries return each element’s bounding rectangle, and its underlying page paints the same layout to a display list for rasterization.
The example uses one fixed column and one flexible column, with automatic rows. It needs no JavaScript, network resources or input images. The blue cells are CSS backgrounds. The packaged default fonts render their labels.
How do I render a CSS grid to PNG in C#?
Use the snippet at the top for file output only. Use the full program to inspect layout before saving. The latter creates a page at 320 by 120 CSS pixels, reads the rectangles for A, B and C, paints a display list and rasterizes it at the same dimensions. Matching those dimensions avoids confusing the layout viewport with the output canvas.
This is a render of supplied HTML, not a capture of a running website. Generate the markup before the call. If a grid depends on JavaScript to insert items or change classes, supply the resulting markup yourself. For external images or stylesheets, provide resource bytes explicitly with the resource-loading recipe.
How does grid-template-columns work?
In this example, 100px 1fr fixes the first column at 100 pixels and gives the remaining space to the second column after the gap. At a container width of 320 pixels with a 10-pixel gap, that leaves 210 pixels. The printed rectangles show A at x=0 with width 100, and B at x=110 with width 210. C returns to x=0 on the next row.
The equal-column variation replaces that definition with 1fr 1fr. The same available space is divided into two 155-pixel columns, with the second starting at x=165. That result comes from changing the CSS and laying it out again, not resizing a finished bitmap.
OneRender documents line placement, spans, negative lines, named areas and automatic placement in its grid architecture.
How do I add a gap to a CSS grid?
Set gap on the grid container. Here it separates both columns and rows. A cell is 40 pixels tall and the next row begins at y=50: the extra 10 pixels are the row gap. It is not padding around the outer boundary. Keep body margin explicit when you want coordinates to start at the image’s top-left corner.
The renderer’s supported grid behavior is not every possible CSS layout. At this pin, the documentation says inline-axis baseline falls back to start, last-baseline container baselines are not produced, and justify-items:legacy computes to normal. It also lists masonry, fragmentation across pages and non-horizontal writing modes as unimplemented. For the PNG task here, use horizontal rows and ordinary tracks; do not infer paged grid support from a successful image render.
MDN’s grid guide explains the CSS concepts independently of the renderer.