AngleSharp.Css 1.1.1-beta.307

logo

AngleSharp.Css

CI GitHub Tag NuGet Count Issues Open CLA Assistant

AngleSharp.Css extends the core AngleSharp library with some more powerful CSS capabilities. This repository is the home of the source for the AngleSharp.Css NuGet package.

Basic Configuration

If you just want a configuration that works (as close as possible to real browsers) you should use the following code:

var config = Configuration.Default
    .WithCss(); // from AngleSharp.Css

This will register a parser for CSS related content. The CSS parsing options and more could be set with parameters of the WithCss method. Alternatively, all the (desired) parts may be registered individually as well. That mostly boils down to three elementary parts:

  • A CSS parser (implementing the ICssParser interface, e.g., CssParser)
  • A factory for creating CSS declarations (IDeclarationFactory)
  • The styling service that can handle CSS documents, see CssStylingService

For an interactive DOM (i.e., to handle style attribute changes in the HTML document) an observer needs to be registered as well.

Furthermore, for some CSSOM features (e.g., media queries) a render device is required.

var config = Configuration.Default
    .WithCss()
    .WithRenderDevice(new DefaultRenderDevice
    {
        DeviceHeight = 768,
        DeviceWidth = 1024,
    });

If no specific IRenderDevice (e.g., via creating an DefaultRenderDevice object) instance is created a default implementation will be set.

The render device also carries the user preferences of the Media Queries Level 5 (and Level 4) user-preference features. DefaultRenderDevice implements IRenderDevicePreferences for that, and any custom IRenderDevice can implement it as well. The dictionary is keyed by the media feature name and holds the keyword that the feature should answer with:

var config = Configuration.Default
    .WithCss()
    .WithRenderDevice(new DefaultRenderDevice
    {
        Preferences = new Dictionary<String, String>
        {
            { "prefers-color-scheme", "dark" },
            { "prefers-reduced-motion", "reduce" },
        },
    });

With this device @media (prefers-color-scheme: dark) applies in the cascade and window.MatchMedia("(prefers-color-scheme: dark)").IsMatched is true. A key that is not set leaves its media feature unknown, i.e., a query using it never matches. The keys a browser would set are:

Key Keywords
prefers-color-scheme light, dark
prefers-reduced-motion no-preference, reduce
prefers-reduced-transparency no-preference, reduce
prefers-contrast no-preference, more, less, custom
prefers-reduced-data no-preference, reduce
forced-colors none, active
hover, any-hover none, hover
pointer, any-pointer none, coarse, fine
display-mode fullscreen, standalone, minimal-ui, browser

The value is compared to the queried keyword case insensitively, so a keyword that is newer than this library works as well. Used without a value, e.g., @media (prefers-reduced-motion), the feature evaluates in a boolean context, where no-preference (and none for forced-colors, hover, any-hover, pointer and any-pointer) is false. Without a preference hover and pointer keep answering as they did before, i.e., as a device with no input mechanism.

Going a bit further it is possible to Render the current document. This render tree information can then be used to retrieve or other information, e.g.,

var tree = document.DefaultView.Render();
var node = tree.Find(document.QuerySelector("div"));
await node.DownloadResources();

The previous snippet renders the current document. Afterwards it retrieves a particular render tree node, which is related to the first found div. Then all (CSS introduced) resources are downloaded for the node, if visible.

Advantages of AngleSharp.Css

The core library already contains the CSS selector parser and the most basic classes and interfaces for dealing with the CSSOM. AngleSharp.Css brings the following advantages and use cases to life:

  • Correct identification of edge cases
  • A live CSSOM model, i.e., callbacks and everything
  • Computing the styling of certain elements
  • Cascades of stylesheets
  • Validation (and property-based exposure) of CSS declarations
  • Responsive design considerations
  • Full access to the value with different converters

The main idea behind AngleSharp.Css is to expose the CSSOM as it would be in the browser (and potentially beyond, i.e., useful for being used by editors). Originally, most of the code found here was embedded in the AngleSharp.Core library, however, due to the overhead for HTML use cases it was decided to transfer the code into its own repository.

Features

  • Feature validators (e.g., for @supports)
  • Document functions (e.g., for domain)
  • Pseudo elements (e.g., ::before)
  • Declarations (e.g., display) incl. knowledge of their values
  • Dynamic DOM coupling (i.e., to react to style attribute changes)
  • CSS custom properties (also known as CSS variables) with extensibility
  • Media queries and all other commonly implemented rules
  • Calculated values (i.e., calc(20px + 50%))
  • Window-based declaration calculations, see window.GetComputedStyle

Benchmarks

The AngleSharp.Performance.Css project uses BenchmarkDotNet to compare CSS parsing performance across libraries. Run the benchmarks in Release mode:

dotnet run --project src/AngleSharp.Performance.Css/AngleSharp.Performance.Css.csproj -c Release --framework net10.0

To run a quick smoke test instead of a full benchmark:

dotnet run --project src/AngleSharp.Performance.Css/AngleSharp.Performance.Css.csproj -c Release --framework net10.0 -- --job short

Participating

Participation in the project is highly welcome. For this project the same rules as for the AngleSharp core project may be applied.

If you have any question, concern, or spot an issue then please report it before opening a pull request. An initial discussion is appreciated regardless of the nature of the problem.

Live discussions can take place in our Gitter chat, which supports using GitHub accounts.

This project has adopted the code of conduct defined by the Contributor Covenant to clarify expected behavior in our community.

For more information see the .NET Foundation Code of Conduct.

.NET Foundation

This project is supported by the .NET Foundation.

License

AngleSharp.Css is released using the MIT license. For more information see the license file.

Showing the top 20 packages that depend on AngleSharp.Css.

Packages Downloads
AngleSharp.Diffing
Provides a complete diffing model of HTML.
22
AngleSharp.Diffing
Provides a complete diffing model of HTML.
23
AngleSharp.Diffing
Provides a complete diffing model of HTML.
25
AngleSharp.Wrappers
A library of wrappers for AngleSharp. Allows you to replace a real AngleSharp DOM tree but still keep the queried/returned node references. Built for and used by https://github.com/bUnit-dev/bUnit.
25
AngleSharp.Wrappers
A library of wrappers for AngleSharp. Allows you to replace a real AngleSharp DOM tree but still keep the queried/returned node references. Built for and used by https://github.com/egil/razor-components-testing-library.
23
bunit.web
bUnit.web is the web specific parts of bUnit, that enables you to easily test and verify the output of Blazor (web) component.
22
bunit.web
bUnit.web is the web specific parts of bUnit, that enables you to easily test and verify the output of Blazor (web) component.
23
bunit.web
bUnit.web is the web specific parts of bUnit, that enables you to easily test and verify the output of Blazor (web) component.
24
bunit.web
bUnit.web is the web specific parts of bUnit, that enables you to easily test and verify the output of Blazor (web) component.
25
bunit.web
bUnit.web is the web specific parts of bUnit, that enables you to easily test and verify the output of Blazor (web) component.
27

.NET 10.0

.NET Framework 4.6.2

.NET Framework 4.7.2

.NET 8.0

.NET Standard 2.0

Version Downloads Last updated
1.1.3-beta.330 3 15.09.2026
1.1.2 1 12.09.2026
1.1.2-beta.323 1 12.09.2026
1.1.2-beta.322 2 12.09.2026
1.1.2-beta.321 1 12.09.2026
1.1.1 1 12.09.2026
1.1.1-beta.317 1 12.09.2026
1.1.1-beta.316 2 12.09.2026
1.1.1-beta.311 1 12.09.2026
1.1.1-beta.310 2 12.09.2026
1.1.1-beta.309 1 12.09.2026
1.1.1-beta.308 1 12.09.2026
1.1.1-beta.307 1 12.09.2026
1.1.0 1 12.09.2026
1.1.0-beta.302 1 12.09.2026
1.1.0-beta.297 2 12.09.2026
1.1.0-beta.295 2 12.09.2026
1.1.0-beta.293 1 12.09.2026
1.1.0-beta.291 1 12.09.2026
1.1.0-beta.283 1 12.09.2026
1.1.0-beta.281 1 12.09.2026
1.1.0-beta.277 1 12.09.2026
1.1.0-beta.276 2 12.09.2026
1.1.0-beta.273 1 03.09.2026
1.0.2 3 22.08.2026
1.0.2-beta.272 1 03.09.2026
1.0.2-beta.271 2 03.09.2026
1.0.2-beta.265 3 22.08.2026
1.0.1 4 10.08.2026
1.0.1-beta.264 3 22.08.2026
1.0.1-beta.262 3 17.08.2026
1.0.1-beta.260 5 10.08.2026
1.0.1-beta.258 4 10.08.2026
1.0.1-beta.256 5 10.08.2026
1.0.1-beta.252 4 10.08.2026
1.0.1-beta.250 4 06.08.2026
1.0.1-beta.247 4 06.08.2026
1.0.1-beta.245 4 10.08.2026
1.0.0 6 31.07.2026
1.0.0-beta.244 4 31.07.2026
1.0.0-beta.243 4 31.07.2026
1.0.0-beta.239 4 10.08.2026
1.0.0-beta.237 3 10.08.2026
1.0.0-beta.231 5 31.07.2026
1.0.0-beta.228 4 31.07.2026
1.0.0-beta.226 3 10.08.2026
1.0.0-beta.224 4 21.07.2026
1.0.0-beta.222 5 21.07.2026
1.0.0-beta.216 9 13.05.2026
1.0.0-beta.213 10 04.04.2026
1.0.0-beta.211 8 29.03.2026
1.0.0-beta.209 7 29.03.2026
1.0.0-beta.204 9 29.03.2026
1.0.0-beta.202 9 29.03.2026
1.0.0-beta.199 10 29.03.2026
1.0.0-beta.197 9 29.03.2026
1.0.0-beta.195 9 29.03.2026
1.0.0-beta.189 8 29.03.2026
1.0.0-beta.186 8 29.03.2026
1.0.0-beta.184 12 29.03.2026
1.0.0-beta.178 11 29.03.2026
1.0.0-beta.176 9 29.03.2026
1.0.0-beta.167 11 09.02.2026
1.0.0-beta.164 18 18.12.2025
1.0.0-beta.159 14 11.11.2025
1.0.0-beta.157 16 17.09.2025
1.0.0-beta.154 19 31.05.2025
1.0.0-beta.151 22 06.03.2025
1.0.0-beta.149 22 06.03.2025
1.0.0-beta.147 24 06.03.2025
1.0.0-beta.144 22 06.03.2025
1.0.0-beta.139 23 06.03.2025
1.0.0-beta.137 23 06.03.2025
1.0.0-beta.130 22 06.03.2025
1.0.0-beta.127 20 06.03.2025
1.0.0-beta.122 27 06.03.2025
1.0.0-alpha-99 23 06.03.2025
1.0.0-alpha-98 17 06.03.2025
1.0.0-alpha-97 25 06.03.2025
1.0.0-alpha-117 27 06.03.2025
1.0.0-alpha-113 28 06.03.2025
1.0.0-alpha-111 23 06.03.2025
1.0.0-alpha-109 26 06.03.2025
1.0.0-alpha-108 23 06.03.2025
1.0.0-alpha-107 21 06.03.2025
1.0.0-alpha-106 20 06.03.2025
1.0.0-alpha-104 22 06.03.2025
1.0.0-alpha-103 35 06.03.2025
1.0.0-alpha-102 21 06.03.2025
1.0.0-alpha-101 24 06.03.2025
1.0.0-alpha-100 21 06.03.2025
0.18.0 5 06.08.2026
0.18.0-beta.233 4 31.07.2026
0.17.0 25 06.03.2025
0.17.0-alpha-87 24 06.03.2025
0.17.0-alpha-86 25 06.03.2025
0.17.0-alpha-80 23 06.03.2025
0.17.0-alpha-78 23 06.03.2025
0.17.0-alpha-77 21 06.03.2025
0.17.0-alpha-76 26 06.03.2025
0.17.0-alpha-75 22 06.03.2025
0.16.4 18 06.03.2025
0.16.4-alpha-72 25 06.03.2025
0.16.4-alpha-68 20 06.03.2025
0.16.3 25 06.03.2025
0.16.3-alpha-65 20 09.03.2025
0.16.2 24 06.03.2025
0.16.2-alpha-62 21 06.03.2025
0.16.2-alpha-61 21 06.03.2025
0.16.2-alpha-60 24 08.03.2025
0.16.2-alpha-42 22 06.03.2025
0.16.2-alpha-35 23 06.03.2025
0.16.2-alpha-30 19 06.03.2025
0.16.2-alpha-29 22 06.03.2025
0.16.2-alpha-28 26 06.03.2025
0.16.1 22 06.03.2025
0.16.1-alpha-27 22 06.03.2025
0.16.1-alpha-24 21 06.03.2025
0.16.0 20 06.03.2025
0.16.0-alpha-23 24 06.03.2025
0.16.0-alpha-19 21 06.03.2025
0.15.1 23 06.03.2025
0.15.1-alpha-13 22 06.03.2025
0.15.1-alpha-11 25 06.03.2025
0.15.0 21 06.03.2025
0.15.0-alpha-7 22 08.03.2025
0.14.3-alpha-5 22 06.03.2025
0.14.3-alpha-4 24 06.03.2025
0.14.3-alpha-166 26 06.03.2025
0.14.3-alpha-165 25 06.03.2025
0.14.2 21 06.03.2025
0.14.2-alpha-163 22 06.03.2025
0.14.2-alpha-162 23 07.03.2025
0.14.1 22 06.03.2025
0.14.1-alpha-161 26 06.03.2025
0.14.0 24 06.03.2025
0.14.0-alpha-160 27 06.03.2025
0.14.0-alpha-154 21 06.03.2025
0.14.0-alpha-152 25 08.03.2025
0.14.0-alpha-149 23 06.03.2025
0.14.0-alpha-147 26 07.03.2025
0.14.0-alpha-145 21 06.03.2025
0.14.0-alpha-143 22 06.03.2025
0.14.0-alpha-139 24 08.03.2025
0.14.0-alpha-138 24 06.03.2025
0.13.0 25 06.03.2025
0.13.0-alpha-135 22 06.03.2025
0.13.0-alpha-131 24 06.03.2025
0.13.0-alpha-123 23 06.03.2025
0.13.0-alpha-121 24 06.03.2025
0.13.0-alpha-119 20 06.03.2025
0.13.0-alpha-117 24 06.03.2025
0.13.0-alpha-116 24 06.03.2025
0.13.0-alpha-115 23 06.03.2025
0.13.0-alpha-114 25 06.03.2025
0.13.0-alpha-113 22 06.03.2025
0.12.1 23 06.03.2025
0.12.0 22 06.03.2025
0.10.0 23 06.03.2025