---
title: "Evaluate CSS :checked without changing HTML in C#"
url: https://recursiveai.org/onehtml/cookbook/evaluate-css-checked-state/
description: "Use FormStateOverrides in onehtml to evaluate a checked checkbox in C#, compare the computed label color, and leave the original HTML unchanged."
version: "1.6.0"
---

# Evaluate CSS :checked without changing HTML in C#

**onehtml** is a free, open-source HTML library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneHtml) · [GitHub](https://github.com/recursive-ai-oss/onehtml-csharp) · [Docs](https://recursiveai.org/onehtml/docs/) · [Download example (.zip)](https://recursiveai.org/onehtml/cookbook/evaluate-css-checked-state/evaluate-css-checked-state.zip)

Use `FormStateOverrides` to preview a checked box without editing the HTML.

```shell
dotnet add package RecursiveAI.OneHtml
```

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

```csharp
using RecursiveAI.OneHtml;
using RecursiveAI.OneHtml.Css;
using RecursiveAI.OneHtml.Selectors;

var doc = Document.Parse("""
    <style>
    label { color: red }
    :checked + label { color: green }
    </style>
    <input id=opt type=checkbox>
    <label for=opt>Send updates</label>
    """);
var box = doc.GetElementById("opt");
var label = doc.Root.QuerySelector("label");
var state = FormStateOverrides.Empty
    .WithChecked(box, true);
var context = StyleContext.Default
    .WithFormState(state);
var styles = DocumentStyles.Create(
    doc, context);
Console.WriteLine(styles.GetStyle(label)
    .GetComputedValue("color"));
```

Prints the computed color of the checkbox’s label:

```text
rgb(0, 128, 0)
```

Unzip [the example](https://recursiveai.org/onehtml/cookbook/evaluate-css-checked-state/evaluate-css-checked-state.zip), then `dotnet run`.

## The full program

The download compares both styles using the same parsed document and checks that the source checkbox still has no `checked` attribute.

```csharp
using RecursiveAI.OneHtml;
using RecursiveAI.OneHtml.Css;
using RecursiveAI.OneHtml.Selectors;

var doc = Document.Parse("""
    <style>
    label { color: red }
    :checked + label { color: green }
    </style>
    <input id=opt type=checkbox>
    <label for=opt>Send updates</label>
    """);
var box = doc.GetElementById("opt");
var label = doc.Root.QuerySelector("label");
var state = FormStateOverrides.Empty
    .WithChecked(box, true);
var context = StyleContext.Default
    .WithFormState(state);
var styles = DocumentStyles.Create(
    doc, context);
Console.WriteLine(styles.GetStyle(label)
    .GetComputedValue("color"));

var original = DocumentStyles.Create(doc);
Console.WriteLine("Without override: " + original.GetStyle(label)
    .GetComputedValue("color"));
Console.WriteLine($"Has checked attribute: {box.HasAttribute("checked")}");
```

`dotnet run` prints:

```text
rgb(0, 128, 0)
Without override: rgb(255, 0, 0)
Has checked attribute: False
```

Use nodes from the document you’re styling. `WithChecked` throws `ArgumentException` if you pass a missing element or add a node from another document to the same set of overrides.

## Variations

**Preview the unticked state.** Append this to the snippet at the top. `WithChecked` returns a new set of overrides, so the earlier result stays green:

```csharp
var uncheckedState = state.WithChecked(box, false);
var uncheckedStyles = DocumentStyles.Create(doc,
    context.WithFormState(uncheckedState));
Console.WriteLine(uncheckedStyles.GetStyle(label)
    .GetComputedValue("color"));
```

```text
rgb(255, 0, 0)
```

## Related recipes

[Get computed CSS styles in C#](https://recursiveai.org/onehtml/cookbook/computed-style-csharp/) · [Read HTML form field values](https://recursiveai.org/onehtml/cookbook/read-html-form-field-values/)

---

## The short answer

Call `FormStateOverrides.Empty.WithChecked(box, true)` on the checkbox node, pass the returned state to `StyleContext.WithFormState`, then compute styles with that context. The same document can supply both an unchanged view and a checked-state view. Read the color of the label, not the input, because the rule targets the next sibling of the checked control.

## How do you change a label’s color with CSS `:checked`?

Use `:checked + label` when the label immediately follows the checkbox under the same parent. The `+` means next element sibling, so merely adding `for` to a label elsewhere won’t make this selector match. In the example, the plain `label` rule supplies red; with the override in the style context, the more specific checked-state rule supplies green. If a preview doesn’t change, inspect both the position of the label and which CSS declaration wins for `color`.

## What’s the difference between `:checked` and the HTML `checked` attribute?

On a checkbox, the attribute declares the default in markup; [`:checked`](https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Selectors/:checked) tests the control’s current state. An unchecked preview is not the same as removing an override: passing `false` requests the unchecked state even when the markup has a checked default. Using a context with no overrides goes back to evaluating the markup.

Onehtml applies checked-state overrides literally. For example, if you’re modeling the user choosing a different radio button, override the old and new radios together. Setting one radio to checked doesn’t automatically uncheck the rest of its group. This is a CSS preview, not a simulated click or a form submission.

The accessibility tree doesn’t read form-state overrides. If it needs to reflect the new state too, build it from a document whose attributes reflect that state. The style snapshot described here is useful precisely when the original HTML should remain unchanged.
