> ## Documentation Index
> Fetch the complete documentation index at: https://docs.nemu.cc/llms.txt
> Use this file to discover all available pages before exploring further.

# Layout

> Group elements with flow and flex containers in the Document API

A container groups children and arranges them with a flow or flex layout. Build one with
`page.create_container`, add children with `container.add`, then add the container to the
page. Children flow or flex inside the container's padded area.

## Container layout

```ts theme={null}
type ContainerLayout =
  | { type: "flex"; direction: "row" | "column"; justify?: FlexJustify; align?: FlexAlign; gap?: number }
  | { type: "flow"; gap?: number };
```

A `flow` layout stacks children vertically with an optional `gap`. A `flex` layout
distributes children along a `row` or `column`. Containers also accept `style`, `width`,
`height`, `position`, and `classname`.

## Flow

Flow is the simplest layout. Children stack from top to bottom, separated by `gap`.

```ts theme={null}
const stack = page.create_container({
  layout: { type: "flow", gap: 8 },
  width: page.get_content_width(),
});
stack.add(
  page.text({ content: "First line" }),
  page.text({ content: "Second line" }),
  page.text({ content: "Third line" }),
);
page.add(stack);
```

## Flex

Flex distributes children along a row or column and controls how spare space is shared.

```ts theme={null}
const row = page.create_container({
  layout: { type: "flex", direction: "row", justify: "space-between", align: "center", gap: 12 },
  width: page.get_content_width(),
});
row.add(
  page.text({ content: "Left" }),
  page.text({ content: "Right" }),
);
page.add(row);
```

### Justify

`justify` controls distribution along the main axis.

<ParamField path="justify" type="FlexJustify">
  One of `flex-start`, `flex-end`, `center`, `space-between`, `space-around`, `space-evenly`.
</ParamField>

### Align

`align` controls placement along the cross axis.

<ParamField path="align" type="FlexAlign">
  One of `flex-start`, `flex-end`, `center`, `stretch`, `baseline`.
</ParamField>

## Adding children

Use `container.add(...elements)` to place children. They flow or flex inside the container's
padded area according to its layout.

<Tip>
  Size a full width container with `page.get_content_width()` so it spans the usable area
  inside the page margins.
</Tip>

## Related pages

<CardGroup cols={2}>
  <Card title="Elements" icon="shapes" href="/packages/pdf/document/elements">
    The text, rectangle, and image primitives you add to a container.
  </Card>

  <Card title="Tables" icon="table" href="/packages/pdf/document/tables">
    Structured rows and columns with the table factory.
  </Card>
</CardGroup>
