Skip to content

Alignment and Distribution

A container places its children along two axes. The justify_children_* utilities distribute them along the main axis (across a row, down a col), and the align_children_* utilities place them on the cross axis. A child can override its parent's cross-axis alignment for itself with align_self_*. In a grid, justify_items_* and justify_self_* place items within their cells along the row.

The examples on this page title each panel with this helper, which draws its text over the top border:

def title(content: str) -> Text:
    return Text(content=f" {content} ", style=position_absolute | inset_top(-1) | inset_left(1) | clip_to_border)

Distributing children along the main axis

The justify_children_* utilities share out the free space a row or column has left over after its children take their sizes. Children that grow leave no free space, so these only matter when nothing claims it.

@component
def justify() -> Div:
    return Div(
        style=col,
        children=tuple(
            Div(
                style=row | justify | border,
                children=(
                    title(name),
                    Text(style=border | border_heavy, content="a"),
                    Text(style=border | border_heavy, content="bb"),
                    Text(style=border | border_heavy, content="ccc"),
                ),
            )
            for name, justify in (
                ("justify_children_start", justify_children_start),
                ("justify_children_center", justify_children_center),
                ("justify_children_end", justify_children_end),
                ("justify_children_space_between", justify_children_space_between),
                ("justify_children_space_around", justify_children_space_around),
                ("justify_children_space_evenly", justify_children_space_evenly),
            )
        ),
    )

The justify_children utilities

┌─ justify_children_start ───────────────────────┐
│┏━┓┏━━┓┏━━━┓                                    │
│┃a┃┃bb┃┃ccc┃                                    │
│┗━┛┗━━┛┗━━━┛                                    │
└────────────────────────────────────────────────┘
┌─ justify_children_center ──────────────────────┐
│                  ┏━┓┏━━┓┏━━━┓                  │
│                  ┃a┃┃bb┃┃ccc┃                  │
│                  ┗━┛┗━━┛┗━━━┛                  │
└────────────────────────────────────────────────┘
┌─ justify_children_end ─────────────────────────┐
│                                    ┏━┓┏━━┓┏━━━┓│
│                                    ┃a┃┃bb┃┃ccc┃│
│                                    ┗━┛┗━━┛┗━━━┛│
└────────────────────────────────────────────────┘
┌─ justify_children_space_between ───────────────┐
│┏━┓                  ┏━━┓                  ┏━━━┓│
│┃a┃                  ┃bb┃                  ┃ccc┃│
│┗━┛                  ┗━━┛                  ┗━━━┛│
└────────────────────────────────────────────────┘
┌─ justify_children_space_around ────────────────┐
│      ┏━┓            ┏━━┓            ┏━━━┓      │
│      ┃a┃            ┃bb┃            ┃ccc┃      │
│      ┗━┛            ┗━━┛            ┗━━━┛      │
└────────────────────────────────────────────────┘
┌─ justify_children_space_evenly ────────────────┐
│         ┏━┓         ┏━━┓         ┏━━━┓         │
│         ┃a┃         ┃bb┃         ┃ccc┃         │
│         ┗━┛         ┗━━┛         ┗━━━┛         │
└────────────────────────────────────────────────┘

Aligning children on the cross axis

The default, align_children_stretch, fills the cross axis; the others keep each child at its content size and place it.

@component
def align() -> Div:
    return Div(
        style=row,
        children=tuple(
            Div(
                style=row | align | fill(1) | border,
                children=(
                    title(name),
                    Text(style=border | border_heavy, content="a"),
                    Text(style=border | border_heavy, content="b\nb"),
                    Text(style=border | border_heavy, content="c\nc\nc"),
                ),
            )
            for name, align in (
                ("align_children_start", align_children_start),
                ("align_children_center", align_children_center),
                ("align_children_end", align_children_end),
                ("align_children_stretch", align_children_stretch),
            )
        ),
    )

The align_children utilities

┌─ align_children_start ─────┐┌─ align_children_center ────┐┌─ align_children_end ───────┐┌─ align_children_stretch ───┐
│┏━┓┏━┓┏━┓                   ││                            ││                            ││┏━┓┏━┓┏━┓                   │
│┃a┃┃b┃┃c┃                   ││      ┏━┓                   ││                            ││┃a┃┃b┃┃c┃                   │
│┗━┛┃b┃┃c┃                   ││┏━┓┏━┓┃c┃                   ││      ┏━┓                   ││┃ ┃┃b┃┃c┃                   │
│   ┗━┛┃c┃                   ││┃a┃┃b┃┃c┃                   ││   ┏━┓┃c┃                   ││┃ ┃┃ ┃┃c┃                   │
│      ┗━┛                   ││┗━┛┃b┃┃c┃                   ││┏━┓┃b┃┃c┃                   ││┃ ┃┃ ┃┃ ┃                   │
│                            ││   ┗━┛┗━┛                   ││┃a┃┃b┃┃c┃                   ││┃ ┃┃ ┃┃ ┃                   │
│                            ││                            ││┗━┛┗━┛┗━┛                   ││┗━┛┗━┛┗━┛                   │
└────────────────────────────┘└────────────────────────────┘└────────────────────────────┘└────────────────────────────┘

Aligning one child

align_self_* on a child replaces its parent's align_children_* for that child alone.

@component
def align_self() -> Div:
    return Div(
        style=row | align_children_start | gap(1) | border,
        children=(
            title("row | align_children_start"),
            Text(style=border | border_heavy, content="no align_self"),
            Text(style=align_self_center | border | border_heavy, content="align_self_center"),
            Text(style=align_self_end | border | border_heavy, content="align_self_end"),
            Text(style=align_self_stretch | border | border_heavy, content="align_self_stretch"),
        ),
    )

The align_self utilities

┌─ row | align_children_start ─────────────────────────────────────────────────┐
│┏━━━━━━━━━━━━━┓                                      ┏━━━━━━━━━━━━━━━━━━┓     │
│┃no align_self┃                                      ┃align_self_stretch┃     │
│┗━━━━━━━━━━━━━┛ ┏━━━━━━━━━━━━━━━━━┓                  ┃                  ┃     │
│                ┃align_self_center┃                  ┃                  ┃     │
│                ┗━━━━━━━━━━━━━━━━━┛ ┏━━━━━━━━━━━━━━┓ ┃                  ┃     │
│                                    ┃align_self_end┃ ┃                  ┃     │
│                                    ┗━━━━━━━━━━━━━━┛ ┗━━━━━━━━━━━━━━━━━━┛     │
└──────────────────────────────────────────────────────────────────────────────┘

Reversing the main axis

row_reverse and col_reverse lay children out right to left and bottom to top, as CSS's flex-direction: row-reverse and column-reverse do, so the first child ends up last on screen without reordering the children themselves. They reverse the main axis too, so with no justify_children_* the children pack against the main axis's new start, the right edge or the bottom.

justify_children_start and justify_children_end are CSS's start and end, not flex-start and flex-end: they name the container's own edges, left and top or right and bottom, whichever way its children run. So row_reverse | justify_children_start keeps the reversed order and packs it against the left edge, as a toolbar whose newest item goes first would.

@component
def reverse() -> Div:
    def items() -> tuple[Text, ...]:
        return tuple(Text(style=border | border_heavy, content=content) for content in ("1", "22", "333"))

    return Div(
        style=col,
        children=(
            *(
                Div(style=direction | border, children=(title(name), *items()))
                for name, direction in (
                    ("row", row),
                    ("row_reverse", row_reverse),
                    ("row_reverse | justify_children_start", row_reverse | justify_children_start),
                )
            ),
            Div(
                style=row,
                children=tuple(
                    Div(
                        style=direction | align_children_start | height(13) | fill(1) | border,
                        children=(title(name), *items()),
                    )
                    for name, direction in (
                        ("col", col),
                        ("col_reverse", col_reverse),
                        ("… | justify_children_start", col_reverse | justify_children_start),
                    )
                ),
            ),
        ),
    )

Rows and columns in order, reversed, and reversed and packed against the start edge

┌─ row ────────────────────────────────────────────────────────────────────────────────────────┐
│┏━┓┏━━┓┏━━━┓                                                                                  │
│┃1┃┃22┃┃333┃                                                                                  │
│┗━┛┗━━┛┗━━━┛                                                                                  │
└──────────────────────────────────────────────────────────────────────────────────────────────┘
┌─ row_reverse ────────────────────────────────────────────────────────────────────────────────┐
│                                                                                  ┏━━━┓┏━━┓┏━┓│
│                                                                                  ┃333┃┃22┃┃1┃│
│                                                                                  ┗━━━┛┗━━┛┗━┛│
└──────────────────────────────────────────────────────────────────────────────────────────────┘
┌─ row_reverse | justify_children_start ───────────────────────────────────────────────────────┐
│┏━━━┓┏━━┓┏━┓                                                                                  │
│┃333┃┃22┃┃1┃                                                                                  │
│┗━━━┛┗━━┛┗━┛                                                                                  │
└──────────────────────────────────────────────────────────────────────────────────────────────┘
┌─ col ────────────────────────┐┌─ col_reverse ────────────────┐┌─ … | justify_children_start ─┐
│┏━┓                           ││                              ││┏━━━┓                         │
│┃1┃                           ││                              ││┃333┃                         │
│┗━┛                           ││┏━━━┓                         ││┗━━━┛                         │
│┏━━┓                          ││┃333┃                         ││┏━━┓                          │
│┃22┃                          ││┗━━━┛                         ││┃22┃                          │
│┗━━┛                          ││┏━━┓                          ││┗━━┛                          │
│┏━━━┓                         ││┃22┃                          ││┏━┓                           │
│┃333┃                         ││┗━━┛                          ││┃1┃                           │
│┗━━━┛                         ││┏━┓                           ││┗━┛                           │
│                              ││┃1┃                           ││                              │
│                              ││┗━┛                           ││                              │
└──────────────────────────────┘└──────────────────────────────┘└──────────────────────────────┘

Centering a box

Center a box on both axes with center_children, which is justify_children_center | align_children_center. It works in a grid too: there it centers the grid's tracks, and a grid with no template sizes its one track to fit its lone child. To center a box over other content rather than among it, see A dialog centered over the app.

@component
def center() -> Div:
    return Div(
        style=row,
        children=(
            Div(
                style=row | center_children | fill(1) | border,
                children=(title("flexbox"), Text(style=border | border_heavy, content="centered")),
            ),
            Div(
                style=display_grid | center_children | fill(1) | border,
                children=(title("grid"), Text(style=border | border_heavy, content="centered")),
            ),
        ),
    )

Centering with flexbox and grid

┌─ flexbox ─────────────┐┌─ grid ────────────────┐
│                       ││                       │
│                       ││                       │
│       ┏━━━━━━━━┓      ││       ┏━━━━━━━━┓      │
│       ┃centered┃      ││       ┃centered┃      │
│       ┗━━━━━━━━┛      ││       ┗━━━━━━━━┛      │
│                       ││                       │
│                       ││                       │
└───────────────────────┘└───────────────────────┘

Aligning grid items in their cells

In a grid, each item sits in a cell, which can be wider than the item. justify_items_* on the grid places every item within its cell along the row, as CSS's justify-items does, and align_children_* places them down the column, as on a flex container. The default, justify_items_stretch, fills the cell's width; the others keep each item at its content width and place it. Flexbox has no cells, so these do nothing there, as in CSS.

@component
def grid_items() -> Div:
    return Div(
        style=row,
        children=tuple(
            Div(
                style=display_grid | grid_template_columns(fr(1)) | justify | fill(1) | border,
                children=(
                    title(name),
                    Text(style=border | border_heavy, content="a"),
                    Text(style=border | border_heavy, content="bb"),
                    Text(style=border | border_heavy, content="ccc"),
                ),
            )
            for name, justify in (
                ("justify_items_start", justify_items_start),
                ("justify_items_center", justify_items_center),
                ("justify_items_end", justify_items_end),
                ("justify_items_stretch", justify_items_stretch),
            )
        ),
    )

The justify_items utilities on a one-column grid

┌─ justify_items_start ─┐┌─ justify_items_center ┐┌─ justify_items_end ───┐┌─ justify_items_stretch 
│┏━┓                    ││          ┏━┓          ││                    ┏━┓││┏━━━━━━━━━━━━━━━━━━━━━┓│
│┃a┃                    ││          ┃a┃          ││                    ┃a┃││┃a                    ┃│
│┗━┛                    ││          ┗━┛          ││                    ┗━┛││┗━━━━━━━━━━━━━━━━━━━━━┛│
│┏━━┓                   ││          ┏━━┓         ││                   ┏━━┓││┏━━━━━━━━━━━━━━━━━━━━━┓│
│┃bb┃                   ││          ┃bb┃         ││                   ┃bb┃││┃bb                   ┃│
│┗━━┛                   ││          ┗━━┛         ││                   ┗━━┛││┗━━━━━━━━━━━━━━━━━━━━━┛│
│┏━━━┓                  ││         ┏━━━┓         ││                  ┏━━━┓││┏━━━━━━━━━━━━━━━━━━━━━┓│
│┃ccc┃                  ││         ┃ccc┃         ││                  ┃ccc┃││┃ccc                  ┃│
│┗━━━┛                  ││         ┗━━━┛         ││                  ┗━━━┛││┗━━━━━━━━━━━━━━━━━━━━━┛│
└───────────────────────┘└───────────────────────┘└───────────────────────┘└───────────────────────┘

justify_self_* on an item replaces its grid's justify_items_* for that item alone, as align_self_* does on the other axis:

@component
def grid_self() -> Div:
    return Div(
        style=display_grid | grid_template_columns(fr(1), fr(1)) | justify_items_start | gap_width(1) | border,
        children=(
            title("display_grid | justify_items_start"),
            Text(style=border | border_heavy, content="no justify_self"),
            Text(style=justify_self_center | border | border_heavy, content="justify_self_center"),
            Text(style=justify_self_end | border | border_heavy, content="justify_self_end"),
            Text(style=justify_self_stretch | border | border_heavy, content="justify_self_stretch"),
        ),
    )

The justify_self utilities in a two-column grid

┌─ display_grid | justify_items_start ─────────────────────┐
│┏━━━━━━━━━━━━━━━┓                ┏━━━━━━━━━━━━━━━━━━━┓    │
│┃no justify_self┃                ┃justify_self_center┃    │
│┗━━━━━━━━━━━━━━━┛                ┗━━━━━━━━━━━━━━━━━━━┛    │
│           ┏━━━━━━━━━━━━━━━━┓ ┏━━━━━━━━━━━━━━━━━━━━━━━━━━┓│
│           ┃justify_self_end┃ ┃justify_self_stretch      ┃│
│           ┗━━━━━━━━━━━━━━━━┛ ┗━━━━━━━━━━━━━━━━━━━━━━━━━━┛│
└──────────────────────────────────────────────────────────┘

Content larger than its container

The *_center and *_end utilities are CSS's safe alignments: content too large for its container starts at the container's start edge and overflows only past the end, so its beginning stays in view. Each has an *_unsafe counterpart with CSS's default behavior, which centers or end-aligns the content anyway and overflows past the start edge too. These containers are fill(1) items, which clip what overflows them, so the unsafe one cuts off the content's beginning as well as its end.

@component
def overflow() -> Div:
    return Div(
        style=row,
        children=tuple(
            Div(
                style=col | justify | fill(1) | border,
                children=(title(name), Text(content=TALL)),
            )
            for name, justify in (
                ("justify_children_center", justify_children_center),
                ("justify_children_center_unsafe", justify_children_center_unsafe),
            )
        ),
    )

Safe and unsafe centering of content taller than its container

┌─ justify_children_center ───────┐┌─ justify_children_center_unsafe ┐
│line 1                           ││line 3                           │
│line 2                           ││line 4                           │
│line 3                           ││line 5                           │
│line 4                           ││line 6                           │
│line 5                           ││line 7                           │
│line 6                           ││line 8                           │
│line 7                           ││line 9                           │
└─────────────────────────────────┘└─────────────────────────────────┘