Lesson 38 of 50 · Web pages

Fluid layouts

A good page works in a narrow window and in a wide one. In the page language you get there with sizes that are relative and layouts that count how much fits, not with separate rules per screen.

Relative sizes

Percent, fill, vw and vh follow the room. max width keeps a column readable on a wide screen, and margin = 0, auto centres it.

Let the row wrap

A row with wrap = true whose children have grow = 1 and min width = 140 puts as many cards on a line as fit, and shares the rest of the line between them. Resize the window and the cards rearrange themselves.

dsl-responsive.salam
page:
    title = "Cards that fit"
    box:
        layout = row
        wrap = true
        gap = 12
        padding = 16
        max width = 720
        margin = 0, auto
        box: grow = 1 min width = 140 height = 80 background = #93c5fd radius = 8 end
        box: grow = 1 min width = 140 height = 80 background = #86efac radius = 8 end
        box: grow = 1 min width = 140 height = 80 background = #fca5a5 radius = 8 end
        box: grow = 1 min width = 140 height = 80 background = #fde68a radius = 8 end
        box: grow = 1 min width = 140 height = 80 background = #c4b5fd radius = 8 end
    end
end

Placing the lines

align content places the lines of a wrapped row.

Keeping a shape

aspect ratio = 1.5 keeps the proportions of a box while the width changes, and image fit = cover fills an image box without stretching.

Asking about the environment

Sometimes the room is not the only thing that changes: the person may use a phone, a dark colour scheme or ask for less motion. Inside an if that holds style attributes you can name these directly: compact, regular and wide (below 600, 600 to 1024, above 1024 pixels), screen width < 500, screen height, landscape, portrait, dark, light, touch, mouse, reduced motion, high contrast, rtl and ltr. They join with and, or and not, and with your own variables.

nav:
    padding = 16
    if compact:
        padding = 4
    else if wide:
        padding = 32
    end
    if touch and screen width < 500:
        gap = 12
    end
    if dark:
        color = "white"
    end
end

The page reads the window when it opens and again when the window is resized or the person changes a system setting, so the styles follow. In a native app these words are not available yet and the compiler reports E233; use a variable you set yourself.