Skip to content
rizuki edited this page Nov 30, 2026 · 6 revisions

Layout

Set .layout = { ... } on a container using fields from Clay_LayoutConfig.

Column("Root",
       .layout = {
           .sizing   = { Grow(), Grow() },
           .padding  = PadAll(16),
           .childGap = 8,
           .childAlignment = { .x = AlignXStart(), .y = AlignYStart() },
       }) { ... }

Sizing

Set width and height with a pair of CC_SizingAxis values. Use these helpers for individual axes or the whole CC_Sizing struct:

Helper Returns Meaning
Fit() CC_SizingAxis Shrink-wrap around the element's children (+ padding). Clay's default.
Grow() CC_SizingAxis Consume all remaining space on this axis, shared proportionally between siblings. Like CSS flex: 1.
Fixed(px) CC_SizingAxis Exactly px pixels on this axis.
Percent(p) CC_SizingAxis p × parent-size, with p a float in [0.0, 1.0]. Clay errors out on values outside this range.
GrowMinMax(min, max) CC_SizingAxis Grow(), but never below min or above max pixels. A max of 0 means no upper limit.
FitMinMax(min, max) CC_SizingAxis Fit(), but never below min or above max pixels. FitMinMax(h, 0) keeps a pill at least as wide as it is tall.
SizingAll(s) CC_Sizing Both axes use the same sizing: e.g. SizingAll(Grow()).
SizingOnly(.width = ..., .height = ...) CC_Sizing Designated init. Unspecified axis defaults to Fit().
.layout = { .sizing = SizingAll(Grow()) }                           // fill the container
.layout = { .sizing = SizingOnly(.width = Fixed(220),
                                 .height = Grow()) }                // sidebar
.layout = { .sizing = SizingOnly(.width = Grow()) }                 // horizontal bar, height = Fit()
.layout = { .sizing = { Percent(0.5f), Grow() } }                   // literal pair still works

Fit() / Grow() also accept min/max bounds when you build the sizing struct directly:

.layout = { .sizing = { CLAY_SIZING_GROW(.min = 120, .max = 480), Grow() } }

Padding

.padding is a CC_Padding with .left, .right, .top, .bottom. ccompose follows a Flutter-style All / Symmetric / Only pattern:

Helper Meaning
PadAll(n) n pixels on all four sides.
PadSymmetric(h, v) h on left/right, v on top/bottom.
PadOnly(.left = ..., .top = ..., .right = ..., .bottom = ...) Designated init. Unspecified sides default to 0.
.layout = { .padding = PadAll(16) }
.layout = { .padding = PadSymmetric(20, 12) }                  // horizontal=20, vertical=12
.layout = { .padding = PadOnly(.left = 16, .top = 8) }         // others = 0

Padding reduces the space available to children inside the bounding box. A Fixed(100) square with PadAll(20) leaves a 60 × 60 content area.

Child gap

.childGap is a flat integer: pixels of space between adjacent children on the main axis. It does not add leading or trailing space.

Column("List", .layout = { .childGap = 8 }) {
    Text("A", ...);
    Text("B", ...);   // 8px below A
    Text("C", ...);   // 8px below B
}

For lines between children, set .border.width.betweenChildren. See Styling: borders.

Child alignment

.childAlignment controls how children are aligned within the parent's bounds on the cross axis (perpendicular to the layout direction).

Helpers are axis-tagged so X-axis and Y-axis values can't be silently mixed up:

Axis Helper Meaning
.x AlignXStart() left-aligned (default)
.x AlignXCenter() horizontally centered
.x AlignXEnd() right-aligned
.y AlignYStart() top-aligned (default)
.y AlignYCenter() vertically centered
.y AlignYEnd() bottom-aligned

For the whole field there's also ChildAlign(.x = ..., .y = ...):

.layout = { .childAlignment = ChildAlign(.x = AlignXCenter(),
                                         .y = AlignYCenter()) }

Alignment depends on the container direction:

  • In a Column, .x aligns each child within the parent's width, and .y positions the stack within the parent's height.
  • In a Row, .y aligns each child within the parent's height, and .x positions the row within the parent's width.
// A Row where every child is vertically centered inside the 40px bar:
Row("Bar",
    .layout = { .sizing         = { Grow(), Fixed(40) },
                .padding        = PadAll(8),
                .childGap       = 12,
                .childAlignment = { .y = AlignYCenter() } }) { ... }

// A Column that centers itself within the screen:
Column("Centered",
       .layout = { .sizing         = { Grow(), Grow() },
                   .childAlignment = { .x = AlignXCenter(),
                                       .y = AlignYCenter() } }) { ... }

Layout direction

Column forces CC_TOP_TO_BOTTOM, Row forces CC_LEFT_TO_RIGHT, Box layers its children instead of flowing them (see Containers). Setting .layout.layoutDirection on these is overwritten by the macro.

To choose direction at runtime, use Element(direction, ...).

Putting it all together

Column("Panel",
       .layout = {
           .sizing         = { Grow(), Grow() },          // fill parent
           .padding        = PadAll(20),                    // inset children 20px
           .childGap       = 12,                            // stack spacing
           .childAlignment = { .x = AlignXCenter() }         // center horizontally
       },
       .backgroundColor = Color(28, 28, 34, 255),
       .cornerRadius    = RadiusAll(8)) {

    Text("Header", .textColor = Color(236,236,236,255), .fontSize = 24);
    Text("Body",   .textColor = Color(180,180,180,255), .fontSize = 14);

    Row("Actions",
        .layout = { .childGap = 8, .childAlignment = { .y = AlignYCenter() } }) {
        Button("Cancel", ...) { Text("Cancel", ...); }
        Button("OK",     ...) { Text("OK",     ...); }
    }
}

Clone this wiki locally