Skip to content

Layering

Most boxes sit where layout puts them, side by side. Positioning moves a box off that spot, so it can overlap others, and z decides which of the overlapping boxes is drawn on top.

Relative positioning

position_relative with insets moves a box from where layout placed it, without moving anything else: its siblings keep the space it would have taken. Insets are measured in cells, and inset_top(2) moves the box down two rows.

@component
def relative() -> Div:
    return Div(
        style=row | align_children_start | gap(3),
        children=(
            Text(style=border, content="first"),
            Text(
                style=position_relative | inset_top(2) | inset_left(2) | border | border_heavy,
                content="position_relative\ninset_top(2)\ninset_left(2)",
            ),
            Text(style=border, content="third"),
        ),
    )

Relative positioning

┌─────┐                         ┌─────┐
│first│                         │third│
└─────┘     ┏━━━━━━━━━━━━━━━━━┓ └─────┘
            ┃position_relative┃        
            ┃inset_top(2)     ┃        
            ┃inset_left(2)    ┃        
            ┗━━━━━━━━━━━━━━━━━┛        

Absolute positioning

position_absolute takes a box out of the flow: its siblings are laid out as if it weren't there, and its insets place it relative to its parent's padding box.

@component
def absolute() -> Div:
    return Div(
        style=col | border,
        children=(
            Text(content="first, in flow"),
            Text(
                style=position_absolute | inset_top(2) | inset_left(10) | border | border_heavy,
                content="position_absolute\ninset_top(2)\ninset_left(10)",
            ),
            Text(content="second, in flow"),
        ),
    )

Absolute positioning

┌────────────────────────────────────────────────┐
│first, in flow                                  │
│second, in flow                                 │
│          ┏━━━━━━━━━━━━━━━━━┓                   │
│          ┃position_absolute┃                   │
│          ┃inset_top(2)     ┃                   │
│          ┃inset_left(10)   ┃                   │
│          ┗━━━━━━━━━━━━━━━━━┛                   │
└────────────────────────────────────────────────┘

Placing a box at an edge or corner

The inset_* utilities position a box absolutely at one of nine places in its parent. Their center axes leave the insets on that axis unset, which leaves the box where its parent's alignment puts it, so they center only in a parent that centers its children, as this one does. To center a box whatever its parent's alignment, see A dialog centered over the app. Margins push the box away from where it was placed.

@component
def root() -> Div:
    return Div(
        style=col,
        children=(
            Div(
                style=row | fill(1) | border | border_heavy | center_children,
                children=(
                    Text(
                        style=position_absolute | inset_left(3) | inset_top(3),
                        content="position_absolute | inset_left(3) | inset_top(3)",
                    ),
                    Text(
                        style=inset_center_center | margin_left(-2) | margin_top(-4),
                        content="inset_center_center | margin_left(-2) | margin_top(-4)",
                    ),
                    Text(
                        style=inset_bottom_right | margin_bottom(4),
                        content="inset_bottom_right | margin_bottom(4)",
                    ),
                    Text(
                        style=inset_top_left,
                        content="inset_top_left",
                    ),
                    Text(
                        style=inset_top_center,
                        content="inset_top_center",
                    ),
                    Text(
                        style=inset_top_right,
                        content="inset_top_right",
                    ),
                    Text(
                        style=inset_center_left,
                        content="inset_center_left",
                    ),
                    Text(
                        style=inset_center_center,
                        content="inset_center_center",
                    ),
                    Text(
                        style=inset_center_right,
                        content="inset_center_right",
                    ),
                    Text(
                        style=inset_bottom_left,
                        content="inset_bottom_left",
                    ),
                    Text(
                        style=inset_bottom_center,
                        content="inset_bottom_center",
                    ),
                    Text(
                        style=inset_bottom_right,
                        content="inset_bottom_right",
                    ),
                ),
            ),
        ),
    )

The inset utilities

┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃inset_top_left       inset_top_center      inset_top_right┃
┃                                                          ┃
┃                                                          ┃
┃   position_absolute | inset_left(3) | inset_top(3)       ┃
┃                                                          ┃
┃                                                          ┃
┃                                                          ┃
┃                                                          ┃
┃                                                          ┃
┃ inset_center_center | margin_left(-2) | margin_top(-4)   ┃
┃                                                          ┃
┃inset_center_left   inset_center_center inset_center_right┃
┃                                                          ┃
┃                                                          ┃
┃                                                          ┃
┃                                                          ┃
┃                                                          ┃
┃                                                          ┃
┃                     inset_bottom_right | margin_bottom(4)┃
┃                                                          ┃
┃                                                          ┃
┃                                                          ┃
┃inset_bottom_left   inset_bottom_center inset_bottom_right┃
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛

Controlling overlap with z

Where boxes overlap, the one with the higher z is drawn on top. Boxes with equal z are drawn in the order they appear in the tree, each parent before its children, and a box that sets no z counts as 0.

@component
def root() -> Div:
    return Div(
        style=col,
        children=(
            Text(
                style=z(1)
                | position_absolute
                | inset_left(6)
                | inset_top(6)
                | border
                | border_lightrounded
                | margin(1)
                | margin_color("purple", 600),
                content="z = +1",
            ),
            Text(
                style=z(0)
                | position_absolute
                | inset_left(4)
                | inset_top(3)
                | border
                | border_lightrounded
                | margin(1)
                | margin_color("teal", 600),
                content="z =  0",
            ),
            Text(
                style=z(-1)
                | position_absolute
                | inset_left(0)
                | inset_top(0)
                | border
                | border_lightrounded
                | margin(1)
                | margin_color("red", 600),
                content="z = -1",
            ),
            Text(
                style=z(2)
                | position_absolute
                | inset_left(13)
                | inset_top(3)
                | border
                | border_lightrounded
                | margin(1)
                | margin_color("amber", 600),
                content="z = +2",
            ),
        ),
    )

Z layers

 ╭──────╮               
 │z = -1│               
 ╰──                    
     ╭──────╮ ╭──────╮  
     │z =  0│ │z = +2│  
     ╰        ╰──────╯  
       ╭─────           
       │z = +1│         
       ╰──────╯         

Stacking contexts

z only orders boxes within a stacking context, as CSS's z-index does. A box that sets z, z(0) included, forms one: it and everything inside it are drawn together, as one layer at its z, and the z of a box inside it orders that box only among the others inside it. A box that sets no z forms none, so the boxes inside it are layered alongside it, among the boxes of the nearest stacking context around it. The app's root always forms one.

So a box is always drawn beneath the boxes inside it, whatever z they set. A negative z puts a box beneath the other boxes of its stacking context, but still over the box that forms the context.

A z(1) dropdown inside a toolbar that sets no z joins the stacking context around the toolbar, so it's drawn over the content below the toolbar:

def menu_bar(toolbar_style: Style) -> Div:
    return Div(
        style=col,
        children=(
            Div(
                style=toolbar_style | row | gap(2) | border | pad_x(1),
                children=(
                    Text(content="file"),
                    Text(content="edit"),
                    Text(
                        style=z(1) | position_absolute | inset_top(2) | inset_left(0) | border | border_heavy,
                        content="new\nopen\nsave\nquit",
                    ),
                ),
            ),
            Text(style=border, content="The page's content"),
        ),
    )


@component
def dropdown_escapes() -> Div:
    return menu_bar(toolbar_style=Style())


@component
def dropdown_scoped() -> Div:
    return menu_bar(toolbar_style=z(0))

A dropdown over the page

┌────────────────────────────┐
│ file  edit                 │
└────────────────────────────┘
┌┏━━━━┓──────────────────────┐
│┃new ┃ge's content          │
└┃open┃──────────────────────┘
 ┃save┃                       
 ┃quit┃                       
 ┗━━━━┛                       

A dropdown beneath the page

┌────────────────────────────┐
│ file  edit                 │
└────────────────────────────┘
┌────────────────────────────┐
│The page's content          │
└────────────────────────────┘
 ┃save┃                       
 ┃quit┃                       
 ┗━━━━┛                       

Give the toolbar z(0) and it forms a stacking context of its own, so the dropdown's z(1) raises it only over the toolbar's own content. The whole toolbar is drawn at 0, and the page's content, which comes after it in the tree, is drawn over the dropdown, leaving only the part of the dropdown that hangs below the page visible.

A dialog centered over the app

To center a box over its parent, as CSS does, position it absolutely with insets of 0, auto margins, and a fit-content size. The insets make its whole parent the space to place it in, the fit-content size keeps it as small as its content, and the auto margins split the space left over evenly on each side, whether or not the parent centers its children. A margin paints nothing unless its element sets margin_color, so the app's content shows around the dialog. The dialog is drawn over the content because it comes after it; a dialog that comes earlier in the tree needs a higher z.

An absolutely positioned box is always placed in its own parent, never a more distant ancestor or the screen, so a dialog centers over the whole app only when it's a child of an element that fills the screen, such as the root component's element here. A dialog rendered deeper in the tree centers over its own parent.

dialog_box = position_absolute | inset(0) | margin_auto | fit_content_width | fit_content_height


@component
def dialog() -> Div:
    return Div(
        style=col,
        children=(
            Text(style=border, content="header"),
            Text(
                style=fill(1) | border | text_wrap_stable,
                content="The app's content stays where it is, and the dialog draws over it. " * 4,
            ),
            Text(
                style=dialog_box | border | border_double | pad_x(2) | pad_y(1),
                content="a dialog centered over the app",
            ),
        ),
    )

A dialog over the app

┌──────────────────────────────────────────────────────────┐
│header                                                    │
└──────────────────────────────────────────────────────────┘
┌──────────────────────────────────────────────────────────┐
│The app's c╔══════════════════════════════════╗alog draws │
│over it. Th║                                  ║nd the     │
│dialog draw║  a dialog centered over the app  ║here it is,│
│and the dia║                                  ║ stays     │
│where it is╚══════════════════════════════════╝           │
│                                                          │
│                                                          │
└──────────────────────────────────────────────────────────┘

Known issue: parents with a border, and grids

The layout engine, Taffy, mismeasures auto margins on an absolutely positioned box in two kinds of parent. In a flex parent with a border, the dialog lands one cell right of and below center, because the space its margins split includes the parent's border. In a grid parent, the auto margins are ignored, so the dialog sits at the parent's top-left corner. A parent with padding and no border centers it correctly.

A badge pinned to a corner

Negative insets reach past the parent's padding box onto its border. inset_top(-1) puts a box on the top border, and inset_right(-1) on the right border. clip_to_border keeps the badges visible if the parent hides its overflow.

@component
def badge() -> Div:
    return Div(
        style=row | align_children_start | gap(2),
        children=(
            Div(
                style=border | pad_x(1),
                children=(
                    Text(content="inbox"),
                    Text(
                        style=position_absolute
                        | inset_top(-1)
                        | inset_right(1)
                        | clip_to_border
                        | text_color("red", 500),
                        content="3",
                    ),
                ),
            ),
            Div(
                style=border | pad_x(1),
                children=(
                    Text(content="alerts"),
                    Text(
                        style=position_absolute
                        | inset_top(-1)
                        | inset_right(-1)
                        | clip_to_border
                        | text_bg("red", 600),
                        content="!",
                    ),
                ),
            ),
        ),
    )

Badges on borders

┌─────3─┐  ┌────────!
│ inbox │  │ alerts │
└───────┘  └────────┘