beetlebug▾
/

Flex layout

flex is a geometry manager beside pack, grid, and place. It lays out the children of a container in a row or a column, with the rules of the flexible box layout of CSS. Use the flex command in a script, or put display: flex in a stylesheet and make no layout call.

Flex layout
A window with a toolbar of three buttons and a search field, a column of links on the left, six cards in two columns in the middle, a column of details on the right, and a footer
examples/flex-demo.tcl: a toolbar from the flex command, and a page with three columns and cards from the rules of examples/flex-demo.css.
The same window, narrow: the six cards are in one column
The same window at 560 logical pixels wide: ztk examples/flex-demo.tcl narrow. The cards wrap to one card on each line.

Pick a geometry manager

Manager Use it for
pack A stack of widgets on the sides of a container, as in each Tk program.
grid A form or a table: rows and columns that line up across the window.
flex A toolbar, a list of cards that wraps, or a page of regions that grow and shrink. Spacing and alignment come from a stylesheet.
place A widget at a fixed position or at a fraction of its container.

One container has one manager for its content, as in Tk. Each container in a window can use a different manager.

The command

wm title . "Toolbar"
frame .bar
pack .bar -fill x
flex container .bar -gap 8 -padding 8 -align center
ttk::button .bar.new -text New
ttk::button .bar.open -text Open
frame .bar.space
ttk::entry .bar.find -width 12
flex .bar.new .bar.open -shrink 0
flex .bar.space -grow 1
flex .bar.find -basis 120
Toolbar
The widget flex-toolbar
LivePlayground

Drag the grip in the corner of the window, or use Narrow and Wide, to see the layout follow the size. The space grows, and the search field stays at the right end.

flex container sets the options of the container. flex WINDOW makes each window an item of its parent, at the end of the list. The space grows and pushes the search field to the right end. flex has each subcommand and option. ztk::flex is a second name of the same command.

The CSS route

A container with display: flex in its style manages each child that has no manager. The order is the order of creation.

wm title . "Cards"
ztk::style add {
    Frame.cards { display: flex; flex-wrap: wrap; align-content: flex-start; gap: 8px; padding: 8px; }
    Frame.cards > Label { flex: 1 1 100px; padding: 12px; border: 1px solid var(--border); border-radius: 8px; }
}
frame .cards
ztk::class .cards add cards
pack .cards -fill both -expand 1
foreach name {Mercury Venus Earth Mars Jupiter Saturn} {
    label .cards.[string tolower $name] -text $name
}
Cards
The widget flex-cards
LivePlayground

Each label has a basis of 100 pixels and grows to fill its line. Three labels fit on one line of this window, so the six labels make two lines. Make the window narrow, and each label gets a line of its own.

A page with a header, a footer, and three columns is two flex containers:

wm title . "Page"
ztk::style add {
    Frame.page { display: flex; flex-direction: column; }
    Frame.page > Label { padding: 8px; background-color: var(--accent); color: var(--accent-fg); }
    Frame.middle { display: flex; flex: 1; gap: 8px; padding: 8px; }
    Frame.middle > Label { width: 80px; border: 1px solid var(--border); border-radius: 6px; }
    Frame.middle > Label.main { flex: 1; }
}
frame .page
ztk::class .page add page
pack .page -fill both -expand 1
label .page.head -text Header
frame .page.middle
ztk::class .page.middle add middle
label .page.middle.nav -text Nav
label .page.middle.main -text Content
ztk::class .page.middle.main add main
label .page.middle.side -text Side
label .page.foot -text Footer
Page
The widget flex-page
LivePlayground

The properties

Property Values On
display flex, block (no flex layout) the container
flex-direction row, row-reverse, column, column-reverse the container
flex-wrap nowrap, wrap, wrap-reverse the container
flex-flow a direction and a wrap the container
justify-content flex-start, flex-end, center, space-between, space-around, space-evenly the container
align-items stretch, flex-start, flex-end, center the container
align-content stretch and the values of justify-content the container
gap, row-gap, column-gap lengths the container
padding one to four lengths the container
flex none, auto, or GROW SHRINK BASIS the item
flex-grow, flex-shrink a number of 0 or more the item
flex-basis auto or a length the item
align-self auto and the values of align-items the item
order an integer the item
margin, margin-top, margin-right, margin-bottom, margin-left lengths the item
width, height auto or a length the item
min-width, min-height, max-width, max-height a length, or none for a max the item

start is the same as flex-start, and end is the same as flex-end. No property of the table inherits. The lengths are px, pt, em, and rem. width and height apply only in a flex layout.

Precedence

  1. A child that has a manager keeps it. An explicit pack, grid, place, or flex of a child wins over the rule of its parent.
  2. pack or grid of a child into a container that has only CSS items becomes the manager of the container. The other children then have no manager and are not mapped, as in Tk.
  3. An option of the flex command wins over the CSS value of the same property, as a widget option wins over a stylesheet.
  4. flex forget keeps a child out of the CSS items. A new flex of the child adds it again.

Sizes

Speed

A resize lays out only the containers whose size or content changed. A container with the same size, the same items, and the same options makes no solve, and the solve allocates no memory after the first one. An item whose rectangle is the same gets no paint and no <Configure>. An item outside the box of its container is not mapped, because a widget is clipped to its parent.

The benchmark makes the window of bench/flex-resize.tcl: rows of a toolbar, a column of links, and cards that wrap, nested three levels deep. Each card is a frame with two labels. The same widgets with grid are the comparison. The window gets a new size for each of 300 frames. The times are in ms.

zig build bench -- flex (ReleaseFast, headless, no CPU draw), 2026-11-29, Intel Core i7-6700K, load average 2.6 to 4.3 from other jobs:

Widgets Layout, flex: p50, p99, max Layout, grid Frame, flex Frame, grid
96 0.013, 0.020, 0.023 0.007, 0.013, 0.013 0.18, 0.48, 0.64 0.17, 0.44, 0.54
1,003 0.132, 0.198, 0.228 0.087, 0.149, 0.167 2.74, 3.71, 3.77 3.40, 5.20, 5.65
4,994 0.662, 1.147, 3.231 0.145, 0.287, 0.318 36.4, 43.6, 92.2 35.0, 41.3, 48.0

tools/flex-resize.sh (ReleaseFast, a real X11 window on Xvfb with the CPU renderer), the same day and machine, load average 2.6. The frame time is update after wm geometry. The layout is the stage of ZTK_FRAME_STATS, p50 and p99:

Widgets Frame, flex: p50, p99, max Frame, grid Layout, flex Layout, grid
96 8.9, 18.2, 19.8 8.8, 13.1, 13.6 0.033, 0.078 0.018, 0.037
1,003 23.7, 36.7, 65.7 22.3, 40.2, 88.6 0.272, 0.567 0.206, 0.412
4,994, 10 frames 72.0, 134.6, 134.6 67.7, 96.0, 96.0 0.871, 9.925 0.172, 4.847

The layout of 1,000 items is under 0.6 ms at p99 in both runs. The frame of a real window is the CPU draw and the copy to the X server, with the same time for flex and grid. On X11 with 5,000 widgets, the frame time grows during a long run with both managers (FRAMES=60 gives a p50 near 290 ms), so the table has 10 frames. At 5,000 widgets the layout of flex costs more than the layout of grid. The wrap of the cards changes on most frames, and each stretched label gets a new width. Grid gives a size of 0 to the rows that do not fit, so their cards get no layout.

Limits

These parts of CSS flexbox are not in ztk: baseline alignment, margin: auto, percentages, display: none, right-to-left rows, and the min-content minimum size. CSS grid (display: grid) and media queries on the size of the window are planned. Styling and themes has the other properties of the stylesheet.