Lesson 37 of 50 · Web pages

Layout: row, column, grid and stack

One property decides how an element places its children: layout. It has five values, and a few more properties tune them. The same description runs in a browser and in a native window.

The five layouts

  • column: children from top to bottom. This is the default.
  • row: children along the text direction. In Persian it runs from right to left.
  • grid: children in rows and columns that you define.
  • stack: all children in the same place, later ones on top.
  • flow: children like words in a paragraph. Text elements use it.

Gap, align and justify

gap is the space between children (one value, or row gap then column gap). align works across the direction of the layout (start, center, end, stretch, baseline) and justify along it (start, center, end, between, around, evenly).

dsl-layout.salam
page:
    title = "Rows and columns"
    box:
        layout = row
        gap = 12
        align = center
        justify = between
        padding = 16
        background = #f3f4f6
        box: width = 80 height = 40 background = #93c5fd end
        box: width = 80 height = 80 background = #86efac end
        box: width = 80 height = 60 background = #fca5a5 end
    end
    box:
        layout = column
        gap = 8
        padding = 16
        box: height = 30 background = #93c5fd end
        box: height = 30 background = #86efac end
    end
end

Sharing the free space

A child with grow = 1 takes a share of the space the others leave. grow = 2 takes twice the share. shrink and basis tune the start size, and align self overrides align for one child.

dsl-grow.salam
page:
    title = "Grow"
    box:
        layout = row
        gap = 8
        padding = 16
        box: width = 80 height = 40 background = #93c5fd end
        box: grow = 1 height = 40 background = #86efac end
        box: grow = 2 height = 40 background = #fca5a5 end
    end
end

Grid

columns and rows list the tracks: a length, a share such as 1fr, or auto. A child can use column, row, column span and row span.

dsl-grid.salam
page:
    title = "Grid"
    box:
        layout = grid
        columns = 100, 1fr, 1fr
        gap = 8, 16
        padding = 16
        box: background = #93c5fd height = 40 end
        box: background = #86efac end
        box: background = #fca5a5 end
        box: column span = 2 background = #fde68a height = 40 end
        box: background = #c4b5fd end
    end
end

Stack and wrap

A stack centres its children with align and justify. wrap = true lets a row or a column start a new line when it is full, and reverse = true reverses the order.

dsl-stack.salam
page:
    title = "Stack"
    box:
        layout = stack
        align = center
        justify = center
        width = 200
        height = 120
        box: width = 200 height = 120 background = #1e3a8a end
        box: width = 120 height = 60 background = #60a5fa radius = 8 end
        heading: size = 3 color = white content = "On top" end
    end
end
dsl-flow.salam
page:
    title = "Flow and wrap"
    box:
        layout = row
        wrap = true
        gap = 8
        padding = 16
        width = 260
        box: width = 100 height = 40 background = #93c5fd end
        box: width = 100 height = 40 background = #86efac end
        box: width = 100 height = 40 background = #fca5a5 end
        box: width = 100 height = 40 background = #fde68a end
    end
    paragraph:
        layout = flow
        content = "Flow places children like words in a paragraph."
    end
end

Packing to the end

end is a value like any other word: justify = end packs the children at the end of the main axis, align = end at the end of the cross axis, and text align = end aligns text to the end of the line, which is the right edge in English and the left edge in Persian.

dsl-align-end.salam
page:
    title = "End"
    box:
        layout = row
        justify = end
        align = end
        gap = 8
        height = 120
        padding = 16
        background = #f3f4f6
        box: width = 60 height = 40 background = #93c5fd end
        box: width = 60 height = 80 background = #86efac end
    end
    paragraph: text align = end padding = 16 content = "Aligned to the end of the line." end
end

Taking an element out of the flow

position = absolute with top, start, end or bottom places a child against its parent, and layer orders overlapping elements.