Recursive AI

Render CSS multi-column layout to PNG 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.

CSS multi-column layout flows content down one column and into the next. Render it to a PNG and inspect where each block lands.

Terminal
dotnet add package RecursiveAI.OneRender.Image

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

The download includes this complete input.html:

HTML
<style>
body { margin:0 }
main { width:320px; column-count:2; column-gap:20px }
div { height:40px; break-inside:avoid; background:#dbeafe }
</style>
<main><div id="a">First</div><div id="b">Second</div>
<div id="c">Third</div><div id="d">Fourth</div></main>
C#
using OneRender.Image;
 
// input.html is shown above and in the zip.
var image = HtmlRender.ToImage(
    File.ReadAllText("input.html"),
    new HtmlRenderOptions {
        Width = 320, Height = 100 });
File.WriteAllBytes("columns.png", image.ToPng());
Console.WriteLine("Wrote columns.png");
Text
Wrote columns.png

Unzip the example, then dotnet run.

The full program

Installing RecursiveAI.OneRender.Image also provides the OneRender.Interaction and OneRender.Painting namespaces used below; no additional package is needed.

C#
using OneRender;
using OneRender.Image;
using OneRender.Interaction;
using OneRender.Painting;
 
string html = File.ReadAllText("input.html");
var page = InteractivePage.Create(html,
    new HtmlPageOptions(FontProviders.CreateDefault())
    { ViewportWidth = 320, ViewportHeight = 100 });
foreach (string id in new[] { "a", "b", "c", "d" })
{
    var rect = page.Layout.GetBoundingClientRect(
        page.Page.Document.GetElementById(id));
    Console.WriteLine($"#{id}: {rect.X},{rect.Y} {rect.Width}x{rect.Height}");
}
var list = new DisplayList();
page.Page.Paint(list);
var image = DisplayListRasterizer.Render(list, 320, 100);
File.WriteAllBytes("columns.png", image.ToPng());
Console.WriteLine($"columns.png: {image.Width}x{image.Height}");
Text
#a: 0,0 150x40
#b: 0,40 150x40
#c: 170,0 150x40
#d: 170,40 150x40
columns.png: 320x100

The first two blocks occupy the left column; the other two occupy the right. Each column is 150 pixels wide, with a 20-pixel gap. The full program prints each block’s bounding rectangle before writing the PNG. A missing input.html causes File.ReadAllText to throw before rendering.

Variations

For a preferred width rather than a fixed count, replace the main rule in input.html and run again:

CSS
main { width:320px; column-width:100px; column-gap:20px }

This still produces two 150-pixel columns and the same printed rectangles: the remaining width is shared between the columns.

The short answer

Put column-count or column-width on an HTML block container and pass the HTML to HtmlRender.ToImage. Set the output width and height in HtmlRenderOptions, then save the result with ToPng. For layout inspection as well as pixels, create an InteractivePage, read the bounding rectangles and paint its display list, as the full program does.

The sample deliberately uses four fixed-height blocks rather than a long article. Their IDs and printed positions make the direction of flow visible: First and Second are stacked in the left column; Third and Fourth are stacked in the right.

How can I create a multi-column layout in CSS?

Set column-count:2 on the container to request two columns, then choose column-gap to control the space between them. The MDN multi-column guide describes this as dividing content across columns, with properties for count, preferred width, gaps and dividing rules. It is a flowing layout, not a separate child element for each column.

In OneRender, children are first laid out as a normal block flow at the resolved column width. The fragmenter then cuts that flow into columns. break-inside:avoid on each block in the example keeps a block together. Without a fixed container height, the default column-fill:balance balances the flow across the requested columns.

The PNG dimensions do not set the column container’s CSS height. Here the output is 320 by 100 pixels, while the content uses 80 pixels vertically.

How do you make a two-column layout?

For two equal columns, use a definite container width and a count of two. Subtract one gap and divide the remaining space by two. This sample’s 320-pixel container minus its 20-pixel gap leaves 300 pixels: each column is 150 pixels wide. The second column starts at X=170, which is what the program prints.

Use column-width instead when the number should depend on the available width. It is a preferred width, not an exact final width. OneRender computes how many columns fit and divides the available space between them; if you also supply column-count, that count caps the result. The variation requests 100 pixels but resolves to two columns of 150 pixels each.

For independent panels or cards whose row positions should align, use Grid or Flexbox instead. Multi-column flow consumes content in reading order down one column before continuing into another; it does not assign a fixed panel to each column.

How does column-count affect CSS layout?

A count controls the used number of columns, but content breaks and height still control how they fill. With a definite height, overflowing content can create extra columns in the inline direction. With column-fill:auto and automatic height, OneRender keeps content in one column unless a forced break splits it. Those settings can explain a result that appears to ignore your requested count.

A direct in-flow block child with column-span:all spans the container and splits the flow into separately balanced rows. Spanners nested more deeply do not have that effect in this release. Column rules are painted between existing adjacent columns and stop at a spanner. The architecture documentation describes these behaviors, including overflow and column-width resolution, for the pinned package used by the download.

All onerender recipes