---
description: FastHTML constructors for stack, cluster, grid, and document flow layouts
output-file: layout.html
title: layout

---
<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->

Component styles set internal padding. Layout attributes on the parent control spacing between child elements. Each FastHTML constructor returns a `Div` with the matching HTML attribute:

- [`Stack`](layout.md#stack) sets `stack` for a vertical column.
- [`Cluster`](layout.md#cluster) sets `cluster` for a wrapping row.
- [`Grid`](layout.md#grid) sets `grid` for adaptive equal-width columns.
- [`Flow`](layout.md#flow) sets `flow` for document spacing.

Put any of those attributes directly on a semantic element such as `Section(..., stack=True)` to match the same CSS rules without an extra wrapper.

```python
from fasthtml.common import Article, Header, Section, Ul, Li, to_xml
from answerui.demo import demo
```

## Stack

[`Stack`](layout.md#stack) arranges its children in a flex column and stretches them across the available width. Setting `gap` on the parent puts space only between adjacent children, avoiding a trailing margin after the last child. The default gap is `--space`.

Every layout constructor accepts CSS lengths or token references (such as `'var(--space-2)'`) for its sizing options. Layout rules stay in the stylesheet. [`_layout`](layout.md#_layout) prepends those custom properties to any `style` string the caller passes:

---

### Stack

```python
def Stack(
    *c, gap=None, **attrs
):
```

*A vertical group with space between its children*

None of these children has a margin. The `[stack]` container sets the spacing between them:

```python
Stack.demo()
```

For a tighter gap, pass `gap`. The constructor sets it as `--gap` on the same `div`, with no extra wrapper or styling class:

```python
to_xml(Stack(P('Ada Lovelace'), Small('Workspace owner'), gap='var(--space-2)'))
```

```output
'<div stack style="--gap: var(--space-2);">\n  <p>Ada Lovelace</p>\n<small>Workspace owner</small></div>\n'
```

You can also put `stack=True` on a semantic element. Here the `section` keeps its tag while matching the `[stack]` rule with the default `--space` gap:

```python
demo(Section(H2('Profile'), P('Visible to the rest of your team.'), stack=True))
```

## Cluster

[`Cluster`](layout.md#cluster) arranges short items such as tags or metadata in a wrapping flex row. Each child stays at its natural width and aligns centrally on the cross axis. The `--gap` custom property (default `--space`) sets both row and column spacing.

---

### Cluster

```python
def Cluster(
    *c, gap=None, **attrs
):
```

*A row of naturally sized children that wraps to fit*

```python
Cluster.demo()
```

Inside a narrow container, the same cluster wraps without a media breakpoint even on a wide viewport:

```python
demo(Div(_cluster_example(), style='max-inline-size: 14rem; border: 1px dashed var(--n-1)'))
```

Pass `grow=True` on one child in a [`Cluster`](layout.md#cluster) to fill the remaining width and push later items to the far edge. Because `[grow]` also sets `min-inline-size: 0`, a long name beside action controls wraps instead of pushing the actions out of the row:

```python
demo(Cluster(Code('FastHTML'), Stack(P('Ada Lovelace'), Small('Owner'), gap='0', grow=True), Code('Edit'), Code('Remove')))
```

## Grid

Unlike [`Cluster`](layout.md#cluster), [`Grid`](layout.md#grid) places its children in equal-width columns. It fits as many columns as the container allows at a minimum width of `16rem` (`--grid-min`) and distributes the remaining width evenly. When the container is narrower than `--grid-min`, a single column shrinks to fit instead of overflowing. Pass `min_width` to override `--grid-min` without adding media breakpoints.

---

### Grid

```python
def Grid(
    *c, gap=None, min_width=None, **attrs
):
```

*Equal-width columns whose count adapts to the available width*

Each column contains a stack with a smaller gap. Changing the grid's `gap` doesn't change the stacks inside it, because each layout sets its own `--gap`.

```python
Grid.demo()
```

`grow=True` also works inside a [`Stack`](layout.md#stack) to fill the remaining height. In a grid of cards, passing `grow=True` on the card body aligns every card's footer to the bottom regardless of title length:

```python
demo(Grid(Stack(P('A short title'), P('Body', grow=True), Small('Foot')), Stack(P('A title long enough that it wraps onto a second line in a narrow column'), P('Body', grow=True), Small('Foot'))), width='24rem')
```

## Lists

Interface groups such as member lists or link menus can still use semantic `Ul` or `Ol` elements. Putting `stack=True`, `cluster=True`, or `grid=True` on a `ul` or `ol` clears the default list markers and padding (`list-style: none; padding: 0`) so its `li` items lay out like any other container.

A stacked `Ul` of names without default list markers:

```python
demo(Ul(Li('Ada Lovelace'), Li('Grace Hopper'), Li('Alan Turing'), stack=True, cls='gap-2'))
```

## Flow

[`Flow`](layout.md#flow) uses CSS grid with a default row gap of `.5em` (`--flow-space`) for short text sections containing headings and paragraphs. Any heading (`h1` through `h6`) after the first child has an extra `.5em` top margin (`margin-block-start`), putting more space before a heading than after it.

`gap` sets `--flow-space` independently of the `--gap` used by [`Stack`](layout.md#stack), [`Cluster`](layout.md#cluster), and [`Grid`](layout.md#grid). You can nest a [`Flow`](layout.md#flow) with a smaller `gap` without changing the outer [`Flow`](layout.md#flow)'s spacing. For full articles that also style lists, blockquotes, code, and tables, use `prose=True`.

---

### Flow

```python
def Flow(
    *c, gap=None, **attrs
):
```

*Document flow with closer spacing after headings*

```python
Flow.demo()
```

## Composition

The page below uses a stack to space its header, project grid, and help text. The header has its own smaller gap and a cluster of metadata. The article uses flow for its heading and paragraphs. Each layout spaces only its own direct children, whatever layouts its neighbours use.

```python
demo(Section(
    Header(H2('Workspace'), P('A shared place for the team\'s projects.'),
        Cluster(Small('3 projects'), Small('Updated today'), gap='var(--space-2)'),
        stack=True, cls='gap-2'),
    _grid_example(),
    Article(H3('Working together'), P('Pick a project to see its latest work.'),
        P('Use the inbox for requests that have not yet been assigned.'), flow=True),
    stack=True, cls='gap-8'))
```

The outer [`Flow`](layout.md#flow) sets a `2rem` gap around the nested [`Flow`](layout.md#flow). The inner [`Flow`](layout.md#flow) uses a `.25rem` gap between its two paragraphs:

```python
demo(Flow(P('Before the nested flow.'), Flow(P('Inner one.'), P('Inner two.'), gap='.25rem'), P('After it.'), gap='2rem'))
```

To change a layout default across an application, override its custom property in the `app` CSS layer (for example, `[cluster] { --gap: var(--space-2); }`). For a single element, pass a spacing utility class such as `cls='gap-2'` (`.gap-0`, `.gap-1` through `.gap-4`, `.gap-6`, `.gap-8`, `.gap-12`), pass a constructor parameter such as `gap` for custom lengths, or set the custom property in `style`. Use ordinary CSS or the flex utility classes for other adjustments such as centring a cluster with `justify-content`. None of the layout rules sets a background, border, padding, or outer margin.
