---
title: "Toggle a CSS class from C# component state"
url: https://recursiveai.org/oneui/cookbook/toggle-css-class-from-component-state/
description: "Bind a CSS class to a C# boolean with class:active in OneUI. Toggle the active class on click without replacing the element or its other classes."
version: "1.1.0"
---

# Toggle a CSS class from C# component state

**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, 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/toggle-css-class-from-component-state/toggle-css-class-from-component-state.zip)

Bind the button’s `active` class to a C# boolean. The button keeps its base `chip` class.

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

Complete `Filter.html`: the click handler in `Filter.cs` flips `IsActive`, which controls the `active` class.

```html
<button class="chip"
        class:active="{IsActive}"
        on:click="Toggle">In stock</button>
```

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

Prints `chip`, `chip active`, then `chip` as the same button is clicked twice.

## The full program

To build by hand, run `dotnet new console` with the .NET 10 SDK, then run the install command above. Save `Filter.html`, `Filter.cs`, and `Filter.css` next to your project file. OneUI pairs the template with the class and implements the partial state property. The download pins OneUI 1.1.0.

`Filter.cs` flips `IsActive` when the button’s click handler runs:

```csharp
using OneUI.Components;

public partial class Filter
{
    [State]
    public partial bool IsActive { get; set; }

    private void Toggle() =>
        IsActive = !IsActive;
}
```

`Filter.css` defines the base style and the style that applies only when `active` is present:

```css
.chip { padding: 4px 12px; }
.active { font-weight: bold; }
```

Replace the starter `Program.cs` with the code below. It clicks the button through an in-memory document and prints the node ID and class list each time:

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

var dom = new ReferenceDocument();
var ui = new UiContext(dom);
dom.Context = ui;
ui.Mount(new Filter());

PrintClasses();
dom.Click("button");
PrintClasses();
dom.Click("button");
PrintClasses();

void PrintClasses()
{
    var button = dom.Require("button");
    var classes = button.GetAttribute("class");
    Console.WriteLine(
        $"Node {button.Id}: {classes}");
}
```

```text
Node 2: chip
Node 2: chip active
Node 2: chip
```

The node ID stays 2: neither click recreates the button.

If the binding names a property that doesn’t exist, the markup generator reports it at build time; see [markup diagnostics](https://recursiveai.org/oneui/docs/diagnostics/).

## Variations

Invert the condition when the class should be present by default and removed on the first click:

```html
<button class="chip"
        class:active="{!IsActive}"
        on:click="Toggle">In stock</button>
```

## Related recipes

- [Scope CSS to a component](https://recursiveai.org/oneui/cookbook/scope-css-to-component/)
- [Build a reactive HTML component with C# state](https://recursiveai.org/oneui/cookbook/create-html-component-with-csharp-state/)

## The short answer

Write `class:active="{IsActive}"` on the element and declare `IsActive` as a boolean `[State]` property in the matching C# component. OneUI adds `active` when the expression becomes true and removes it when it becomes false. Other classes on the element stay in place.

## How do you conditionally add a CSS class without replacing other classes?

Put classes that always apply in the normal `class` attribute. In `Filter.html`, `class="chip"` sets the baseline and `class:active` controls just the extra class. The click handler only changes state; it doesn’t query the document or construct a class string. The class binding produces an `AddClass` or `RemoveClass` change for that node. Setting the whole `class` attribute instead would replace the class list.

A single class binding is useful when base styles and conditional styles have different jobs. Here the padding is the same whether the filter is selected or not, and only the font weight changes. Write the stylesheet against `.chip` and `.active` separately. The in-memory document shows the class list; a rendering backend applies the corresponding styling.

## Should you toggle a class or conditionally render a new element?

Keep one element and toggle its class when only its appearance changes. In the output, node 2 remains node 2 across both clicks; the binding changes a class, not the tree. That also means the element and its click listener don’t need to be recreated to switch the style. Use a conditional template when the content should be absent from the document, such as a panel that has no role until a user opens it. A class binding by itself doesn’t remove content: any hiding behavior comes from the CSS you attach to the class.
