Recursive AI

Render CSS text-overflow: ellipsis 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.

Set a width, keep the label on one line, and hide its overflow to paint an ellipsis (…).

Terminal
dotnet add package RecursiveAI.OneRender.Image

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

C#
using OneRender.Image;
 
var image = HtmlRender.ToImage("""
    <style>
      p { width: 160px; white-space: nowrap;
          overflow: hidden;
          text-overflow: ellipsis }
    </style>
    <p>Quarterly budget
       reconciliation report</p>
    """, new HtmlRenderOptions
    { Width = 240, Height = 70 });
File.WriteAllBytes(
    "ellipsis.png", image.ToPng());
Console.WriteLine("Wrote ellipsis.png");

Paste the code above into Program.cs of a new dotnet new console project and dotnet run, or unzip the example and dotnet run.

Text
Wrote ellipsis.png

A single line reading Quarterly budget reco followed by an ellipsis.

If the line wraps or still has room for all its text, there’s nothing at the line’s end for text-overflow to replace.

Variations

Use a custom marker. Replace text-overflow: ellipsis with this declaration in the same rule:

CSS
text-overflow: " [more]";

A single line reading Quarterly budget followed by the custom marker [more].

The short answer

Give the text a block container with a width, set white-space: nowrap so a long label stays on one line, then combine overflow: hidden and text-overflow: ellipsis. Pass that HTML to HtmlRender.ToImage and save the PNG. The paragraph retains its full text in the source HTML; OneRender chooses what fits when it paints the line, so you don’t need to estimate a character count in C#.

Why isn’t CSS text-overflow: ellipsis working?

Start by checking the element that owns the line. The CSS property affects overflowing inline content in a block container. A span in the middle of a paragraph isn’t a block container simply because you assign it a width. Put the rule on the surrounding paragraph or another block container that actually limits the line. With overflow: visible, the text is free to extend past the edge, so there’s no ellipsis. And with normal wrapping, text can move to another line instead of crossing the edge at all.

The width of the text box matters, not just the width of the final PNG. If the label is in a flex row, check whether the item is allowed to shrink; the automatic minimum size of a flex item can keep the item wider than the space you intended to give it. Inspect the width on the actual block before changing the text. The example uses a fixed width so the effect doesn’t depend on the viewport width alone.

Does text-overflow: ellipsis remove text from the rendered PDF?

In OneRender, the cut text is neither drawn nor extracted from a PDF. The markup is unchanged, but it doesn’t follow that the hidden part will be there when someone copies text out of the PDF. If the complete label matters in print, use a print layout that wraps the text or gives it enough width. OneRender reserves room for the marker and cuts at whole grapheme clusters, so characters that combine into one visible unit aren’t split at the cut. The marker is at the inline end (left for direction: rtl), and the two-value text-overflow form isn’t supported. See the 1.5.0 changelog for the painting rules.

All onerender recipes