Recursive AI

Get rendered UI element bounds in C#

OneUI is a free, open-source .NET library from Recursive AI for writing UI components in HTML, CSS, and C# that run in native windows on desktops and phones, or in a browser via WebAssembly, with OneApp. MIT or Apache-2.0.

Add ref="{Panel}" to the element, then call Panel.GetBoundingClientRect() after layout.

Terminal
dotnet add package RecursiveAI.OneUI
dotnet add package RecursiveAI.OneApp

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

C#
var box = Panel.GetBoundingClientRect();

The example is the full program below: unzip it and dotnet run.

The sample measures a 120 × 48 CSS-pixel panel at (20, 10).

The full program

Save StatusCard.html next to Example.cs. OneUI’s source generator pairs the template with the partial StatusCard class and binds Panel. The zip uses ordinary PackageReference entries for RecursiveAI.OneUI 1.2.1 and RecursiveAI.OneApp 1.2.0. OneApp handles the layout; Step(0) runs the first frame without opening a visible window.

HTML
<div style="padding: 10px 20px">
  <div ref="{Panel}" style="width: 120px; height: 48px">
    Ready
  </div>
</div>
C#
using OneApp;
using OnePlatform;
using OneUI.Components;
 
var card = new StatusCard();
using var app = AppHost.Create(new AppOptions(card)
{
    Backend = PlatformBackendKind.Headless,
    UseGpu = false,
    Width = 400,
    Height = 200
});
app.Step(0);
 
if (card.Panel.GetBoundingClientRect() is { } box)
{
    Console.WriteLine($"X={box.X}, Y={box.Y}");
    Console.WriteLine($"{box.Width} x {box.Height} CSS px");
}
else
{
    Console.WriteLine("No layout box");
}
 
public partial class StatusCard
{
    public ElementRef Panel { get; } = new();
}
Text
X=20, Y=10
120 x 48 CSS px

GetBoundingClientRect() returns null for a bound element with no layout box. An unbound ElementRef or a backend that doesn’t answer layout queries throws InvalidOperationException.

Variations

Replace the first Console.WriteLine inside the if to get the far edges instead of the origin. It prints Right=140, Bottom=58.

C#
Console.WriteLine($"Right={box.Right}, Bottom={box.Bottom}");

The short answer

Bind ref="{Panel}" to the element you want to measure. On the UI thread, call Panel.GetBoundingClientRect() after rendering and check for null. OneApp computes layout and returns a DomRect in CSS pixels. OneUI’s ReferenceDocument, an in-memory document for testing without a window, only reports boxes you supply.

Are the bounds relative to the viewport or the parent?

The rectangle is relative to the viewport. X and Y are the left and top of the rendered border box: the box includes padding and borders, but not margins. In the sample, the parent’s padding places the child at (20, 10), even though the child doesn’t set a position. If you want the offset from another element’s border box, query both elements in the same viewport and subtract their left and top coordinates. That gives you a difference between the two box origins, not necessarily a CSS left or top value. Since scrolling can move the box within the viewport without changing its size, measure again when you need its current position.

Why does GetBoundingClientRect() return no bounds?

A hidden element with display: none has no layout box, even if the reference is bound. A conditional branch that removes the element altogether can leave its reference unbound instead. Check IsBound if the element may not exist. The query itself commits pending updates before measuring, but if this update creates the referenced element, call UiContext.Commit() first so there’s a bound reference to make the call on. Make the query from a handler or other UI-thread code, not from a binding expression or while the backend is applying changes. See layout and focus queries for details.

All oneui recipes