Recursive AI

Inspect CSS ::before and ::after styles in C#

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

Read styles for CSS-generated content without running a browser.

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;
 
var doc = Document.Parse("""
    <style>
    p::before { content: '['; }
    p::after { content: ']'; }
    </style>
    <p>Draft</p>
    """);
var styles = DocumentStyles.Create(doc);
var p = doc.Root.QuerySelector("p");
var before = styles.GetPseudoStyle(p,
    PseudoElementKind.Before);
var after = styles.GetPseudoStyle(p,
    PseudoElementKind.After);
Console.WriteLine(
    before?.GetComputedValue("content"));
Console.WriteLine(
    after?.GetComputedValue("content"));

Unzip the example, then dotnet run.

Prints the computed content for ::before, then ::after.

Text
'['
']'

The full program

The download compares the paragraph’s own color with both pseudo-elements’ colors. The more specific #draft::before rule sets red, while ::after inherits navy. If no rule styles the requested pseudo-element, GetPseudoStyle returns null for the unstyled ::before or ::after.

C#
using RecursiveAI.OneHtml;
using RecursiveAI.OneHtml.Css;
 
var doc = Document.Parse("""
    <style>
    .status { color: navy; }
    .status::before { content: "["; color: gray; }
    #draft::before { color: red; }
    .status::after { content: "]"; }
    </style>
    <p id="draft" class="status">Draft</p>
    <p id="plain">Plain text</p>
    """);
 
var styles = DocumentStyles.Create(doc);
var draft = doc.Root.QuerySelector("#draft");
Console.WriteLine("Element: " + styles.GetStyle(draft).GetComputedValue("color"));
Show("::before", PseudoElementKind.Before);
Show("::after", PseudoElementKind.After);
 
var plain = doc.Root.QuerySelector("#plain");
var unstyled = styles.GetPseudoStyle(plain, PseudoElementKind.Before);
Console.WriteLine("Plain ::before: " + (unstyled is null ? "no matching style" : "styled"));
 
void Show(string label, PseudoElementKind kind)
{
    var pseudo = styles.GetPseudoStyle(draft, kind);
    if (pseudo is null)
    {
        Console.WriteLine($"{label}: no matching style");
        return;
    }
 
    Console.WriteLine($"{label}: {pseudo.GetComputedValue("content")}, " +
        pseudo.GetComputedValue("color"));
}

dotnet run prints:

Text
Element: rgb(0, 0, 128)
::before: "[", rgb(255, 0, 0)
::after: "]", rgb(0, 0, 128)
Plain ::before: no matching style

Variations

Distinguish an absent style from content: none

Use this markup instead. The ::before style exists, but its computed content is none, so it doesn’t generate a box. The ::after lookup returns null.

C#
// Replace the short snippet's HTML with:
var doc = Document.Parse("""
    <style>
    p::before { content: none; color: red; }
    </style>
    <p>Draft</p>
    """);

The second line is blank because the ::after lookup returns null.

Text
none
 

The short answer

Create DocumentStyles, find the originating element, and pass it to GetPseudoStyle with PseudoElementKind.Before or PseudoElementKind.After. Read the value you want with GetComputedValue. Use GetStyle only for the ordinary element: it answers a different question, even when one of the pseudo-elements ends up with the same color.

How do I inspect computed styles for ::before and ::after?

A pseudo-element isn’t a separate HTML node to select. Find p, not p::before, and ask the style engine for the requested pseudo-element on that paragraph. The onehtml cascade docs explain how selectors ending in a pseudo-element match against the originating element. In the download, #draft::before wins for color because it’s more specific than .status::before; .status::before still supplies content because the ID rule doesn’t set that property.

A browser’s computed style API can query the pseudo-element on a live page. The C# example instead computes from the document and style sources you supply. It’s useful for checking generated markup before navigation, not for seeing what a script changed or measuring where a box ended up. If your rules are in an external stylesheet, supply it through StyleSourceOptions.Resolver rather than expecting onehtml to fetch it.

What’s the difference between ::before and ::after in CSS?

::before inserts generated content before the element’s contents; ::after inserts it after. Both can inherit properties such as color from the element, and each can override those properties separately. In the download the element and closing bracket are both navy, but the opening bracket is red. A declaration on the element is not a declaration targeting the pseudo-element directly.

Keep three things separate: a matching pseudo-element style, a generated box, and the final text. The variation has a matching ::before rule and a computed color, but content: none means no generated box. For generated content that uses attr() or counter(), the typed CSS value exposed by ComputedStyle.Content gives you those items; the style lookup doesn’t substitute attribute or counter values for you. The examples use literal strings so you can read the results directly.

All onehtml recipes