Skip to main content

Markdown styling

The Markdown control is built on the shared FlowDocument model, where every rendered element (Paragraph, Section, Table, RichSpan, RichHyperlink, etc.) is a full Avalonia StyledElement. This means you can style Markdown output using two complementary approaches:

  1. DocumentNode style selectors — target elements by type and CSS-like class (e.g., Paragraph.h1, Section.quoteBlock).
  2. Named resources — override theme values such as font sizes, margins, and brush colors.
info

This control is available as part of Avalonia Pro or higher.

DocumentNode style selectors​

Because all document elements are StyledElement instances, you can target them with standard Avalonia style selectors. The Markdown renderers apply CSS-like classes to each element, so you can style specific Markdown constructs without affecting others.

Available selectors​

The following table lists the style selectors used by the default theme. You can override or extend any of these in your application styles.

Block selectors​

SelectorDescription
:is(Block)All block-level elements (paragraphs, sections, etc.). Sets base margin.
:is(Block).codeBlockFenced code blocks. Background, border, padding, and monospace font.
Paragraph.headerAll heading paragraphs (h1–h6). Bold font weight.
Paragraph.h1Level 1 heading.
Paragraph.h2Level 2 heading.
Paragraph.h3Level 3 heading.
Paragraph.h4Level 4 heading.
Paragraph.h5Level 5 heading.
Paragraph.h6Level 6 heading.
Section.quoteBlock *Block quotes. Left border, padding, muted foreground.
Section.alertBlock *All alert blocks (NOTE, TIP, IMPORTANT, WARNING, CAUTION).
Section.alertBlock.note *Note alert block border.
Section.alertBlock.tip *Tip alert block border.
Section.alertBlock.important *Important alert block border.
Section.alertBlock.warning *Warning alert block border.
Section.alertBlock.caution *Caution alert block border.
BlockUIContainer.thematicBreakHorizontal rule (thematic break).
TableTable element. Border, cell spacing.
TableCellTable cell element. Border.
TableRow.tableHeaderHeader row. Bold font weight.
ListList element. Left padding, line height.
ListItemList item element. Line height.

* Alert block headers (e.g., "Note", "Warning") are drawn by the alert block itself. They are not considered a paragraph in the document, so they cannot be directly selected for styling. Instead, style them through Section.alertBlock, whose inherited FontFamily, FontSize and Foreground reach the header and the body together.

Inline selectors​

SelectorDescription
RichRun.codeInline code spans. Background and monospace font.
RichSpan.headerInline spans inside headings. Bold font weight.
RichSpan.h1 through RichSpan.h6Inline spans with heading-level font sizes.
RichSpan.strikeThroughStrikethrough text decoration.
RichSpan.insertedUnderline text decoration (inserted text).
RichSpan.markedHighlighted/marked text background.
RichHyperlinkHyperlink element. Foreground color.
RichHyperlink:pointeroverHyperlink on hover. Underline + hover color.
RichHyperlink:visitedVisited hyperlink color.

Document element selectors​

SelectorDescription
MarkdownCodeBlockCode block element. Set Highlighter property here.
MarkdownImageImage inline element. Set ImageLoader property here.

Custom styling examples​

Override heading colors:

<Style Selector="Paragraph.h1">
<Setter Property="Foreground" Value="#1a73e8" />
</Style>
<Style Selector="Paragraph.h2">
<Setter Property="Foreground" Value="#188038" />
</Style>

Custom quote block appearance:

<Style Selector="Section.quoteBlock">
<Setter Property="Background" Value="#f8f9fa" />
<Setter Property="BorderBrush" Value="#6c757d" />
<Setter Property="BorderThickness" Value="3,0,0,0" />
<Setter Property="CornerRadius" Value="4" />
</Style>

Custom inline code styling:

<Style Selector="RichRun.code">
<Setter Property="Background" Value="#e8f0fe" />
<Setter Property="FontFamily" Value="Cascadia Code" />
</Style>

Custom code block with rounded corners and different background:

<Style Selector=":is(Block).codeBlock">
<Setter Property="Background" Value="#1e1e1e" />
<Setter Property="Foreground" Value="#d4d4d4" />
<Setter Property="CornerRadius" Value="8" />
<Setter Property="Padding" Value="20" />
</Style>

Scoped nesting selectors also work. For example, remove paragraph margins inside tables:

<Style Selector="Table Paragraph">
<Setter Property="Margin" Value="0" />
</Style>

Customizable resources​

In addition to style selectors, you can override named resources in your theme or resource dictionary. These are used by the default styles and provide an easy way to adjust values without rewriting selectors.

Blocks​

KeyTypeDefaultNotes
MarkdownBlockMarginThickness0,8Block outer margin
KeyTypeDefault (Light)Default (Dark)Notes
MarkdownHyperlinkForegroundBrush#0969da#58a6ffLink color
MarkdownHyperlinkForegroundVisitedBrush#551A8B#bc8cffVisited link color
MarkdownHyperlinkForegroundPointerOverBrush#0056b3#79c0ffHover link color

Selection​

KeyTypeDefaultNotes
MarkdownSelectionBrushBrush#FF086F9ESelection highlight

Code​

KeyTypeDefaultNotes
MarkdownCodeFontFamilyFontFamilyCourier NewInline/code font

Code blocks​

KeyTypeDefault (Light)Default (Dark)Notes
MarkdownCodeBlockParagraphPaddingThickness1616Inner padding
MarkdownCodeBlockParagraphBorderThicknessThickness11Border thickness
MarkdownCodeBlockParagraphCornerRadiusCornerRadius66Corner radius
MarkdownCodeBlockParagraphBackgroundBrush#1f818b98#20484f58Background
MarkdownCodeBlockParagraphBorderBrushBrush#e3ebf6#30363dBorder brush

Inline code​

KeyTypeDefault (Light)Default (Dark)Notes
MarkdownCodeRunBackgroundBrush#1f818b98#20484f58Inline code background

Thematic break​

KeyTypeDefault (Light)Default (Dark)Notes
MarkdownThematicBreakRectangleFillBrush#d1d9e0#30363dThematic break color

Headers (per level)​

Each header exposes FontSize, BorderThickness, Padding, Margin.

KeyTypeDefaultNotes
MarkdownHeader1ParagraphFontSizeDouble31.5H1 font size
MarkdownHeader1ParagraphBorderThicknessThickness0,0,0,1H1 bottom border
MarkdownHeader1ParagraphPaddingThickness0,0,0,16H1 inner padding (bottom)
MarkdownHeader1ParagraphMarginThickness0,31,0,14H1 outer margin
MarkdownHeader2ParagraphFontSizeDouble24.5H2 font size
MarkdownHeader2ParagraphBorderThicknessThickness0,0,0,1H2 bottom border
MarkdownHeader2ParagraphPaddingThickness0,0,0,12H2 inner padding (bottom)
MarkdownHeader2ParagraphMarginThickness0,24.5,0,14H2 outer margin
MarkdownHeader3ParagraphFontSizeDouble21H3 font size
MarkdownHeader3ParagraphBorderThicknessThickness0,0,0,1H3 bottom border
MarkdownHeader3ParagraphPaddingThickness0,0,0,8H3 inner padding (bottom)
MarkdownHeader3ParagraphMarginThickness0,21,0,14H3 outer margin
MarkdownHeader4ParagraphFontSizeDouble16.8H4 font size
MarkdownHeader4ParagraphBorderThicknessThickness0,0,0,1H4 bottom border
MarkdownHeader4ParagraphPaddingThickness0,0,0,6H4 inner padding (bottom)
MarkdownHeader4ParagraphMarginThickness0,16.8,0,14H4 outer margin
MarkdownHeader5ParagraphFontSizeDouble14H5 font size
MarkdownHeader5ParagraphBorderThicknessThickness0,0,0,1H5 bottom border
MarkdownHeader5ParagraphPaddingThickness0,0,0,4H5 inner padding (bottom)
MarkdownHeader5ParagraphMarginThickness0,14,0,14H5 outer margin
MarkdownHeader6ParagraphFontSizeDouble14H6 font size
MarkdownHeader6ParagraphBorderThicknessThickness0,0,0,1H6 bottom border
MarkdownHeader6ParagraphPaddingThickness0,0,0,2H6 inner padding (bottom)
MarkdownHeader6ParagraphMarginThickness0,14,0,14H6 outer margin

Quote blocks​

KeyTypeDefault (Light)Default (Dark)Notes
MarkdownQuoteBlockSectionBorderThicknessThickness4,0,0,04,0,0,0Quote block left border thickness
MarkdownQuoteBlockSectionBorderBrushBrush#DDDDDD#3b434bQuote block border brush
MarkdownQuoteBlockSectionForegroundBrush#777777#8b949eQuote block foreground color
MarkdownQuoteBlockSectionPaddingThickness15,015,0Quote block inner padding
MarkdownQuoteBlockFirstChildSectionMarginThickness0,0,0,140,0,0,14Margin for first child in quote
MarkdownQuoteBlockLastChildSectionMarginThickness00Margin for last child in quote

Tables​

KeyTypeDefault (Light)Default (Dark)Notes
MarkdownTableCellBorderBrushBrushBlack#30363dTable cell border brush
MarkdownTableBorderBrushBrushBlack#30363dTable border brush
MarkdownTableBorderThicknessThickness0,0,1,10,0,1,1Table outer border thickness
MarkdownTableCellBorderThicknessThickness1,1,0,01,1,0,0Cell border thickness
MarkdownTableCellParagraphPaddingThickness12,512,5Cell paragraph padding

Inline styles​

KeyTypeDefault (Light)Default (Dark)Notes
MarkdownMarkedSpanBackgroundBrushYellow#bb8009Highlight background for marked spans

Alert blocks (per type)​

KeyTypeDefault (Light)Default (Dark)Notes
MarkdownAlertBlockNoteBorderBrushBrush#0969da#58a6ffNote alert border brush
MarkdownAlertBlockTipBorderBrushBrush#1a7f37#3fb950Tip alert border brush
MarkdownAlertBlockImportantBorderBrushBrush#8250df#bc8cffImportant alert border brush
MarkdownAlertBlockWarningBorderBrushBrush#9a6700#d29922Warning alert border brush
MarkdownAlertBlockCautionBorderBrushBrush#d1242f#f85149Caution alert border brush

Copy button​

KeyTypeDefault (Light)Default (Dark)Notes
MarkdownCopyButtonFillBrush#59636e#8b949eCopy button fill color
MarkdownCopyButtonContentTemplateDataTemplate——Content template for copy button

How to override​

Place your styles after the StyleInclude for Default.axaml in App.axaml or in a merged resource dictionary. Later styles take precedence:

<Application.Styles>
<StyleInclude Source="avares://Avalonia.Controls.Markdown/Themes/Default.axaml" />

<!-- Your overrides -->
<Style Selector="Paragraph.h1">
<Setter Property="Foreground" Value="Navy" />
</Style>
<Style Selector="Section.quoteBlock">
<Setter Property="Background" Value="#f0f4f8" />
</Style>
</Application.Styles>

Named resources​

Define resources in your application theme or resource dictionary:

<SolidColorBrush x:Key="MarkdownHyperlinkForeground" Color="#FF0000" />

Example: custom theme​

<ResourceDictionary>
<SolidColorBrush x:Key="MarkdownSelectionBrush" Color="#FFD700" />
<FontFamily x:Key="MarkdownCodeFontFamily">Cascadia Code</FontFamily>
</ResourceDictionary>

Combined with style selectors for a dark code theme:

<Style Selector=":is(Block).codeBlock">
<Setter Property="Background" Value="#2d2d2d" />
<Setter Property="Foreground" Value="#cccccc" />
<Setter Property="CornerRadius" Value="8" />
</Style>

See also​