Recursive AI

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.

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

Terminal
dotnet add package RecursiveAI.OneUI

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

CSS
.badge { color: green; }

The example 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:

C#
public partial class Badge { }

Page.html:

HTML
<Badge />
<span class="badge">Outside</span>

Page.cs:

C#
public partial class Page { }

Program.cs:

C#
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, 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; }

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 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 also list the at-rules OneUI accepts; the CSS property list tells you which declarations the renderer supports.

All oneui recipes