---
title: "Get rectangles for a wrapped HTML inline element in C#"
url: https://recursiveai.org/onerender/cookbook/measure-wrapped-inline-element-rectangles/
description: "Get one viewport-relative rectangle per piece of a wrapped HTML span in C# with OneRender’s GetClientRects, instead of a single bounding box."
version: "1.4.0"
---

# Get rectangles for a wrapped HTML inline element 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/measure-wrapped-inline-element-rectangles/measure-wrapped-inline-element-rectangles.zip)

`GetClientRects` measures each piece of a wrapped `<span>` so a highlight can follow the text.

```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.

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

const string html = """
    <div style="width:100px">
      <span id=label>First Second Third</span>
    </div>
    """;
var options = new HtmlPageOptions(
    FontProviders.CreateDefault());
var page =
    InteractivePage.Create(html, options);
var label = page.Page.Document
    .GetElementById("label");
// Eight slots here; retry if the buffer fills.
Span<Rect> rects = stackalloc Rect[8];
int count = page.Layout
    .GetClientRects(label, rects);
foreach (var r in rects[..count])
{
    string pos = FormattableString.Invariant(
        $"({r.X:F1}, {r.Y:F1})");
    string size = FormattableString.Invariant(
        $"{r.Width:F1} × {r.Height:F1}");
    Console.WriteLine($"{pos} {size}");
}
```

Paste the code above into Program.cs of a new `dotnet new console` project and `dotnet run`, or unzip [the example](https://recursiveai.org/onerender/cookbook/measure-wrapped-inline-element-rectangles/measure-wrapped-inline-element-rectangles.zip) and `dotnet run`.

Prints two rectangles as `(x, y) width × height` in CSS pixels:

```text
(8.0, 8.0) 80.4 × 17.0
(8.0, 26.0) 35.5 × 17.0
```

The example uses the fonts from `RecursiveAI.OneRender.Image` to lay out the text. If the element has no layout box, as with `display: none`, the query writes zero rectangles.

## Variations

**Get one enclosing rectangle.** Use this in place of the rectangle buffer and loop when you want one outline around the whole span:

```csharp
var bounds = page.Layout.GetBoundingClientRect(label);
Console.WriteLine(FormattableString.Invariant($"{bounds.Width:F1} × {bounds.Height:F1}"));
```

## Related recipes

- [Find which element is at a point](https://recursiveai.org/onerender/cookbook/hit-testing-events/)
- [Scroll a rendered HTML container](https://recursiveai.org/onerender/cookbook/scroll-rendered-html-page/)

---

## The short answer

Lay out the HTML with `InteractivePage`, find the inline element, and call `page.Layout.GetClientRects(element, buffer)`. Each rectangle describes the border box of one laid-out piece. The first rectangle in this example covers “First Second” and the second covers “Third.” Read only the elements of the buffer up to the returned count.

## What’s the difference between GetClientRects and GetBoundingClientRect?

`GetClientRects` gives you the separate pieces in tree order. `GetBoundingClientRect` gives you the smallest rectangle that encloses the nonempty pieces. In the example, that larger rectangle extends past the end of “Third,” because it has to be wide enough for the first line. Use that rectangle to position a tooltip next to the whole span; use the separate rectangles when the blank area to the right of the second line shouldn’t be highlighted.

OneRender’s methods follow the distinction in [CSSOM View’s geometry APIs](https://drafts.csswg.org/cssom-view/#extension-to-the-element-interface). The rectangles describe CSS border boxes, not pixel outlines of the glyphs. Use the same layout width and fonts as the surface you’re annotating: both affect where the span breaks.

## What are client rectangles relative to?

The origin is the top-left of the viewport, not the top-left of the element or document. The coordinates account for scrolling, and the widths and positions can be fractional. If you draw an overlay in device pixels, use the same CSS-to-device scale you used for the page; don’t round each rectangle to whole CSS pixels first.

The buffer is supplied by the caller. If it fills, OneRender drops any remaining rectangles and returns the number it wrote, not the total. A result equal to the buffer length might be an exact fit or might be truncated. If you need every piece for arbitrary input, retry with a larger buffer when it fills.
