---
title: "Render inline SVG in a C# UI component"
url: https://recursiveai.org/oneui/cookbook/render-inline-svg-in-component/
description: "Put an SVG shape in a OneUI HTML template, bind its fill to C# state, and run a console example that checks the SVG namespace and updated fill."
version: "1.0.0"
---

# Render inline SVG in a C# UI component

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

[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/render-inline-svg-in-component/render-inline-svg-in-component.zip)

Put the vector shape in `StatusDot.html` and bind its fill color.

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

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

```html
<svg viewBox="0 0 18 18" aria-hidden="true">
  <circle cx="9" cy="9" r="8" fill="{Fill}" />
</svg>
```

[The example](https://recursiveai.org/oneui/cookbook/render-inline-svg-in-component/render-inline-svg-in-component.zip) is the full program below: unzip it and `dotnet run`.

`{Fill}` sets the circle’s fill from C#. OneUI creates the shape in the SVG namespace; you don’t need to add `xmlns` to the template.

## The full program

Put `StatusDot.html` and its matching partial class file, `StatusDot.cs`, in the same folder. OneUI generates the implementation of `Fill` and the code that updates the circle. The console sample uses `ReferenceDocument`, an in-memory host that doesn’t open a window. For pixels on screen, mount the component in a [OneApp](https://recursiveai.org/oneapp/) window or browser host.

`StatusDot.cs`:

```csharp
using OneUI.Components;

public partial class StatusDot
{
    [State]
    public partial string Fill { get; set; }
    public StatusDot() => Fill = "gray";
}
```

`Program.cs`:

```csharp
using OneUI.Components;
using OneUI.Components.Reference;

var doc = new ReferenceDocument();
var ui = new UiContext(doc);
doc.Context = ui;
var dot = new StatusDot();
ui.Mount(dot);

var circle = doc.Require("circle");
Console.WriteLine($"SVG namespace: {circle.Namespace}");
Console.WriteLine($"Before: {circle.GetAttribute("fill")}");
dot.Fill = "green";
Console.WriteLine($"After: {circle.GetAttribute("fill")}");
```

`dotnet run` prints:

```text
SVG namespace: http://www.w3.org/2000/svg
Before: gray
After: green
```

SVG names are case-sensitive: for example, `viewbox` instead of `viewBox` produces the build error [`OUI0011`](https://recursiveai.org/oneui/docs/diagnostics/).

## Variations

For an outlined dot, replace the `<circle>` with this one. The same state now controls the outline, not the interior. Read `stroke` instead of `fill` in the console app to see the change:

```html
<circle cx="9" cy="9" r="8"
        fill="none" stroke="{Fill}"
        stroke-width="2" />
```

## Related recipes

- [Scope CSS to an HTML component](https://recursiveai.org/oneui/cookbook/scope-css-to-component/)
- [Build a UI component without HTML templates](https://recursiveai.org/oneui/cookbook/build-ui-component-without-markup/)

## The short answer

Write `<svg>` directly in your OneUI template, put the shapes inside it and bind a supported attribute such as `fill="{Fill}"`. OneUI recognizes the SVG subtree and creates namespaced nodes; changes to the C# state update the attribute on the existing shape. There’s no image file to reload when the color changes.

## Do you need `xmlns` for inline SVG?

Not in a OneUI template. The `<svg>` tag tells the compiler to create SVG nodes. It accepts an explicit `xmlns` but doesn’t use it to choose the namespace. The equivalent HTML element and SVG element can have different rules, so the compiler checks this subtree against its SVG element and attribute list. Keep the casing of names like `viewBox` and `linearGradient` when you paste a design from an SVG file.

## How do you change inline SVG color from C#?

Bind `fill` for the interior of a shape or `stroke` for the outline, as in the two examples above. Change the corresponding `[State]` property when the status changes. Set the presentation attributes on the shapes rather than relying on a selector inside the SVG: the OneUI docs note that page CSS rules don’t reach inside inline SVG in the native renderer. A shape using `currentColor` can follow the SVG root’s CSS `color`.

OneUI accepts paths, basic shapes, gradients and a [documented set of other SVG elements](https://recursiveai.org/oneui/docs/markup/#svg). It rejects constructs such as SVG text, filters and animation, even if they work in a standalone SVG viewer. If the icon conveys a status, pair it with a visible HTML label rather than using the color alone; the sample dot is marked `aria-hidden` because it is decorative.
