Using spacing

Most time you will pass spacing into functions. There are special function fields that take only size. They are usually called like width, length, in(out)set, spacing and so on.

Like in CSS, one of the ways to set up spacing in Typst is setting margins and padding of elements. However, you can also insert spacing directly using functions h (horizontal spacing) and v (vertical spacing).

Links to reference: h, v.

Horizontal #h(1cm) spacing.
#v(1cm)
And some vertical too!
Rendered image

Absolute length units

Link to reference

Absolute length (aka just "length") units are not affected by outer content and size of parent.

#set rect(height: 1em)
#table(
  columns: 2,
  [Points], rect(width: 72pt),
  [Millimeters], rect(width: 25.4mm),
  [Centimeters], rect(width: 2.54cm),
  [Inches], rect(width: 1in),
)
Rendered image

Relative to current font size

1em = 1 current font size:

#set rect(height: 1em)
#table(
  columns: 2,
  [Centimeters], rect(width: 2.54cm),
  [Relative to font size], rect(width: 6.5em)
)

Double font size: #box(stroke: red, baseline: 40%, height: 2em, width: 2em)
Rendered image

It is a very convenient unit, so it is used a lot in Typst.

Combined

Combined: #box(rect(height: 5pt + 1em))

#(5pt + 1em).abs
#(5pt + 1em).em
Rendered image

Ratio length

Link to reference

1% = 1% from parent size in that dimension

This line width is 50% of available page size (without margins):

#line(length: 50%)

This line width is 50% of the box width: #box(stroke: red, width: 4em, inset: (y: 0.5em), line(length: 50%))
Rendered image

Relative length

Link to reference

You can combine absolute and ratio lengths into relative length:

#rect(width: 100% - 50pt)

#(100% - 50pt).length \
#(100% - 50pt).ratio
Rendered image

Fractional length

Link to reference

Single fraction length just takes maximum size possible to fill the parent:

Left #h(1fr) Right

#rect(height: 1em)[
  #h(1fr)
]
Rendered image

There are not many places you can use fractions, mainly those are h and v.

Several fractions

If you use several fractions inside one parent, they will take all remaining space proportional to their number:

Left #h(1fr) Left-ish #h(2fr) Right
Rendered image

Nested layout

Remember that fractions work in parent only, don't rely on them in nested layout:

Word: #h(1fr) #box(height: 1em, stroke: red)[
  #h(2fr)
]
Rendered image