Recursive AI

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.

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

Terminal
dotnet add package RecursiveAI.OneHtml

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

C#
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, 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.

C#
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:

C#
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)

Get computed CSS styles in C# · 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 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.

All onehtml recipes