The Padder widget wraps any renderable content with configurable padding on all sides.
When to Use
Use Padder when you need to control whitespace and spacing around content. Common scenarios:
- Visual separation: Add breathing room between consecutive widgets or sections
- Indentation and hierarchy: Create visual structure by indenting nested content
- Fine-tuned layout: Achieve precise spacing when containers like Panel have fixed padding
For centering content horizontally or vertically, use Align instead. For content with a border and built-in padding, use Panel instead.
Basic Usage
Wrap any renderable with new Padder(content). By default, adds 1 space padding on all sides.
public static void BasicPadderExample()
{
var text = new Markup("[bold blue]Important Message[/]");
var padded = new Padder(text);
AnsiConsole.Write(padded);
}
Setting Padding
Uniform Padding
Pass a single value to new Padding() to apply the same padding on all four sides.
public static void PadderUniformPaddingExample()
{
var text = new Markup("[yellow]Status: Active[/]");
var padded = new Padder(text, new Padding(3));
AnsiConsole.Write(padded);
}
Horizontal and Vertical
Use the two-parameter constructor to set horizontal (left/right) and vertical (top/bottom) padding independently.
public static void PadderHorizontalVerticalPaddingExample()
{
var text = new Markup("[green]Success![/]");
// 4 spaces left/right, 2 lines top/bottom
var padded = new Padder(text, new Padding(4, 2));
AnsiConsole.Write(padded);
}
Individual Sides
Use the four-parameter constructor to control each side precisely when you need asymmetric spacing.
public static void PadderIndividualSidesExample()
{
var text = new Markup("[red]Error detected[/]");
// Left: 2, Top: 1, Right: 6, Bottom: 1
var padded = new Padder(text, new Padding(2, 1, 6, 1));
AnsiConsole.Write(padded);
}
Using Extension Methods
Use the fluent PadLeft(), PadRight(), PadTop(), and PadBottom() extension methods to adjust individual sides after construction.
public static void PadderFluentExtensionsExample()
{
var text = new Markup("[cyan]Notification[/]");
var padded = new Padder(text)
.PadLeft(5)
.PadRight(5)
.PadTop(2)
.PadBottom(2);
AnsiConsole.Write(padded);
}
Working with Other Widgets
With Panels
Wrap panels with padding to add outer spacing when the panel's internal padding isn't sufficient.
public static void PadderWithPanelExample()
{
var panel = new Panel("[yellow]Warning:[/] Low disk space")
.BorderColor(Color.Yellow)
.Header("System Alert");
var padded = new Padder(panel, new Padding(3, 1));
AnsiConsole.Write(padded);
}
With Tables
Add padding around tables to separate them from surrounding content or create margins.
public static void PadderWithTableExample()
{
var table = new Table()
.AddColumn("Name")
.AddColumn("Status")
.AddRow("Service A", "[green]Running[/]")
.AddRow("Service B", "[red]Stopped[/]");
var padded = new Padder(table, new Padding(2, 1, 2, 1));
AnsiConsole.Write(padded);
}
Controlling Width
By default, Padder automatically calculates width based on content. Set Expand = true to fill the available width, which affects how padding appears.
public static void PadderExpandExample()
{
var text = new Markup("[blue]Left aligned text[/]");
AnsiConsole.MarkupLine("[yellow]Auto-width (default):[/]");
var autoWidth = new Padder(text, new Padding(2));
AnsiConsole.Write(autoWidth);
AnsiConsole.WriteLine();
AnsiConsole.MarkupLine("[yellow]Expanded to fill width:[/]");
var expanded = new Padder(text, new Padding(2))
{
Expand = true
};
AnsiConsole.Write(expanded);
}
Advanced Usage
Nested Padding
Nest multiple Padder instances to create compound spacing effects or complex layouts.
public static void PadderNestedExample()
{
var content = new Markup("[bold white on blue] Inner Content [/]");
var innerPadder = new Padder(content, new Padding(2, 1));
var outerPadder = new Padder(innerPadder, new Padding(4, 2));
AnsiConsole.Write(outerPadder);
}
Creating Visual Structure
Use targeted padding on specific sides to build structured layouts with headers, body content, and footers.
public static void PadderVisualSeparationExample()
{
var header = new Padder(
new Markup("[bold underline]Application Dashboard[/]"),
new Padding(0, 0, 0, 1));
var body = new Padder(
new Markup("System running normally\nAll services operational"),
new Padding(2, 1));
var footer = new Padder(
new Markup("[dim]Last updated: 2024-11-24[/]"),
new Padding(0, 1, 0, 0));
AnsiConsole.Write(header);
AnsiConsole.Write(body);
AnsiConsole.Write(footer);
}
API Reference
Represents padding around a object.
Constructors
Padder(IRenderable child, Nullable<Padding> padding)Initializes a new instance of the class.
Parameters:
child (IRenderable)padding (Nullable<Padding>)Properties
Expand
: boolGets or sets a value indicating whether or not the padding should fit the available space. If false, the padding width will be auto calculated. Defaults to false.
Padding
: Nullable<Padding>Extension Methods
IEnumerable<Segment> GetSegments(IAnsiConsole console)Gets the segments for a renderable using the specified console.
Parameters:
console (IAnsiConsole)Returns:
An enumerable containing segments representing the specified .