---
title: "Scroll a list item into view in C#"
url: https://recursiveai.org/oneui/cookbook/scroll-to-an-item-programmatically/
description: "Scroll a list item into view in C# with OneUI. Bind an ElementRef after rendering, choose an alignment, and read back the scroll position."
version: "1.2.1"
---

# Scroll a list item into view 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.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneUI) · [GitHub](https://github.com/recursive-ai-oss/oneui-csharp) · [Docs](https://recursiveai.org/oneui/docs/) · [Download example (.zip)](https://recursiveai.org/oneui/cookbook/scroll-to-an-item-programmatically/scroll-to-an-item-programmatically.zip)

Set `ref="{Target}"` on the row. Once it renders, scroll it into view.

```shell
dotnet add package RecursiveAI.OneUI
dotnet add package RecursiveAI.OneApp
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
Target.ScrollIntoView(
    ScrollAlignment.Nearest);
```

[The example](https://recursiveai.org/oneui/cookbook/scroll-to-an-item-programmatically/scroll-to-an-item-programmatically.zip) is the full program below: unzip it and `dotnet run`.

The sample moves the list from `0 px` to `60 px`, just enough to show Reports.

## The full program

Save this as `Results.html` next to `Example.cs`. OneUI’s source generator pairs it with the partial `Results` class and binds `List` and `Target`. The zip pins `RecursiveAI.OneUI` at `1.2.1` and `RecursiveAI.OneApp` at `1.2.0` as ordinary `PackageReference` entries. OneApp lays out the list without opening a visible window.

```html
<ul ref="{List}" style="height: 60px; overflow: auto; margin: 0; padding: 0">
  <li style="height: 30px">Invoices</li>
  <li style="height: 30px">Receipts</li>
  <li style="height: 30px">Statements</li>
  <li ref="{Target}" style="height: 30px">Reports</li>
  <li style="height: 30px">Exports</li>
</ul>
```

```csharp
using OneApp;
using OnePlatform;
using OneUI.Components;

var results = new Results();
using var app = AppHost.Create(new AppOptions(results)
{
    Backend = PlatformBackendKind.Headless,
    UseGpu = false,
    Width = 400,
    Height = 200
});
app.Step(0);

Console.WriteLine($"Before: {results.List.ScrollTop} px");
results.Reveal();
Console.WriteLine($"After: {results.List.ScrollTop} px");

public partial class Results
{
    public ElementRef List { get; } = new();
    ElementRef Target { get; } = new();

    public void Reveal() =>
        Target.ScrollIntoView(ScrollAlignment.Nearest);
}
```

```text
Before: 0 px
After: 60 px
```

In a windowed app, wire a button’s `on:click="Reveal"` in `Results.html` to run `Reveal()` when the user clicks it.

An unbound reference throws `InvalidOperationException`; a bound row with `display: none` has no box to scroll to.

## Variations

Replace the call in `Reveal` with this to center Reports instead. The `After` line becomes `75 px`.

```csharp
Target.ScrollIntoView(ScrollAlignment.Center);
```

## Related recipes

- [Render a list in a component template](https://recursiveai.org/oneui/cookbook/render-list-in-component-template/)
- [Focus an input after a component updates](https://recursiveai.org/oneui/cookbook/focus-input-after-component-update/)

## The short answer

Use a OneUI `ElementRef` on the list item, not the list itself. After the item exists, `ScrollIntoView(ScrollAlignment.Nearest)` moves its scroll containers only as far as needed to reveal it. If you’ve just set state to add the item and your next line uses its reference, call `UiContext.Commit()` first so the new `ref` is bound.

## How do you scroll an item to the center of a list?

Use `ScrollAlignment.Center` for vertical alignment in a normal top-to-bottom list. In the example, Reports starts 90 CSS pixels from the top and is 30 pixels tall, so its center starts at 105. The list shows 60 pixels, so a scroll offset of 75 puts the row’s center 30 pixels from the visible top. That calculation doesn’t have to live in your component: OneApp uses the measured boxes and keeps the result within the scrollable range. A row near the end may not land exactly in the center because there isn’t enough content after it.

## Why isn’t `ScrollIntoView` scrolling my list?

Check the box around the list first. A list that grows to fit all its rows has no overflow to scroll. Give it a constrained height and `overflow: auto`, as in the example. Then check the row: if the conditional markup hasn’t created it, the reference won’t be bound. If the row exists but has no layout box, the request does nothing. With `Nearest`, a row that’s already visible also doesn’t need to move.

For nested scroll containers, scrolling starts with the innermost one and then works outward using the row’s updated position. Reading `ScrollTop` on the outer list alone won’t tell you whether an inner one moved. `ScrollIntoView` does not ask OneUI to scroll the document viewport itself; that is up to the backend. See the [scrolling rules](https://recursiveai.org/oneui/docs/change-sets/#version-3-programmatic-scrolling) for alignment and behavior details.
