Ratatui Widgets: Components, Styling and Practical Examples
Tour the built-in widget catalog, learn how styles, spans and blocks compose, and pick the right widget for each part of your interface.
Learn how the constraint solver divides space, when to use each constraint and how to build responsive screens that survive resizing.

Every Ratatui interface starts with a question: how should the available terminal space be divided? The layout system answers it with rectangles and constraints. You describe what each area needs, such as "exactly three rows", "a quarter of the width" or "whatever is left", and Ratatui computes positions that satisfy those requirements as well as possible. Because the layout is recalculated on every frame, a well-designed one adapts automatically when the user resizes the window. This guide explains rectangles, each constraint type, nesting, spacing, flex alignment and responsive patterns, using code compiled against Ratatui 0.30.2.
A Rect is four numbers: x, y, width and height, measured in terminal cells from the top-left corner. Everything in Ratatui renders into a Rect. frame.area() returns the rectangle for the whole terminal, and layouts split one rectangle into several smaller ones.
Rectangles also have helpful methods. inner(Margin::new(1, 1)) shrinks a rectangle on all sides, intersection clips one rectangle to another, and contains(Position) tests whether a point (for example a mouse click) falls inside it. A Block can compute its own inner area with block.inner(area), which accounts for borders and padding.
Create a layout with Layout::vertical (areas stacked top to bottom) or Layout::horizontal (areas side by side), passing one constraint per area. Calling .areas(rect) returns a fixed-size array you can destructure, which documents intent and lets the compiler verify the count. If the number of areas is dynamic, use .split(rect), which returns a slice.
let [header, body, footer] = Layout::vertical([
Constraint::Length(3),
Constraint::Min(5),
Constraint::Length(1),
])
.areas(area);
let [sidebar, content] =
Layout::horizontal([Constraint::Percentage(25), Constraint::Fill(1)]).areas(body);
frame.render_widget(Block::bordered().title(" Header "), header);
frame.render_widget(Block::bordered().title(" Sidebar "), sidebar);
frame.render_widget(Block::bordered().title(" Content "), content);
frame.render_widget(Paragraph::new("status line"), footer);Each constraint expresses a different kind of requirement. Mixing them is how you get layouts that feel natural at every size.
Length(n) requests exactly n cells. Use it for elements with a known size: a one-line status bar, a three-row header with borders, a fixed-width line-number gutter.
Percentage(p) and Ratio(a, b) request a share of the available space. Percentage(25) and Ratio(1, 4) are equivalent; ratios are handy for thirds, which percentages cannot express exactly.
Min(n) guarantees at least n cells but can grow; Max(n) allows at most n cells but can shrink. They are bounds rather than targets, which makes them useful for keeping areas readable on small screens without letting them dominate large ones.
Fill(weight) takes whatever space the other constraints leave, shared among all Fill areas by weight. Fill(2) next to Fill(1) receives twice as much. For "the rest of the screen", Fill(1) states intent more clearly than Min(0).
| Constraint | Requests | Typical use |
|---|---|---|
Length(n) | Exactly n cells | Headers, status bars, gutters |
Percentage(p) | p% of the space | Proportional sidebars |
Ratio(a, b) | a/b of the space | Exact thirds and fifths |
Min(n) | At least n cells | Content that must stay readable |
Max(n) | At most n cells | Panels that should not sprawl |
Fill(w) | Leftover space by weight | Main content areas |
Constraints can conflict. Three Length(40) areas cannot fit in an 80-column terminal, and a Min(30) next to a Percentage(80) may be impossible on a narrow screen. Ratatui uses a constraint solver that assigns priorities and finds the closest solution rather than failing. In practice, fixed lengths and minimums are honored before proportional shares, and fill areas absorb the remainder. When an exact result matters, test your layout at the smallest size you support, and prefer flexible constraints (Fill, Min) for the parts that can adapt.
Solving is cheap but not free, so Ratatui caches layout results by default through the layout-cache feature. Recomputing the same layout every frame costs almost nothing after the first time.
Three modifiers polish a layout. .spacing(n) inserts n empty cells between areas, which looks cleaner than doubling borders. .margin(n) adds empty space around the whole layout, and .horizontal_margin() and .vertical_margin() control each axis separately. .flex(...) decides what happens to leftover space when the constraints do not fill the area: Flex::Start packs areas at the beginning, Flex::End at the end, Flex::Center in the middle, and Flex::SpaceBetween and Flex::SpaceAround distribute gaps between and around them.
Centering is the most common use of flex. Splitting vertically with a single Length and Flex::Center, then horizontally the same way, yields a centered rectangle, the standard building block for popups and dialogs.
Complex screens are built by splitting areas that came from earlier splits. A dashboard might divide the screen vertically into a header and body, the body horizontally into a sidebar and main area, and the main area vertically again into a chart and a table. Each level only worries about its own children, which keeps the code readable. Moving each level into its own function that takes a Rect makes it easier still, because the function can be reused or rearranged without touching the rest of the layout.
Because you compute the layout inside draw, you can branch on the current size. The program below places navigation beside the content when the terminal is at least 80 columns wide and stacks it on top when it is narrower. It also includes a reusable centered helper built with Flex::Center:
use ratatui::layout::{Constraint, Flex, Layout, Rect};
use ratatui::widgets::{Block, Clear, Paragraph};
use ratatui::Frame;
fn ui(frame: &mut Frame) {
let area = frame.area();
let [header, body, footer] = Layout::vertical([
Constraint::Length(3),
Constraint::Fill(1),
Constraint::Length(1),
])
.areas(area);
// Responsive body: side by side when wide, stacked when narrow.
let direction_wide = body.width >= 80;
let [nav, main] = if direction_wide {
Layout::horizontal([Constraint::Length(24), Constraint::Fill(1)])
.spacing(1)
.areas(body)
} else {
Layout::vertical([Constraint::Length(5), Constraint::Fill(1)]).areas(body)
};
frame.render_widget(Block::bordered().title(" App "), header);
frame.render_widget(Block::bordered().title(" Navigation "), nav);
frame.render_widget(Block::bordered().title(" Content "), main);
frame.render_widget(Paragraph::new("q: quit ?: help"), footer);
// Very small terminals get a centered hint on top of everything.
if area.height < 12 {
let hint = centered(area, 30, 3);
frame.render_widget(Clear, hint);
frame.render_widget(Paragraph::new("Enlarge the terminal").block(Block::bordered()), hint);
}
}
/// A centered rectangle, useful for popups.
fn centered(area: Rect, width: u16, height: u16) -> Rect {
let [row] = Layout::vertical([Constraint::Length(height)]).flex(Flex::Center).areas(area);
let [cell] = Layout::horizontal([Constraint::Length(width)]).flex(Flex::Center).areas(row);
cell
}
fn main() -> std::io::Result<()> {
ratatui::run(|terminal| {
terminal.draw(ui)?;
ratatui::crossterm::event::read()?;
Ok(())
})
}Other responsive techniques include hiding secondary panels below a threshold, switching a table to fewer columns, and showing a friendly "terminal too small" message when the area cannot fit the minimum layout. Always handle very small sizes gracefully: users resize windows in unexpected ways, and a tiny pane in a tiling window manager is common.

When a layout does not look the way you expect, make the rectangles visible. Temporarily wrap every area in a bordered Block whose title shows its size, for example format!(" {}x{} ", area.width, area.height). Seeing the actual dimensions at different terminal sizes reveals which constraint is winning and where space is going. You can also log the computed rectangles to a file, or write a TestBackend test that renders the layout at a fixed size and asserts on the result, which keeps the layout from regressing later.
The default macros feature adds helpers from the ratatui-macros crate, such as constraints!, which turns a compact list into constraint values, along with line!, span! and text! for styled text. They are optional; the explicit constructors used in this article are clearer for beginners and work everywhere.
Length for the header and footer and Fill(1) for the body is the starting point for almost every app.Fill detail pane on the right suits file managers, mail clients and database browsers.Fill weights, then split each row horizontally. Equal weights produce a clean grid; unequal weights emphasize key panels.Clear and render the dialog on top.Fill for the left message and Length segments for mode, position and time.Most real interfaces are combinations of these patterns, nested two or three levels deep.
Rect values by hand breaks as soon as the terminal size changes. Derive every area from frame.area().draw causes overlap after a resize. Recompute each frame; the cache makes it cheap.Length(1) area with a border has no room for content. Use Length(3) for a single bordered line.Fill for the flexible part.Both take leftover space, but Fill states that intent explicitly and supports weights, so several fill areas can share space proportionally.
Use .areas() when the number of areas is known at compile time; it returns an array you can destructure. Use .split() when the count is dynamic, such as one column per item.
Split vertically with one Length constraint and Flex::Center, then split the result horizontally the same way. Render Clear first so the popup erases content beneath it.
Terminal cells are whole numbers, so fractional results are rounded. The solver distributes remainders, which can make areas differ by one cell.
No. Layout results are cached by the default layout-cache feature, so repeated identical layouts are almost free.
Ratatui's layout system turns a terminal into a grid you can reason about: rectangles describe space, constraints describe needs and the solver reconciles them. Combine fixed Length areas with flexible Fill and Min areas, use spacing and flex alignment for polish, nest layouts for complex screens and branch on size for responsive behavior. For the complete API, see the layout module on docs.rs. Then put these skills to work in our real-time dashboard tutorial or explore more patterns on the examples page.