Use ResolveKeyframes to read the animation stops without playing them.
dotnet add package RecursiveAI.OneHtmlRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
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, then dotnet run.
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:
foreach (var animation in styles.GetAnimationKeyframes(style)) if (animation is not null) Console.WriteLine(animation.Name);
pulseRelated recipes
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 and onehtml’s keyframe resolution notes.
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.