Markup Widget

Render styled text using an inline markup syntax

The Markup widget renders text with colors and decorations using an inline tag syntax like [bold red]text[/].

When to Use

Use Markup when you want inline styling with readable syntax. Common scenarios:

  • Status messages: Quick colored output like [green]Success[/] or [red]Error[/]
  • Formatted output: Mix styles naturally within text
  • Static styling: When styles are known at compile time

For programmatic control over styling or when styles are determined at runtime, use Text instead.

Basic Usage

Use AnsiConsole.MarkupLine() for quick styled output.

// Simple colored text
AnsiConsole.MarkupLine("[green]Success![/]");
AnsiConsole.MarkupLine("[red]Error occurred[/]");
  
// Multiple colors in one line
AnsiConsole.MarkupLine("[blue]Info:[/] Processing [yellow]3[/] items...");
  
// Text decorations
AnsiConsole.MarkupLine("[bold]Bold text[/]");
AnsiConsole.MarkupLine("[italic]Italic text[/]");
AnsiConsole.MarkupLine("[underline]Underlined text[/]");
  
// Combined styles
AnsiConsole.MarkupLine("[bold red]Critical error[/]");
AnsiConsole.MarkupLine("[bold green on black]Highlighted success[/]");

Creating Markup Objects

Create Markup objects when you need to embed styled text in containers like panels, tables, or layouts.

// Create a Markup object
var message = new Markup("[bold blue]Important Notice[/]");
AnsiConsole.Write(message);
AnsiConsole.WriteLine();
  
// Markup with multi-line content
var multiLine = new Markup(
    "[yellow]Warning:[/] Multiple issues detected.\n" +
    "[dim]See log for details.[/]"
);
AnsiConsole.Write(multiLine);

Escaping Content

Use Markup.Escape() when working with user-provided or dynamic content that might contain bracket characters. Without escaping, brackets are interpreted as markup tags.

// User input that contains brackets
var userInput = "Use [brackets] for arrays";
var escaped = Markup.Escape(userInput);
  
// Safe to use in markup string
AnsiConsole.MarkupLine($"[blue]User said:[/] {escaped}");
  
// Without escaping, brackets would be interpreted as markup tags
var fileName = "config[backup].json";
AnsiConsole.MarkupLine($"[green]Processing:[/] {Markup.Escape(fileName)}");

Working with Containers

Markup objects work well as content inside panels, tables, and other container widgets.

// Markup in a Panel
var panel = new Panel(new Markup("[bold]Welcome![/]\n\nThis panel contains [green]styled[/] text."))
    .Header("Message")
    .BorderColor(Color.Blue);
  
AnsiConsole.Write(panel);
AnsiConsole.WriteLine();
  
// Markup in a Table
var table = new Table()
    .AddColumn("Status")
    .AddColumn("Message");
  
table.AddRow(
    new Markup("[green]OK[/]"),
    new Markup("All systems operational")
);
table.AddRow(
    new Markup("[yellow]WARN[/]"),
    new Markup("High memory usage detected")
);
table.AddRow(
    new Markup("[red]ERROR[/]"),
    new Markup("[bold]Connection failed[/]")
);
  
AnsiConsole.Write(table);

Removing Markup

Use Markup.Remove() to strip all markup tags from a string, leaving only plain text.

var styled = "[bold red]Error:[/] File [underline]not found[/]";
  
// Remove all markup tags to get plain text
var plain = Markup.Remove(styled);
  
AnsiConsole.MarkupLine("[dim]Original:[/]");
AnsiConsole.MarkupLine(styled);
AnsiConsole.WriteLine();
AnsiConsole.MarkupLine("[dim]Without markup:[/]");
AnsiConsole.WriteLine(plain);

See Also

API Reference

A renderable piece of markup text.

Constructors

Markup(string text, Style style)

Initializes a new instance of the class.

Parameters:

text (string)
The markup text.
style (Style)
The style of the text.

Properties

Justification : Nullable<Justify>
Length : int

Gets the character count.

Lines : int

Gets the number of lines.

Overflow : Nullable<Overflow>

Extension Methods

IEnumerable<Segment> GetSegments(IAnsiConsole console)

Gets the segments for a renderable using the specified console.

Parameters:

console (IAnsiConsole)
The console.

Returns:

An enumerable containing segments representing the specified .