Lesson 38 of 56 · 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.
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
endPlacing 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.
Reading the window with page
Inside a page or a component, page is the window the page is shown in. page.width and page.height are its size in pixels. page.compact, page.regular and page.wide are true below 600, from 600 to 1024 and above 1024 pixels. page.landscape, page.portrait, page.dark, page.light, page.touch, page.mouse, page.reduced motion, page.high contrast, page.rtl and page.ltr describe the device and the person's settings. They join with and, or and not, and with your own variables.
page:
background = "white"
if page.width >= 400 and page.height < 900:
background = "honeydew"
end
nav:
layout = "row"
gap = 16
if page.compact:
layout = "column"
gap = 4
end
if page.dark:
color = "white"
end
link: url = "/" content = "Home" end
link: url = "/about" content = "About" end
end
if page.width < 500:
paragraph: content = "Turn the phone for more room." end
end
endAn if can change style attributes, as on nav, or show and hide elements, as with the last paragraph. The page reads the window when it opens and again when the window is resized or a system setting changes, so the page follows. On the web a style if on the page, an element or a component that uses only page sizes and settings becomes a CSS @media rule, which applies before any script runs. In a native window the layout is computed again at the new size, so text keeps its size instead of being scaled with the window.
Because page names the window, it cannot be a variable, parameter or loop name inside a page or a component (E253), and an unknown property such as page.size is reported as E254. The short words compact, wide, dark, screen width and the others still work in a style if.
Next: states and transitions.