---
title: "Scope CSS to an HTML component in C#"
url: https://recursiveai.org/oneui/cookbook/scope-css-to-component/
description: "Put a CSS file next to a OneUI HTML template to keep selectors local. Run a C# example that checks the rule against two elements with the same class."
version: "1.0.0"
---

# Scope CSS to an HTML component 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 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/scope-css-to-component/scope-css-to-component.zip)

Put `.badge` in `Badge.css` beside `Badge.html` to keep the rule local.

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

```css
.badge { color: green; }
```

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

The rule colors the badge in `Badge.html`, not the `.badge` in another template.

## The full program

Pair `Badge.html` with `Badge.cs` and `Page.html` with `Page.cs`: each template and partial class share a name and folder. The console app mounts both badges in a `ReferenceDocument`, an in-memory document, and queries the compiled selector. It doesn’t paint a window.

`Badge.html`:

```html
<span class="badge">In stock</span>
```

`Badge.cs`:

```csharp
public partial class Badge { }
```

`Page.html`:

```html
<Badge />
<span class="badge">Outside</span>
```

`Page.cs`:

```csharp
public partial class Page { }
```

`Program.cs`:

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

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

var sheet = doc.StyleSheets.Single();
Console.WriteLine(sheet.Css.Trim());
string selector = sheet.Css.Split('{')[0].Trim();
Console.WriteLine($"Elements with .badge: {doc.QueryAll(".badge").Count}");
Console.WriteLine($"Scoped matches: {doc.QueryAll(selector).Count}");
Console.WriteLine($"Styled element: {doc.Text(selector)}");
```

`dotnet run` prints:

```text
.badge[data-oui-09d96e18]{color:green;}
Elements with .badge: 2
Scoped matches: 1
Styled element: In stock
```

If a property isn’t in the [supported CSS list](https://recursiveai.org/oneui/docs/css-support/), the compiler reports `OUI0101`.

## Variations

To opt this selector out of scoping, replace `Badge.css` with the rule below. The same program now reports two selector matches:

```css
:global(.badge) { color: green; }
```

## Related recipes

- [Pass HTML into named component slots](https://recursiveai.org/oneui/cookbook/pass-html-content-to-component-slots/)
- [Render inline SVG in a component](https://recursiveai.org/oneui/cookbook/render-inline-svg-in-component/)

## The short answer

Name the files `Badge.html`, `Badge.css` and `Badge.cs`, and put them in the same folder. Use `.badge` in the stylesheet as you normally would. OneUI’s compiler adds an attribute selector to the rule and puts the matching attribute on the elements it generates for `Badge`. There’s no attribute for you to invent or copy into the template.

## How does scoped CSS keep styles from leaking to other components?

OneUI adds the marker to the last compound selector, the part that matches the target element. For the example’s single-class selector, the output becomes `.badge[data-oui-09d96e18]`. A `.badge` from `Page.html` has the right class but not the marker, so it doesn’t match the rule. This is selector rewriting, not a separate document tree. The rule’s direct match is local, but that does not mean [CSS inheritance](https://developer.mozilla.org/en-US/docs/Web/CSS/Guides/Cascade/Inheritance) stops at component boundaries.

Make the element you intend to style part of the template that owns the stylesheet. If a child component creates the element, put the rule in the child’s stylesheet, or style it deliberately with a shared rule. The generated marker is useful for reading the output, not for writing selectors in application code.

## How do you make a CSS rule global again?

Use `:global(...)` when you really want the selector to work outside the template. The variation above lets `.badge` match either badge. Use this for an intentional shared convention, not to reach into a child whose markup may change. The [scoped styles docs](https://recursiveai.org/oneui/docs/markup/#scoped-styles) also list the at-rules OneUI accepts; the [CSS property list](https://recursiveai.org/oneui/docs/css-support/) tells you which declarations the renderer supports.
