The Panel widget wraps content in a decorative border with optional header and styling.
When to Use
Use Panel when you need to visually group or highlight content. Common scenarios:
- Status displays: Show system status, alerts, or important messages
- Section headers: Group related information with a titled border
- Emphasized content: Draw attention to specific output
For tabular data with rows and columns, use Table instead. For side-by-side content, use Columns.
Basic Usage
Create a panel by passing text or any renderable content.
public static void BasicPanelExample()
{
var panel = new Panel("Hello, World!");
AnsiConsole.Write(panel);
}
Headers
Add a title to identify the panel's content.
public static void PanelHeaderExample()
{
var panel = new Panel("This panel has a header")
.Header("Information");
AnsiConsole.Write(panel);
}
Header Alignment
Position headers on the left, center, or right of the panel border.
public static void PanelHeaderAlignmentExample()
{
var left = new Panel("Left-aligned header")
.Header("Left", Justify.Left);
var center = new Panel("Center-aligned header")
.Header("Center", Justify.Center);
var right = new Panel("Right-aligned header")
.Header("Right", Justify.Right);
AnsiConsole.Write(left);
AnsiConsole.Write(center);
AnsiConsole.Write(right);
}
Borders
Border Styles
Choose a border style to match your application's visual tone.
public static void PanelBorderStylesExample()
{
var square = new Panel("Square border (default)")
.SquareBorder();
var rounded = new Panel("Rounded border")
.RoundedBorder();
var heavy = new Panel("Heavy border")
.HeavyBorder();
var doubleBorder = new Panel("Double border")
.DoubleBorder();
AnsiConsole.Write(square);
AnsiConsole.Write(rounded);
AnsiConsole.Write(heavy);
AnsiConsole.Write(doubleBorder);
}
Note
Removing the Border
Use NoBorder() when you want padding and headers without a visible border.
public static void PanelNoBorderExample()
{
var panel = new Panel("Content without a border")
.NoBorder()
.Header("Still has a header");
AnsiConsole.Write(panel);
}
Border Color
Apply color to make panels stand out or convey meaning.
public static void PanelBorderColorExample()
{
var panel = new Panel("Colored border")
.Header("Status")
.BorderColor(Color.Green);
AnsiConsole.Write(panel);
}
Layout
Padding
Control the space between content and the border.
public static void PanelPaddingExample()
{
var minimal = new Panel("Minimal padding")
.Padding(0, 0);
var spacious = new Panel("Extra padding")
.Padding(4, 2);
AnsiConsole.Write(minimal);
AnsiConsole.Write(spacious);
}
Expanding to Fill Width
By default, panels fit their content. Use Expand() to fill the available width.
public static void PanelExpandExample()
{
var collapsed = new Panel("Fits content width");
var expanded = new Panel("Fills available width")
.Expand();
AnsiConsole.Write(collapsed);
AnsiConsole.Write(expanded);
}
Fixed Width
Set an explicit width when you need consistent sizing.
public static void PanelWidthExample()
{
var panel = new Panel("Fixed width panel")
{
Width = 40
};
AnsiConsole.Write(panel);
}
Nesting Content
Nested Panels
Create visual hierarchy by placing panels inside panels.
public static void PanelNestingExample()
{
var inner = new Panel("Inner content")
.Header("Inner")
.RoundedBorder()
.BorderColor(Color.Yellow);
var outer = new Panel(inner)
.Header("Outer")
.DoubleBorder()
.BorderColor(Color.Blue);
AnsiConsole.Write(outer);
}
Tables in Panels
Combine panels with other widgets like tables for structured displays.
public static void PanelWithTableExample()
{
var table = new Table()
.AddColumn("Name")
.AddColumn("Status")
.AddRow("Server 1", "[green]Online[/]")
.AddRow("Server 2", "[red]Offline[/]");
var panel = new Panel(table)
.Header("Server Status")
.BorderColor(Color.Blue);
AnsiConsole.Write(panel);
}
Combining Options
Panels support combining multiple styling options for rich displays.
public static void PanelFullySyledExample()
{
var panel = new Panel("[bold]Important Notice[/]\n\nThis message requires your attention.")
.Header("[yellow]Warning[/]", Justify.Center)
.RoundedBorder()
.BorderColor(Color.Yellow)
.Padding(2, 1)
.Expand();
AnsiConsole.Write(panel);
}
API Reference
A renderable panel.
Constructors
Panel(string text)Initializes a new instance of the class.
Parameters:
text (string)Panel(IRenderable content)Initializes a new instance of the class.
Parameters:
content (IRenderable)Properties
Border
: BoxBorderBorderStyle
: StyleExpand
: boolGets or sets a value indicating whether or not the panel should fit the available space. If false, the panel width will be auto calculated. Defaults to false.
Header
: PanelHeaderGets or sets the header.
Height
: Nullable<int>Gets or sets the height of the panel.
Padding
: Nullable<Padding>Gets or sets the padding.
UseSafeBorder
: boolWidth
: Nullable<int>Gets or sets the width of the panel.
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 .
Panel Header(string text, Nullable<Justify> alignment)Sets the panel header.
Parameters:
text (string)alignment (Nullable<Justify>)Returns:
The same instance so that multiple calls can be chained.
Panel Header(PanelHeader header)Sets the panel header.
Parameters:
header (PanelHeader)Returns:
The same instance so that multiple calls can be chained.
Panel HeaderAlignment(Justify alignment)Sets the panel header alignment.
Parameters:
alignment (Justify)Returns:
The same instance so that multiple calls can be chained.