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.

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.
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

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
}

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

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
- A child that has a manager keeps it. An explicit
pack,grid,place, orflexof a child wins over the rule of its parent. packorgridof 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.- An option of the
flexcommand wins over the CSS value of the same property, as a widget option wins over a stylesheet. flex forgetkeeps a child out of the CSS items. A newflexof the child adds it again.
Sizes
- The requested size of a widget (
winfo reqwidth) is its content size forflex-basis: auto. - The requested size of the container puts each item on one line at its basis, with the gaps, the margins, and the padding.
flex: 1has the basis 0, so such an item adds only its margins. - The default minimum size of an item is 0, so an item can shrink below its requested size. Use
flex-shrink: 0,-shrink 0, or amin-widthto keep a size. - A wrapping container gets its lines from its width, and its requested height is the height of those lines. The layout pass measures it again when its width is known, so its parent gives it the room of each line. In a column container the same is true of the height and the width.
flex propagate WINDOW 0keeps a size that the script sets, aspack propagatedoes. - An item that does not fit in the box of its container keeps its size, and it is not mapped.
- A resize of the container lays out the items again, and each item gets
<Configure>. - Each length is in logical pixels, so a layout is the same at each scale of the screen.
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.