Lesson 33 of 50 · Web pages

Values, units and the box

Every element in a Salam page is a box, and you style it with properties written as name = value. The same properties work on the web and in a native window. This lesson shows how values are written and how the box is measured.

Properties are attributes

A style property is written like any attribute: name = value inside the element. Names may have spaces, such as min width or font size. A misspelled name or a value of the wrong kind is a compile error, and the message tells you what was expected. Every property of the language is listed in the style vocabulary reference.

Numbers, percent and units

A bare number uses the natural unit of the property: pixels for lengths, milliseconds for times, degrees for angles. A percent is a share of the parent. Other units are written after the number: em, rem, vw, vh, fr, ms, s, deg. A comma continues one property, so padding = 8, 16 is 8 above and below and 16 at the sides.

dsl-values.salam
page:
    title = "Values"
    box:
        width = 50%
        max width = 480
        padding = 8, 16
        margin = 16, auto
        background = #eef4ff
        color = #1e3a8a
        radius = 8
        paragraph: content = "50% of the parent, 8 above and below, 16 at the sides." end
        paragraph: font size = 1.25em content = "A size relative to the font." end
    end
end

Colours

A colour is written without quotes: a name such as red, or a hex colour with 3, 4, 6 or 8 digits (#rgb, #rrggbb, #rrggbbaa), where the last pair is the transparency. The reference lists the other colour forms of the standard.

dsl-colors.salam
page:
    title = "Colours"
    box:
        layout = row
        gap = 8
        padding = 16
        box: width = 64 height = 64 background = red end
        box: width = 64 height = 64 background = #3366aa end
        box: width = 64 height = 64 background = #3366aa80 end
        box: width = 64 height = 64 background = #f59e0b end
        box: width = 64 height = 64 background = #16a34a opacity = 0.4 end
    end
end

The box

Every box has content, then padding, then a border, then margin. Sizes always include the padding and the border, so a box with width = 320 is 320 wide whatever its padding. Padding and margin take one to four values (top, end, bottom, start) or a single side such as padding top, margin x and padding y. A margin can be auto.

dsl-box.salam
page:
    title = "The box"
    box:
        background = #e5e7eb
        padding = 24
        width = 320
        box:
            margin = 8
            border = 4, solid, #1d4ed8
            padding = 16
            background = #bfdbfe
            paragraph: content = "Content" end
        end
    end
end

Sizes

width and height take a length, auto, fill (the whole parent) or fit (just the content). min width, max width and the height versions limit them, and aspect ratio derives one side from the other.

dsl-sizes.salam
page:
    title = "Sizes"
    box:
        gap = 12
        padding = 16
        box: width = fill height = 40 background = #bbf7d0 end
        box: width = 50% height = 40 background = #fde68a end
        box: width = 200 min width = 120 max width = 150 height = 40 background = #fecaca end
        box: width = 160 aspect ratio = 1.5 background = #c7d2fe end
        box: width = 75% height = 40 background = #e9d5ff end
    end
end

Content that does not fit

overflow is visible, hidden, scroll or auto, with overflow x and overflow y for one direction. clip = true cuts children at the rounded corners. visible = false removes an element from the layout.

dsl-overflow.salam
page:
    title = "Overflow"
    box:
        layout = row
        gap = 16
        padding = 16
        box:
            width = 160 height = 80 overflow = hidden background = #e0f2fe
            paragraph: content = "This long text is cut at the bottom of the box because overflow is hidden. More words follow here." end
        end
        box:
            width = 160 height = 80 overflow = scroll background = #fef9c3
            paragraph: content = "This long text can be scrolled because overflow is scroll. More words follow here." end
        end
    end
end

Components

A component packages a piece of page with parameters, and content: end marks where its children go.

dsl-components.salam
component Card(name: str):
    box:
        padding = 16
        border = 2, solid, #0f766e
        radius = 8
        heading: size = 3 content = name color = #0f766e end
        content: end
    end
end

page:
    title = "Cards"
    box:
        padding = 32
        gap = 16
        Card: name = "Fast"
            paragraph: content = "Compiled to machine code." end
        end
        Card: name = "Checked"
            paragraph: content = "Styles are checked while building." end
        end
    end
end