---
title: "Inspect CSS ::before and ::after styles in C#"
url: https://recursiveai.org/onehtml/cookbook/inspect-css-pseudo-element-styles/
description: "Read computed CSS styles for ::before and ::after in C# from static HTML. Inspect generated content, inherited colors and missing pseudo-element styles."
version: "1.6.0"
---

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

[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/inspect-css-pseudo-element-styles/inspect-css-pseudo-element-styles.zip)

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

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

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](https://recursiveai.org/onehtml/cookbook/inspect-css-pseudo-element-styles/inspect-css-pseudo-element-styles.zip), 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`.

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

```csharp
// 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
 
```

## Related recipes

- [Read ordinary computed styles](https://recursiveai.org/onehtml/cookbook/computed-style-csharp/)
- [Calculate CSS selector specificity](https://recursiveai.org/onehtml/cookbook/calculate-css-selector-specificity/)

---

## 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](https://recursiveai.org/onehtml/docs/cascade/#pseudo-element-styles) 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](https://developer.mozilla.org/en-US/docs/Web/API/Window/getComputedStyle) 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`](https://recursiveai.org/onehtml/docs/cascade/#style-sources) rather than expecting onehtml to fetch it.

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

[`::before`](https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Selectors/::before) inserts generated content before the element’s contents; [`::after`](https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Selectors/::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](https://recursiveai.org/onehtml/docs/css-values/#property-table) 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.
