In short
A poster for astronomy talks before a solar eclipse. It shows how to place each item by measuring from the edge of the page or from another item.
What you'll build
A poster from a fictional astronomy club for four Thursday lectures before the total solar eclipse of 2 August 2027. An ultramarine sky fills the top two thirds of an A3 sheet. On the right a black Moon covers a lemon Sun cut off by the trim, and a corona drawn in code spreads behind both. The title, The Shadow Returns in 120 pt Funnel Display, stands on its standfirst at the foot of the sky. Under the horizon four blue dates head the talks, and the fine print runs in two columns above a blue band. Each element hangs from the bleed, the page, its slot or another element, and its offsets count from there. The second sheet repeats the poster with a pink frame round each element’s box and round the header and footer containers, and a tag on thirteen elements that prints each one’s placement.
This recipe answers
- How do I place design elements against the page, the bleed or one another instead of by coordinates?
- How do I add a watermark, a background tint or a decorative image on every page?
The short answer
const SKY = 272; // mm from the trim's top edge to the horizon, the sky's lower edge
const INSET = BLEED + MARGIN; // from the bleed's edge to the text's edge
const AIR = 14; // mm between the standfirst and the horizon
const poster = () => ({ enabled: true, slot: { elements: [ // array order is paint order
// 'bleed' and 'page' are frames of the sheet; 'container' would be this heading's own box.
{ kind: 'box', id: 'sky', style: { backgroundColor: col('sky') }, // no width: fills the bleed
placement: at('bleed', 'top-left', 0, 0, { height: mm(BLEED + SKY) }) },
...eclipse(), // the Sun hangs from the trim's corner, the Moon and the corona from the Sun
// The headline stands on the horizon and grows upwards: 'above' puts an element's
// bottom-left corner on the top-left corner of its anchor, so a longer title lifts the kicker.
text('deck', '{attr.deck}', FACE.deck, { ...at('#sky', 'align-bottom', INSET, -AIR),
size: { maxWidth: mm(153) } }), // shrink-wraps its lines, but wraps at 153 mm
text('title', '{titleText}', FACE.title, at('#deck', 'above', 0, -9)), // breaks at the \\
text('kicker', '{attr.kicker}', FACE.kicker, at('#title', 'above', 0, -7)),
...programme(), // four columns chained off the horizon
] } }); // hook-up: headings.levels[0] = { span: 'page', breakBefore, advancedDesign: poster() }
// {titleText} is the heading's text; each {attr.*} is written on its line in content.md.
Two elements pinned to frames of the sheet, every other one to an element
Ingredients
- Features
- Anchoring design elementsText, rules and boxes in page designsPictures in page designsDesigned openersFull-width chapter bandBleed bands and thumb tabsHeading stylesRunning heads per sectionHeading attributesLine breaks in titlesSemantic colour paletteParagraph stylesFigures and tables as resourcesPages on a canvas
- Also uses
- Bleed and crop marks
- Type
- Funnel Display, Funnel Sans, Martian Mono (SIL OFL 1.1)
- Assets
- None: every picture is drawn in code
Method
#1 · Pin two elements to the sheet and hang the rest from them
The code is the short answer above. In it, anchor.to names what an element hangs from: 'bleed' and 'page' are frames of the sheet (the same box while page.cutLines is off), 'container' is the slot’s own box and '#id' is another element (element placement). A box with no width runs to the far edge of its frame, so the sky fills the bleed from side to side. An element placed against the sky measures from the sky’s left edge, which is the bleed’s, so the standfirst takes INSET, 18 mm, as its x. The headline is built upwards from the sky’s lower edge. 'align-bottom' puts the foot of the standfirst’s box AIR, 14 mm, above that edge, and 'above' sets each element’s foot on the top of the one below it, so a title that runs to more lines lifts the kicker instead of covering the standfirst. Under the design, the fine print starts on the first 16 pt grid line at least 3.2 mm below the lowest element, the speakers’ line; those 3.2 mm are the heading’s default bottom margin.
#2 · Hang the Moon and the corona from the Sun
const SUN = 150; // mm across; the Moon is drawn the same size
const CORONA = 300; // mm, the square picture of the corona
const HALO = (CORONA - SUN) / 2; // how far the corona reaches past the Sun on every side
const BITE = { x: 2, y: 2.5 }; // mm the Moon sits off the Sun: a sliver is left at upper left
const disc = (id, colour, placement) => ({ kind: 'box', id, placement: { ...placement,
size: square(SUN) }, style: { backgroundColor: col(colour), borderRadius: mm(SUN / 2) } });
const eclipse = () => [
{ kind: 'image', id: 'corona', resourceId: 'corona', // listed before the discs: painted behind
placement: at('#sun', 'align-top', -HALO, -HALO, square(CORONA)) },
disc('sun', 'sun', at('page', 'top-right', 30, 25)), // 30 mm of it past the right edge
disc('moon', 'ink', at('#sun', 'align-top', BITE.x, BITE.y)),
];
The Sun is the one element pinned to the page, the trim box. Measured from the 'top-right' corner, a positive x runs past the right edge, so 30 mm of the disc is cut off. The Moon and the corona hang from the Sun with 'align-top', so moving the Sun moves both. Their offsets count from the Sun’s top-left corner: the Moon sits 2 mm right and 2.5 mm down, which leaves a sliver of Sun at the upper left, and the corona starts HALO, 75 mm, up and to the left, which centres its 300 mm square on the 150 mm disc. The corona is an image element that takes the size of its box (image elements) and comes before the discs in the list, so it is painted behind them.
#3 · Chain the evenings into columns
const GUT = 7; // mm between the evenings, and between the two columns of text under them
const COL = (A3.width - 2 * MARGIN - 3 * GUT) / 4; // four columns across the text width: 60 mm
const DROP = 12; // mm from the horizon to the dates
const programme = () => [1, 2, 3, 4].flatMap((n) => [
text(`day${n}`, `{attr.d${n}}`, FACE.day, n === 1
? at('#sky', 'below', INSET, DROP, wide(COL)) // the first date hangs from the horizon
: at(`#day${n - 1}`, 'right-of', GUT, 0, wide(COL))), // the others from the one before
text(`talk${n}`, `{attr.t${n}}`, FACE.talk, at(`#day${n}`, 'below', 0, 3, wide(COL))),
text(`who${n}`, `{attr.s${n}}`, FACE.who, at(`#talk${n}`, 'below', 0, 2.5, wide(COL))),
]);
Each date, talk and speaker line has a fixed size.width of COL, and 'right-of' plus GUT lays the next date one column over. Without a width a date shrink-wraps to its digits (the ‘8’ is 17 mm wide), so the ‘15’ would start 7 mm after it and each talk would run on one line into the next column. COL splits the text width into four with the same 7 mm gutter as the two columns of fine print below, which puts the second column of fine print under the 22. Each talk hangs 'below' its date and each speaker line below its talk, so a talk that wrapped to a third line would push down its own speaker and no other.
#4 · Put the band in the footer, on every page
const MARK = 10; // mm, the club's mark
const PAD = (FOOT - MARK) / 2; // centres the mark in the bottom margin
const markTall = { height: mm(MARK) }; // as tall as the mark: the line is centred on it
const footer = { elements: [
{ kind: 'box', id: 'band', style: { backgroundColor: col('sky') }, // the bottom margin, bled
placement: at('bleed', 'bottom-left', 0, 0, { height: mm(BLEED + FOOT) }) },
// The container runs from the text down to the trim: 'bottom-*' counts up, 'top-*' down.
{ kind: 'image', id: 'mark', resourceId: 'mark',
placement: at('container', 'bottom-left', 0, -PAD, square(MARK)) },
text('club', '{attr.club}', FACE.foot, at('#mark', 'right-of', 3, 0, markTall)),
text('free', '{attr.free}', FACE.foot, at('container', 'top-right', 0, PAD, markTall)),
] };
Anything that has to print on every page, such as this band, goes in the header or footer slot, and a heading style with a header or footer of its own replaces that slot on its section’s pages. The guides style’s footer repeats these four elements with their frames and tags, which is why the band and the mark print on both sheets. The footer’s container is the bottom margin, from the text down to the trim: 'bottom-left' counts up from the trim and 'top-right' down from the text, and the mark’s −10 and the “Free entry” line’s +10 land on the same 10 mm strip in the middle of the band (headers and footers). The footer is painted after the text and takes no room from it, so FOOT sets both the bottom margin and the band’s height, and the band stays clear of the lines above it.
#5 · Draw the construction lines from the same config
const GUIDE = { borderColor: col('guide'), borderWidth: pt(1) };
const signed = ({ value }) => `${value < 0 ? '−' : '+'}${Math.abs(value)}`; // all offsets in mm
const describe = ({ id, placement: { anchor: { to, edge }, offset = {} } }) => [`#${id}`,
`${edge} ${to}`, // the two words of anchor: { to, edge }
['x', 'y'].filter((k) => offset[k]?.value).map((k) => `${k} ${signed(offset[k])}`).join(' '),
].filter(Boolean).join(' · ');
// A border never changes an element's size (padding does), so each frame traces its box.
// An image or a rounded box gets a square frame of its size, hung from its top-left corner.
const frame = (el) => (el.kind === 'text' ? [{ ...el, box: { ...el.box, ...GUIDE } }]
: el.kind === 'box' && !el.style.borderRadius ? [{ ...el, style: { ...el.style, ...GUIDE } }]
: [el, { kind: 'box', id: `${el.id}-box`, style: GUIDE,
placement: at(`#${el.id}`, 'align-top', 0, 0, el.placement.size) }]);
const tag = (el, [edge, x = 0, y = 0, note]) => text(`${el.id}-tag`, note ?? describe(el),
face('Martian Mono', 8.5, 500, 'ink', { box: { backgroundColor: col('guide'),
padding: { top: pt(1.6), bottom: pt(1.2), left: pt(3), right: pt(3) } } }),
at(`#${el.id}`, edge, x, y));
const guides = (elements, tags) => [...elements.flatMap(frame),
...elements.filter((el) => tags[el.id]).map((el) => tag(el, tags[el.id]))];
const container = (id) => ({ kind: 'box', id, style: GUIDE, // the slot's own box, drawn
placement: at('container', 'top-left', 0, 0, { width: 'fill', height: 'fill' }) });
const TAGS = { // where each tag sits against its element: [edge, x, y, text]
sky: ['align-bottom', INSET, -3], corona: ['align-bottom', 90, -2], sun: ['above'],
moon: ['below', 20, 2], kicker: ['above'], title: ['above'], deck: ['above'],
day1: ['above', 0, -6], day2: ['above'], who1: ['below'],
mark: ['above', 0, -2], club: ['above', 69, -2], free: ['above', -30, -2],
header: ['align-bottom', 0, -2, t({ en: 'header container', es: 'contenedor de la cabecera' })],
footer: ['align-bottom', 0, -2, t({ en: 'footer container', es: 'contenedor del pie' })],
};
guides() walks the poster’s elements. A text gets a border on its box and a square box a border on its style; neither moves anything, because only padding makes a box bigger. An image or a round disc gets a square box of its own size hung from its top-left corner, so the corner of the Sun that the Moon and the corona hang from is drawn too. Each tag is a text hung from its element, written by describe() from that element’s own placement, so a tag cannot disagree with the config it labels. {style="guides"} on the second heading brings in this design and a header and footer of its own (heading styles). A tag with no width only has the room between its anchor and the container’s right edge, which is why the #free tag starts 30 mm left of its text. pageIndexOffset: 1, in the content’s continuation, makes the poster a verso, so the viewer sets the two sheets side by side.
The whole recipe
// ═══ Postext Cookbook · Nº 029 · Anchoring cheat sheet: a poster built from chained elements ═══
// https://postext.dev/en/cookbook/anchoring-cheat-sheet
// Code: MIT · Text: original (CC BY 4.0) · Drawings: generated in code (CC BY 4.0)
// Fonts: Funnel Display, Funnel Sans, Martian Mono (SIL OFL 1.1) · Needs postext ≥ 1.34.0
// An A3 poster whose elements hang from the bleed, the page, their slot or one another, never
// from coordinates; the second sheet is the same poster with each element framed and tagged.
import {
buildDocumentWithFonts, renderPageToCanvas, registerResourceImage,
} from 'https://esm.sh/postext';
const LANG = 'en'; // @lang: the language of the sample document ('en' | 'es')
const RECIPE = 'anchoring-cheat-sheet';
// ─── 1 · Design ─────────────────────────────────────────────────────────────
const palette = {
ink: '#0b0b0c', // text, and the Moon
sky: '#1b3bff', // the sky, the foot band and the dates
sun: '#ffe53b', // the Sun and the kicker
paper: '#ffffff', // type on the sky
muted: '#5c5f66', // speakers and the colophon
guide: '#ff2d9b', // frames and tags on the construction sheet
};
// col(id): a colour linked to its palette entry, so a swapped palette recolours the sheet.
const col = (id) => ({ hex: palette[id], model: 'hex', paletteId: id });
const colorPalette = [
...Object.entries(palette).map(([id, hex]) => ({ id, name: id, value: { hex, model: 'hex' } })),
{ id: 'main-color', name: 'sky (defaults)', value: { hex: palette.sky, model: 'hex' } },
];
const A3 = { width: 297, height: 420 }; // mm, the trim
const MARGIN = 18; // mm at the top and sides: the body's edges, and the header container's
const FOOT = 30; // mm, the bottom margin: the footer container and its band
const BLEED = 0; // mm; 3 for the printer, which switches on page.cutLines below
// at(): a placement. Offsets are distances from the anchor point, never page coordinates.
const at = (to, edge, x = 0, y = 0, size) => ({ anchor: { to, edge },
offset: { x: mm(x), y: mm(y) }, ...(size && { size }) });
const wide = (w) => ({ width: mm(w) }); // fixed, so a text is as wide as its column
const square = (d) => ({ width: mm(d), height: mm(d) });
// Design text is centred by default, and in a footer it is cut with '…': these wrap, set left.
const text = (id, content, face, placement) => ({ kind: 'text', id, content, overflow: 'wrap',
align: 'left', ...face, placement });
const face = (fontFamily, size, fontWeight, colour, more) => ({ fontFamily, fontSize: pt(size),
fontWeight, color: col(colour), ...more });
const caps = (tracking) => ({ letterSpacing: pt(tracking), textTransform: 'uppercase' });
const FACE = { // lineHeight as a multiple of each size
kicker: face('Martian Mono', 12, 500, 'sun', caps(1.8)),
title: face('Funnel Display', 120, 800, 'paper', { lineHeight: 0.86 }),
deck: face('Funnel Sans', 22, 400, 'paper', { lineHeight: 1.22 }),
The composed script.js runs as it is: paste it into any page’s module script, or open the recipe on CodePen. Recipe folder on GitHub ↗ (opens in a new tab)
Variations
#Add a 3 mm bleed and crop marks
Three millimetres of BLEED switch on the cut lines and push the sky and the foot band 3 mm into the bleed they hang from, while INSET keeps the text on the margin; the Sun, the Moon and the corona hang from the page, and everything a page paints is clipped to the bleed, so the Sun ends 3 mm past the trim and the slug holds only the crop marks.
-const BLEED = 0; // mm; 3 for the printer, which switches on page.cutLines below
+const BLEED = 3; // mm; 3 for the printer, which switches on page.cutLines below#Raise the horizon
Taking 20 mm off SKY lifts the sky’s edge, and with it the standfirst, the title, the kicker and the twelve texts of the programme, while the fine print starts on a higher grid line; the Sun hangs from the page, so the second change lifts it the same 20 mm, or the ‘w’ of Shadow would touch the crescent.
-const SKY = 272; // mm from the trim's top edge to the horizon, the sky's lower edge
+const SKY = 252; // mm from the trim's top edge to the horizon, the sky's lower edge
- disc('sun', 'sun', at('page', 'top-right', 30, 25)), // 30 mm of it past the right edge
+ disc('sun', 'sun', at('page', 'top-right', 30, 5)), // 30 mm of it past the right edgePitfalls
Pitfall
Header and footer elements paint over text
Header and footer elements are painted over the page and the text area does not make room for them. Keep them within the margins, which are what reserve their space. Running heads and folios →
Pitfall
Attribute values: no { or }; single-quote a value with "
An attribute value ends at the closing brace, so it cannot hold { or }. A value that contains a double quote goes in single quotes; a dollar sign is fine. Heading attributes →
Sandbox check · designCyclicAnchor
Cyclic anchor reference
Why. Design elements anchor to each other in a loop, so none of them can be placed.
Fix. Anchor one element of the chain to the container, the page or the bleed. Docs →
Sandbox check · designDanglingAnchor
Dangling anchor reference
Why. A design element anchors to an #id that no element in the slot has.
Fix. Correct the id, or add the element it refers to. Docs →
- A pen gets no report of a cyclic or a dangling anchor. The Sandbox’s Checks panel lists them in the header, the footer, part pages and heading levels, but not in heading styles such as
guides. An element pinned to an#idthat does not exist is placed against the container: an element edge such as'below'falls back to the container’s top-left corner, offset included, and a container edge such as'top-right'to that corner. In a loop, one element falls back the same way and the others chain off it. 'align-left'and'align-top'land on the same point: both put the element’s top-left corner on its anchor’s top-left corner.- A border is painted centred on the box’s edge, so each 1 pt pink frame reaches 0.5 pt outside the box it traces, although the configuration docs say strokes are painted inside.
Credits
- Recipe
- Ignacio Ferro
- Text
- Original prose, CC BY 4.0
- Fonts
- Funnel Display (SIL OFL 1.1) · Funnel Sans (SIL OFL 1.1) · Martian Mono (SIL OFL 1.1)
Edit this write-up ↗ (opens in a new tab)Recipe folder on GitHub ↗ (opens in a new tab)


