# Flex

> CSS flexbox: justify, align, gap, wrap and grow. A native Pyzed component for Python desktop GUIs, with a live preview, code examples and its API.

Source: https://albakore.github.io/pyzed/library/flex/

:::tip[In plain words]
Spreads things along a line: packed together, evenly spaced, or with one that grows.
:::

Lays children out along one axis like CSS flexbox: `justify` on the main axis (`start`, `center`, `end`, `between`, `around`, `evenly`), `align` on the cross axis, `gap` and `wrap`. `pz.flex_item(child, grow=, basis=)` makes a child grow or gives it a base size.

`pz.flex` · Category: [Layout](https://albakore.github.io/pyzed/library/#layout) · [Full reference →](https://albakore.github.io/pyzed/reference/widgets/#flex)

**Code:**

```python
box = lambda text, w=90, h=44: pz.container(ui.label(text), center=True, width=w, height=h, bg=pz.theme.current().element, radius=6)
pz.column([
    pz.flex([box("A"), box("B"), box("C")], justify="between", width=440),
    pz.flex([box("fixed"), pz.flex_item(box("grows", w="fill"), grow=1), box("fixed")], gap=8, width=440),
    pz.flex([box("A"), box("B")], justify="center", gap=8, width=440),
], spacing=14)
```

:::note[New]
This component was added recently. Tell us how it works for you in the [issue tracker](https://github.com/albakore/pyzed/issues).
:::

## When to use

- When `row`/`column` are not enough: even distribution, growing items, wrapping cards.

## Usage

```python
import pyzed as pz
from pyzed import ui

box = lambda text, w=90, h=44: pz.container(ui.label(text), center=True, width=w, height=h, bg=pz.theme.current().element, radius=6)
pz.column([
    pz.flex([box("A"), box("B"), box("C")], justify="between", width=440),
    pz.flex([box("fixed"), pz.flex_item(box("grows", w="fill"), grow=1), box("fixed")], gap=8, width=440),
    pz.flex([box("A"), box("B")], justify="center", gap=8, width=440),
], spacing=14)
```

## Variants

:::note[About these captures]
Each capture is recorded by pyzed itself, off-screen at 2x, in One Dark or One Light to match this site's theme, and cropped to the component so it shows at its real size. The pointer hovers, presses or types into it; a component that does not react to the pointer cycles through a few themes. With reduced motion you see the first frame. Open **Code** under a capture to see the snippet that draws it; free names such as `on_save` stand for your own callbacks.
:::

### Justify

```python
box = lambda text, w=90, h=44: pz.container(ui.label(text), center=True, width=w, height=h, bg=pz.theme.current().element, radius=6)
pz.flex([box("A"), box("B")], justify="start", gap=8, width=360)
```

```python
box = lambda text, w=90, h=44: pz.container(ui.label(text), center=True, width=w, height=h, bg=pz.theme.current().element, radius=6)
pz.flex([box("A"), box("B")], justify="center", gap=8, width=360)
```

```python
box = lambda text, w=90, h=44: pz.container(ui.label(text), center=True, width=w, height=h, bg=pz.theme.current().element, radius=6)
pz.flex([box("A"), box("B")], justify="end", gap=8, width=360)
```

```python
box = lambda text, w=90, h=44: pz.container(ui.label(text), center=True, width=w, height=h, bg=pz.theme.current().element, radius=6)
pz.flex([box("A"), box("B"), box("C")], justify="between", width=360)
```

```python
box = lambda text, w=90, h=44: pz.container(ui.label(text), center=True, width=w, height=h, bg=pz.theme.current().element, radius=6)
pz.flex([box("A"), box("B"), box("C")], justify="around", width=360)
```

```python
box = lambda text, w=90, h=44: pz.container(ui.label(text), center=True, width=w, height=h, bg=pz.theme.current().element, radius=6)
pz.flex([box("A"), box("B"), box("C")], justify="evenly", width=360)
```

### Grow

```python
box = lambda text, w=90, h=44: pz.container(ui.label(text), center=True, width=w, height=h, bg=pz.theme.current().element, radius=6)
pz.flex([box("fixed"), pz.flex_item(box("grows", w="fill"), grow=1), box("fixed")], gap=8, width=360)
```

```python
box = lambda text, w=90, h=44: pz.container(ui.label(text), center=True, width=w, height=h, bg=pz.theme.current().element, radius=6)
pz.flex([box(str(i)) for i in range(1, 7)], gap=8, wrap=True, width=300)
```

## Events

Flex has no events of its own: it shows what you give it. Like every widget it also has the **18 common events** (`click`, `double_click`, `right_click`, `mouse_enter`, `drag`, `key_down`, `resize`…), attached with `.on(...)`: see [Events](https://albakore.github.io/pyzed/guides/events/) and [Events of every component](https://albakore.github.io/pyzed/library/events/).

## Properties and API

### `pz.flex`

```python
flex(
    children=(),
    *,
    direction='row',
    justify='start',
    align='start',
    gap=0,
    wrap=False,
    padding=None,
    width=None,
    height=None,
)
```

Lays children out along one axis, like CSS flexbox, with `justify` on the main axis and `align` on the cross axis. Use `pz.flex_item` to make a child grow or give it a fixed size.

| Property | Default | Description |
| --- | --- | --- |
| `children` | `()` | The widgets, or `pz.flex_item(...)` for children with their own sizing. |
| `direction` | `"row"` | `"row"` or `"column"`. |
| `justify` | `"start"` | `"start"`, `"center"`, `"end"`, `"between"`, `"around"` or `"evenly"`. No effect when a child grows. |
| `align` | `"start"` | Cross-axis alignment: `"start"`, `"center"` or `"end"`. |
| `gap` | `0` | Space between children. |
| `wrap` | `False` | Wraps onto new lines (rows with `justify="start"` only). |
| `padding` | `None` | Space around the children. |
| `width` | `None` | Width. |
| `height` | `None` | Height. |

[Full reference →](https://albakore.github.io/pyzed/reference/widgets/#flex)

### `pz.flex_item`

```python
flex_item(content, *, grow=1, basis=None, align=None)
```

Sizes a child of `pz.flex`.

| Property | Default | Description |
| --- | --- | --- |
| `content` | *required* | The widget. |
| `grow` | `1` | Share of the free space on the main axis (`0` keeps its natural size). |
| `basis` | `None` | A fixed main size in pixels, instead of growing. |
| `align` | `None` | Cross-axis alignment for this child. |

[Full reference →](https://albakore.github.io/pyzed/reference/widgets/#flex_item)

## Related

- [Grid](https://albakore.github.io/pyzed/library/grid/): Equal columns, or CSS-like tracks with spans.
- [Row](https://albakore.github.io/pyzed/library/row/): Lays out children horizontally.
