---
title: "Extract CSS animation keyframes from HTML in C#"
url: https://recursiveai.org/onehtml/cookbook/read-css-animation-keyframes/
description: "Read the CSS animation keyframes for an HTML element in C#, including offsets, property values, and missing endpoints inferred from its style."
version: "1.6.0"
---

# Extract CSS animation keyframes from 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/read-css-animation-keyframes/read-css-animation-keyframes.zip)

Use `ResolveKeyframes` to read the animation stops without playing them.

```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>
    #dot { opacity: .8;
           animation: pulse 1s; }
    @keyframes pulse { 50% { opacity: .2 } }
    </style>
    <span id="dot">•</span>
    """);
var styles = DocumentStyles.Create(doc);
var dot = doc.Root.QuerySelector("#dot");
var style = styles.GetStyle(dot);
var pulse = styles.ResolveKeyframes(
    style, "pulse")
    ?? throw new FormatException("No pulse");
foreach (var frame in pulse.Keyframes)
foreach (var value in frame.Values)
{
    var percent = frame.Offset * 100;
    Console.WriteLine($"{percent}% " +
        $"{value.Property}: {value.Value}");
}
```

Unzip [the example](https://recursiveai.org/onehtml/cookbook/read-css-animation-keyframes/read-css-animation-keyframes.zip), then `dotnet run`.

```text
0% opacity: 0.8
50% opacity: .2
100% opacity: 0.8
```

The 50% frame keeps the written value `.2`; the inferred endpoints use the computed value `0.8`.

If the supplied name has no matching `@keyframes` rule, `ResolveKeyframes` returns `null`. The example turns that into an error so a misspelled name doesn’t go unnoticed.

## Variations

**Read the animations named in the element’s CSS.** Replace the `pulse` binding and print loop to list all resolved names. Skip `null` entries, which stand for `none` or a name with no matching rule:

```csharp
foreach (var animation in styles.GetAnimationKeyframes(style))
    if (animation is not null)
        Console.WriteLine(animation.Name);
```

```text
pulse
```

## Related recipes

- [Read computed CSS styles in C#](https://recursiveai.org/onehtml/cookbook/computed-style-csharp/)
- [Resolve CSS custom properties with fallback](https://recursiveai.org/onehtml/cookbook/resolve-css-custom-properties-with-fallback/)

## The short answer

Parse the HTML with `Document.Parse`, create `DocumentStyles`, and pass the selected element’s computed style and the animation name to `ResolveKeyframes`. Each resolved frame has an `Offset` from 0 to 1 and a list of property names and values. Multiply the offset by 100 to print a percentage. This reads the stops, not what the animation looks like between them.

## What happens when CSS keyframes omit 0% and 100%?

`from` and `to` mean `0%` and `100%`, respectively. You don’t have to write either endpoint. In the example, the only authored stop is `50%`; for the missing endpoints, the library uses the element’s computed `opacity` of `0.8`. That’s why the printout has three rows even though there’s only one declared stop. The same rule applies property by property: if an endpoint exists but doesn’t set a property animated elsewhere, the resolved frames still account for that property’s base value there. See [MDN’s `@keyframes` reference](https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/At-rules/@keyframes) and [onehtml’s keyframe resolution notes](https://recursiveai.org/onehtml/docs/cascade/#keyframes).

## Why aren’t my CSS keyframes showing up?

First check that the name in `animation-name` matches the `@keyframes` name exactly; names are case-sensitive. Then check whether a conditional rule such as `@media` applies in the style context you’re using and whether the stylesheet is available to the cascade. If you only need to inspect the winning definition by name, `GetKeyframesRule` reads the rule itself. Use `ResolveKeyframes` or `GetAnimationKeyframes` when you need the result for a particular element, including its base style and substitutions from its custom properties.

Authored values aren’t all converted to the units a browser might report: for example, an explicit `1em` can remain `1em`. Inferred endpoints use computed values. The library doesn’t run a clock, interpolate intermediate values, or capture an animation that requires executing page scripts.
