layout
FastHTML constructors for stack, cluster, grid, and document flow layouts
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:
Stacksetsstackfor a vertical column.Clustersetsclusterfor a wrapping row.Gridsetsgridfor adaptive equal-width columns.Flowsetsflowfor 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.
from fasthtml.common import Article, Header, Section, Ul, Li, to_xml
from answerui.demo import demo
Stack
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 prepends those custom properties to any style string the caller passes:
Stack
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:
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:
to_xml(Stack(P('Ada Lovelace'), Small('Workspace owner'), gap='var(--space-2)'))
'<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:
demo(Section(H2('Profile'), P('Visible to the rest of your team.'), stack=True))
Cluster
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
def Cluster(
*c, gap=None, **attrs
):
A row of naturally sized children that wraps to fit
Cluster.demo()
Inside a narrow container, the same cluster wraps without a media breakpoint even on a wide viewport:
demo(Div(_cluster_example(), style='max-inline-size: 14rem; border: 1px dashed var(--n-1)'))
Pass grow=True on one child in a 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:
demo(Cluster(Code('FastHTML'), Stack(P('Ada Lovelace'), Small('Owner'), gap='0', grow=True), Code('Edit'), Code('Remove')))
Grid
Unlike Cluster, 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
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.
Grid.demo()
grow=True also works inside a 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:
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:
demo(Ul(Li('Ada Lovelace'), Li('Grace Hopper'), Li('Alan Turing'), stack=True, cls='gap-2'))
Flow
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, Cluster, and Grid. You can nest a Flow with a smaller gap without changing the outer Flow's spacing. For full articles that also style lists, blockquotes, code, and tables, use prose=True.
Flow
def Flow(
*c, gap=None, **attrs
):
Document flow with closer spacing after headings
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.
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 sets a 2rem gap around the nested Flow. The inner Flow uses a .25rem gap between its two paragraphs:
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.