HtmlToAvalonia 1.0.2

dotnet add package HtmlToAvalonia --version 1.0.2
                    
NuGet\Install-Package HtmlToAvalonia -Version 1.0.2
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="HtmlToAvalonia" Version="1.0.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="HtmlToAvalonia" Version="1.0.2" />
                    
Directory.Packages.props
<PackageReference Include="HtmlToAvalonia" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add HtmlToAvalonia --version 1.0.2
                    
#r "nuget: HtmlToAvalonia, 1.0.2"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package HtmlToAvalonia@1.0.2
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=HtmlToAvalonia&version=1.0.2
                    
Install as a Cake Addin
#tool nuget:?package=HtmlToAvalonia&version=1.0.2
                    
Install as a Cake Tool

HtmlToAvalonia

A library that converts HTML strings with CSS formatting into Avalonia UI controls.

This library does not intend to to be a pixel-perfect replica of a web browser. The main goal is to make it easier for non-technical users of typical WYSIWYG editors to contribute rich content to Avalonia applications.

Features

  • HTML Parsing: Uses AngleSharp for standards-compliant HTML parsing
  • CSS Support: CSS styling support using AngleSharp.Css with GetComputedStyle
  • Text Formatting: Supports bold, italic, underline, headings, and custom styles
  • Layout: Handles block and inline elements, tables with colspan/rowspan, and text alignment
  • Color Support: Parses named colors, hex colors, and rgb/rgba values
  • Font Styling: Font size, weight, style, and decorations

Installation

Add the NuGet package to your project:

dotnet add package HtmlToAvalonia

Usage

Basic Usage

using HtmlToAvalonia;
using Avalonia.Controls;

// Simple text with formatting
var html = "<p>Hello <b>World</b>!</p>";
Control control = HtmlConverter.ToAvalonia(html);

// With CSS styling
var styledHtml = @"
    <div style='color: red; font-size: 16px;'>
        <p>This is <b>bold</b> and <i>italic</i> text.</p>
    </div>";
Control styledControl = HtmlConverter.ToAvalonia(styledHtml);

// With tables
var tableHtml = @"
    <table>
        <tr>
            <th>Header 1</th>
            <th>Header 2</th>
        </tr>
        <tr>
            <td>Cell 1</td>
            <td>Cell 2</td>
        </tr>
    </table>";
Control tableControl = HtmlConverter.ToAvalonia(tableHtml);

// With alignment
var centeredHtml = "<center><h1>Centered Title</h1></center>";
Control centeredControl = HtmlConverter.ToAvalonia(centeredHtml);

Advanced Usage with Optional Parameters

The ToAvalonia method accepts optional parameters to customize the rendering context:

var control3 = HtmlConverter.ToAvalonia(
    html,
    // Specify custom viewport dimensions (useful for responsive layouts)
    viewportWidth: 1024,
    viewportHeight: 768,
    // Specify custom base font size for em/rem calculations
    fontSize: 14.0
);

Parameters:

  • viewportWidth (optional): Viewport width in pixels. Defaults to primary screen width or 1920.
  • viewportHeight (optional): Viewport height in pixels. Defaults to primary screen height or 1080.
  • fontSize (optional): Base font size in pixels for em/rem unit calculations. Default is 16.0.

Note: The library automatically detects your screen's actual dimensions and DPI from Avalonia when parameters are not specified.

Applying HTML to an Existing TextBlock

If you want to apply HTML formatting directly to an existing TextBlock without creating wrapper controls, use the ApplyToTextBlock method. This is useful when you only need inline formatting (bold, italic, underline, spans, etc.) and don't need complex layouts like tables or divs:

using HtmlToAvalonia;
using Avalonia.Controls;

// Create a TextBlock
var textBlock = new TextBlock();

// Apply HTML with inline formatting
var html = "This is <b>bold</b>, <i>italic</i>, and <u>underlined</u> text with <span style='color: red;'>colored spans</span>.";
HtmlConverter.ApplyToTextBlock(textBlock, html);

// The TextBlock now contains formatted inline content
// Block-level elements (div, p, table, etc.) are ignored - only their inline content is extracted

Key Differences:

  • ToAvalonia(): Returns a Control that can include complex layouts (tables, divs, panels, etc.)
  • ApplyToTextBlock(): Applies only inline formatting to an existing TextBlock, ignoring block-level elements

Supported inline elements:

  • <b>, <strong> - Bold text
  • <i>, <em> - Italic text
  • <u> - Underlined text
  • <span> - Styled spans with CSS
  • <br> - Line breaks
  • <h1>, <h2>, <h3>, <h4>, <h5>, <h6> - Headings (rendered inline with appropriate font sizes)
  • <p> - Paragraphs (rendered inline without creating separate blocks)

Example with CSS and headings:

var textBlock = new TextBlock();
var html = @"
    <div>
        <h2>Welcome to HtmlToAvalonia!</h2>
        <p>This library supports <b style='color: blue;'>bold</b>, <i>italic</i>, and <u>underlined</u> text.</p>
        <br/>
        <h3>Features:</h3>
        You can use <span style='font-size: 18px; color: red;'>custom styles</span> and
        <b><i>combined formatting</i></b>.
    </div>";
HtmlConverter.ApplyToTextBlock(textBlock, html);

In this example, the <div> tag is ignored, but all the inline content and formatting is preserved. Headings (<h2>, <h3>) and paragraphs (<p>) are rendered inline with appropriate styling.

Supported HTML Tags

  • Text Formatting: <b>, <strong>, <i>, <em>, <u>
  • Headings: <h1>, <h2>, <h3>, <h4>, <h5>, <h6>
  • Containers: <div>, <span>, <p>, <body>
  • Layout: <center>, <table>, <tr>, <td>, <th>
  • Line Breaks: <br>

Supported CSS Properties

  • Color: color, background-color
  • Font: font-size, font-weight, font-style
  • Text: text-align, text-decoration
  • Spacing: padding, margin
  • Dimensions: width, height

Examples

Basic Formatting

var html = @"
    <div>
        <h1>Title</h1>
        <p>This is a paragraph with <b>bold</b>, <i>italic</i>, and <u>underlined</u> text.</p>
    </div>";
var control = HtmlConverter.ToAvalonia(html);

CSS Styling

var html = @"
    <div style='background-color: #f0f0f0; padding: 10px;'>
        <p style='color: blue; font-size: 18px; font-weight: bold;'>
            Styled paragraph
        </p>
    </div>";
var control = HtmlConverter.ToAvalonia(html);

Tables

var html = @"
    <table>
        <tr>
            <th colspan='2'>Header spanning 2 columns</th>
        </tr>
        <tr>
            <td>Row 1, Cell 1</td>
            <td>Row 1, Cell 2</td>
        </tr>
        <tr>
            <td rowspan='2'>Spanning 2 rows</td>
            <td>Row 2, Cell 2</td>
        </tr>
        <tr>
            <td>Row 3, Cell 2</td>
        </tr>
    </table>";
var control = HtmlConverter.ToAvalonia(html);

Text Alignment

var html = @"
    <div>
        <p style='text-align: left;'>Left aligned</p>
        <p style='text-align: center;'>Center aligned</p>
        <p style='text-align: right;'>Right aligned</p>
    </div>";
var control = HtmlConverter.ToAvalonia(html);

License

MIT License - see LICENSE file for details

Contributing

Contributions are welcome! Please feel free to submit issues or pull requests.

Limitations

  • External stylesheets are not loaded (only inline styles and <style> tags)
  • Complex CSS selectors and pseudo-elements are not fully supported
  • JavaScript and dynamic content are not supported
  • Some advanced CSS properties may not have Avalonia equivalents
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.2 408 12/9/2025
1.0.1 400 12/9/2025
1.0.0 389 12/9/2025