Skip to main content
Recipe number 17

Cookbook · Chapter 3 · Headings & openers

Five chapter openers in one book

One opener on level 1 and four heading styles named in the Markdown; each style swaps the accent colour, and some also change the margins, columns or folios.

On this page
Output
Canvas
Postext
Tested with Postext 1.65.0
Needs ≥ 1.25.0
Licence
Updated 10 Oct 2026
Code MIT · Text CC BY 4.0

pp. 4–5 of 7

  • Trim 180 × 240 mm
  • 1 column
  • Lora 10.8/14.5
  • Fraunces
  • Geist
  • Geist Mono
  • 7 pages
  • Level
  • Postext 1.57.1
  • Laid out in 72 ms
  • 249 lines of code

In short

A short book about how chapters can begin, where each chapter starts in a different style. It shows how to give a chapter its own colours, margins and layout with one setting.

What you'll build

Five Ways to Begin is a seven-page book about chapter openers, 180 × 240 mm, and each chapter is set in the opener it describes. Chapter one is a novel's: a tracked kicker, a light italic title and a five-line red initial, with running heads on the pages that follow. Chapter two puts its title and a printer's flower inside a cream cartouche drawn with two blue rules. Chapter three stacks its title in white Fraunces Black on a magenta field that bleeds off the page, over a mono initial reversed out of a box. Chapter four, the manual, has mono running heads, an orange number badge beside its title and two columns of Geist. The coda sets its title halfway down an empty page, above a raised initial. Chapter one uses level 1's own opener; the other four name a heading style on their heading line.

This recipe answers

  • How do I give every chapter its own photo, colour or opener variant?
  • How do I add an author line, a standfirst or a lead with a drop cap to an opener?
  • How do I set a chapter title across the page while the body runs in two columns below?
  • How do I build a number badge beside the title that widens when the number grows (9 → 10)?

The short answer

script.js · lines 16–52in full code
// A heading line picks a style by id, and its attributes feed the opener's {attr.…} texts:
//   # The Frame {style="framed" kicker="Chapter the Second"}
// A style overrides the level fields it names (opener, drop cap) and, until the next level-1
// heading, the section fields it names; the rest comes from the level and the document.
const level1 = () => ({ level: 1,
  // span 'page' opens each chapter at the head of a new page and runs its opener across
  // every column (chapter four has two). By default an H1 waits for a recto, with a blank
  // page before it; parity 'any' lets these short chapters open on either side.
  span: 'page',
  breakBefore: { enabled: true, parity: 'any' },
  marginBottom: pt(0), // each opener ends where its first paragraph starts
  advancedDesign: literary, // chapter one, unstyled: the book's own opener
  dropCap: { lines: 5, fontFamily: 'Fraunces', fontWeight: 400, color: col('accent'),
    gap: mm(2.5) } }); // a five-line drop in rubric red
const headingStyles = () => [ // factories, called by config() once every design exists
  // Each style brings its opener and swaps 'accent' for a colour of its own, on its pages.
  { id: 'framed', advancedDesign: framed, palette: { accent: palette.lapis },
    margins: { left: mm(EVEN), right: mm(EVEN) }, // a centred text block; 'accent' is blue:
    dropCap: { lines: 3, fontFamily: 'Fraunces', fontWeight: 600, color: col('accent'),
      gap: mm(2) } },
  { id: 'stacked', advancedDesign: stacked, palette: { accent: palette.magenta }, // white, on
    dropCap: { lines: BOXED, fontFamily: 'Geist Mono', fontWeight: 700, color: col('paper'),
      gap: mm(2.2) } }, // the square the opener draws for it
  { id: 'spec', advancedDesign: spec, palette: { accent: palette.signal }, dropCap: false,
    marginBottom: pt(0), // the hairline under the title is the separator
    margins: { left: mm(SPEC), right: mm(SPEC) }, // a 144 mm text block
    layout: { layoutType: 'double', gutterWidth: mm(7) },
    bodyStyle: { fontFamily: 'Geist', fontSize: pt(9.8), textAlign: 'left' }, // on the grid
    header: { elements: [...specHead('even'), ...specHead('odd')] },
    footer: { elements: [] } }, // no folio at the foot: the head gives the page
  { id: 'quiet', numbered: false, // a coda: it advances no chapter counter
    advancedDesign: quiet, palette: { accent: palette.graphite }, footer: tinyFolio,
    dropCap: { lines: 1, fontFamily: 'Fraunces', fontWeight: 300, fontSize: pt(46), // raised:
      color: col('accent'), gap: mm(1.2) }, // on the first baseline, rising into the white
    margins: { left: mm(QUIET.inner), right: mm(QUIET.outer) }, // a 106 mm measure
    bodyStyle: { fontSize: pt(QUIET.size), lineHeight: pt(QUIET.lead), textAlign: 'left' } },
]; // hook-up in config(): headings: { levels: [level1(), …] }, headingStyles()

One opener per chapter: the level's own, then heading styles by name

Ingredients

Type
Lora, Fraunces, Geist, Geist Mono (SIL OFL 1.1)
Assets
None: every picture is drawn in code

Method

#1 · Set the book's own opener on the level

script.js · lines 97–105in full code
// The first paragraph, in :::paragraphs{style="lead"}, starts 17 mm under the title.
const literary = design([
  text('kicker', '{attr.kicker}', 'Geist', 8, centred(22), { ...caps(1.7), align: 'center' }),
  text('title', '{titleText}', 'Fraunces', 32, at('#kicker', 'below', 0, 4, 'fill'),
    { fontWeight: 300, italic: true, lineHeight: 1.05, align: 'center' }),
  { kind: 'rule', id: 'rule', direction: 'horizontal', thickness: pt(0.8), color: col('accent'),
    placement: at('#title', 'below', (MEASURE - 18) / 2, 6, 18) }, // an 18 mm rule, centred
  room('#title', 17),
]);

Chapter one has no style, so it takes level 1's own design, which every style starts from (the short answer shows the level and the styles). The chapter's first paragraph is in the Markdown, in a :::paragraphs{style="lead"} group a size larger than the text (12/17 pt against 10.8/14.5), and the level's dropCap opens it (drop caps): the M stands on the fifth baseline, and with no fontSize its top is level with the first line's capitals, both cap heights measured from Fraunces and Lora. The opener ends with an empty box, room(), 17 mm under the title, where the lead starts; a :::space under the lead leaves a line and starts the text flush.

#2 · Frame the title in two boxes

script.js · lines 109–127in full code
const FRAME = 62; // mm: the outer box's height
const INSET = 3; // mm: the hairline box runs this far inside it
const FLEURON = 44; // mm: the ornament's width, centred under the title
const framed = design([ // array order is paint order: the boxes first, the type on top
  { kind: 'box', id: 'frame', placement: { ...at('container', 'top-left', 0, 0),
    size: { width: 'fill', height: mm(FRAME) } },
  style: { backgroundColor: col('tint'), borderColor: col('accent'), borderWidth: pt(1.6) } },
  { kind: 'box', id: 'hairline', placement: { ...at('#frame', 'align-top', INSET, INSET),
    size: { width: mm(MEASURE - 2 * INSET), height: mm(FRAME - 2 * INSET) } },
  style: { borderColor: col('accent'), borderWidth: pt(0.5) } },
  text('kicker', '{attr.kicker}', 'Geist', 7.5, centred(14), { ...caps(1.6), align: 'center' }),
  text('title', '{titleText}', 'Fraunces', 34, at('#kicker', 'below', 0, 3.5, 'fill'),
    { fontWeight: 600, lineHeight: 1.05, align: 'center' }),
  // An image element draws a registered resource, never numbered or captioned. Its resourceId
  // may be '{attr.photo}' (postext ≥ 1.8): then each heading names its own photo.
  { kind: 'image', id: 'fleuron', resourceId: 'fleuron',
    placement: at('#title', 'below', (MEASURE - FLEURON) / 2, 4.5, FLEURON) },
  room('#frame', 9), // the text starts 9 mm under the frame
]);

Array order is paint order, so the boxes come first and the type on top; the ornament is an image element, which draws a registered SVG resource that is never numbered or cited. An image element is also how each chapter gets a photo of its own: since postext 1.8 its resourceId takes placeholders, so with resourceId: '{attr.photo}' one style draws the photo each heading names, # The Frame {style="framed" photo="lake"}, as the dishes of the recipe card do (image elements). The framed style in the short answer sets palette to turn accent blue on this chapter's pages, so the rules, the kicker and the drop folio print blue, and its margins centre the text block under the frame. The style's own dropCap names col('accent'), which the palette turns blue too, so the three-line L matches the frame.

#3 · Stack the title, box the initial

script.js · lines 131–160in full code
const FIELD = 130; // mm from the trim: the foot of the colour field
const S_BEARING = 1.2; // mm: the white left of the title's S (0.0365 em at 94 pt)
// The boxed initial: the style's drop cap sets the white letter; the opener draws the square,
// fitted by hand in pt. The text starts on the grid line 8 mm under the field (from the trim).
const TEXT_TOP = Math.ceil(((FIELD - TOP + 8) * 72) / 25.4 / LEAD) * LEAD; // pt
const BASE = 0.8; // a line's baseline stands 0.8 down its line box
const CAPS = 0.7; // the height of a capital in Lora, em
// Geist Mono, em: cap height, the advance every capital shares, the white either side of an M
const MONO = { caps: 0.71, advance: 0.6, side: 0.044 };
const BOXED = 3; // lines the initial spans
const capsTop = TEXT_TOP + BASE * LEADIN.lead - CAPS * LEADIN.size; // the first line's capitals
const foot = TEXT_TOP + (BOXED - 1 + BASE) * LEADIN.lead; // the third baseline: the letter's foot
// The M runs from the first line's capitals to its foot; the square runs past both as far as
// the M's sides sit in, so the colour is as wide on all four sides.
const letter = (foot - capsTop) / MONO.caps, pad = MONO.side * letter;
const stacked = design([
  { kind: 'box', id: 'field', style: { backgroundColor: col('accent') },
    placement: { anchor: { to: 'bleed', edge: 'top-left' }, size: { height: mm(FIELD) } } },
  text('kicker', '{attr.kicker}', 'Geist', 8, at('container', 'top-left', 0, 4),
    { ...caps(1.8), color: col('paper') }),
  // {titleText} keeps the \\ breaks from the Markdown, so each line ends at the writer's \\.
  // Moved left by the S's side bearing, so its curve meets the text edge.
  text('title', '{titleText}', 'Fraunces', 94,
    at('container', 'top-left', -S_BEARING, 15, 'fill'),
    { fontWeight: 900, lineHeight: 0.84, color: col('paper') }),
  { kind: 'box', id: 'initial', style: { backgroundColor: col('accent') }, reserve: false,
    placement: { anchor: { to: 'container', edge: 'top-left' }, offset: { y: pt(capsTop - pad) },
      size: { width: pt(MONO.advance * letter), height: pt(foot - capsTop + 2 * pad) } } },
  room('container', (TEXT_TOP * 25.4) / 72, 'top-left'), // the opener ends where the text starts
]);

The stack is written in the Markdown: # Say it \\ out \\ loud. breaks {titleText} at each \\, while running heads and the PDF outline read the title on one line (line breaks in titles). The field hangs from the top of the bleed, full width and 130 mm deep, and the lead starts on the first grid line 8 mm under it (TEXT_TOP). The boxed initial is two pieces. The style's dropCap sets the M over three lines of the lead, in white, and the opener paints a magenta box under it, placed from TEXT_TOP, the lead's leading and Geist Mono's metrics, so the colour above and below the M is as wide as its side bearings. The box takes no room in the opener (reserve: false); an empty room box ends the opener where the lead starts.

#4 · Give the manual a badge, two columns and its own heads

script.js · lines 181–208in full code
const SPEC = 18; // mm: both side margins, for a 144 mm text block in two columns
const PAD = 1.6; // mm of colour over the badge's figures
const spec = design([
  { kind: 'rule', id: 'top-rule', direction: 'horizontal', thickness: pt(2.4), color: col('ink'),
    placement: at('container', 'top-left', 0, 0, 'fill') },
  text('kicker', '{attr.kicker}', 'Geist Mono', 7.5, at('container', 'top-left', 0, 3.2),
    { ...caps(1.2, 500), color: col('graphite') }),
  // The badge's width follows its text, with padding on both sides, so 9 → 10 widens it. Its
  // line box holds 1 mm more under the figures than over them, so the foot pads 1 mm less.
  text('badge', '{chapterNumber}', 'Geist Mono', 26, at('container', 'top-left', 0, 12),
    { fontWeight: 700, lineHeight: 1, box: { backgroundColor: col('accent'), padding: {
      top: mm(PAD), right: mm(2.4), bottom: mm(PAD - 1), left: mm(2.4) } } }),
  // Same size and line height as the badge, dropped by its top padding, so the baselines meet.
  text('title', '{titleText}', 'Geist', 26, at('#badge', 'right-of', 4, PAD),
    { fontWeight: 700, lineHeight: 1 }),
  { kind: 'rule', id: 'hairline', direction: 'horizontal', thickness: pt(0.5), color: col('ink'),
    placement: at('container', 'top-left', 0, 31, 'fill') }, // room for a one-line title
]);
const specHead = (parity) => { // mono labels over the opener's rule, on every page of the section
  const [edge, s] = parity === 'even' ? ['top-left', 1] : ['top-right', -1];
  const mono = { ...caps(1.1, 500), color: col('graphite'), parity, pages: 'all' };
  return [
    text(`sp-page-${parity}`, '{pageNumber} / {totalPages}', 'Geist Mono', 7,
      at('container', edge, 0, HEAD_Y), mono),
    text(`sp-chap-${parity}`, '§ {chapterNumber} · {chapterTitle}', 'Geist Mono', 7,
      at('container', edge, s * 16, HEAD_Y), mono),
  ];
};

The badge is a text element with a fill and padding but no width, so its width follows {chapterNumber}, and the title is placed right-of it: at chapter 10 the badge grows by one figure, 5.5 mm, and the title moves with it. Its bottom padding is 1 mm less than the top because the line box holds that much more under the figures, so the 4 sits centred. In the spec style, layout sets the section in two columns and the level's span: 'page' runs the opener across both. Its margins widen the text block to 144 mm, bodyStyle sets ragged Geist on the book's 14.5 pt grid, header replaces the book's running heads and an empty footer drops the folio.

#5 · Leave the coda room

script.js · lines 212–221in full code
const quiet = design([ // the kicker is in the accent, which this style turns graphite
  text('kicker', '{attr.kicker}', 'Geist', 7.5, at('container', 'top-left', 0, 92),
    caps(1.6, 500)),
  text('title', '{titleText}', 'Fraunces', 26, at('#kicker', 'below', 0, 3),
    { fontWeight: 300, lineHeight: 1.1 }),
  room('#title', 3), // and the initial's rise, in whole lines: the text 16 mm under the title
]);
const tiny = (parity, edge) => text(`tiny-${parity}`, '{pageNumber}', 'Geist', 6.5,
  at('container', edge, 0, -12), { fontWeight: 500, color: col('muted'), parity });
const tinyFolio = { elements: [tiny('even', 'bottom-left'), tiny('odd', 'bottom-right')] };

The kicker hangs 92 mm below the top of the text block, and the raised initial is the quiet style's one-line drop cap: its foot stands on the first baseline and, at 46 pt, it rises into the white above instead of sinking into the paragraph. The paragraph keeps that rise clear above it in whole lines, so the opener's room under the title is only 3 mm. In the quiet style, margins narrow the measure to 106 mm, bodyStyle sets the text ragged at 12.5/17.5 pt, footer swaps the drop folio for a small one at the outer corner and palette turns the kicker grey. Nothing on this page prints a chapter number, so numbered: false would show only in a chapter added after the coda, which would still be the fifth.

The whole recipe

Sandbox
// ═══ Postext Cookbook · Nº 017 · Five chapter openers in one book ═════════════════
// https://postext.dev/en/cookbook/five-chapter-openers
// Code: MIT · Text: original (CC BY 4.0) · Ornament: generated in code (CC BY 4.0)
// Fonts: Lora, Fraunces, Geist, Geist Mono (SIL OFL 1.1) · Needs postext ≥ 1.25.0
// A small book about openers, each chapter set in the one it describes. Every heading style
// brings its opener and palette; three move the margins, two bring their own type and folios.
import {
  buildDocumentWithFonts, renderPageToCanvas, registerResourceImage,
} from 'https://esm.sh/postext';

const LANG = 'en'; // @lang: the language of the sample document ('en' | 'es')
const RECIPE = 'five-chapter-openers';

// ─── 1 · Design ─────────────────────────────────────────────────────────────
// #region answer: one opener per chapter: the level's own, then heading styles by name
// A heading line picks a style by id, and its attributes feed the opener's {attr.…} texts:
//   # The Frame {style="framed" kicker="Chapter the Second"}
// A style overrides the level fields it names (opener, drop cap) and, until the next level-1
// heading, the section fields it names; the rest comes from the level and the document.
const level1 = () => ({ level: 1,
  // span 'page' opens each chapter at the head of a new page and runs its opener across
  // every column (chapter four has two). By default an H1 waits for a recto, with a blank
  // page before it; parity 'any' lets these short chapters open on either side.
  span: 'page',
  breakBefore: { enabled: true, parity: 'any' },
  marginBottom: pt(0), // each opener ends where its first paragraph starts
  advancedDesign: literary, // chapter one, unstyled: the book's own opener
  dropCap: { lines: 5, fontFamily: 'Fraunces', fontWeight: 400, color: col('accent'),
    gap: mm(2.5) } }); // a five-line drop in rubric red
const headingStyles = () => [ // factories, called by config() once every design exists
  // Each style brings its opener and swaps 'accent' for a colour of its own, on its pages.
  { id: 'framed', advancedDesign: framed, palette: { accent: palette.lapis },
    margins: { left: mm(EVEN), right: mm(EVEN) }, // a centred text block; 'accent' is blue:
    dropCap: { lines: 3, fontFamily: 'Fraunces', fontWeight: 600, color: col('accent'),
      gap: mm(2) } },
  { id: 'stacked', advancedDesign: stacked, palette: { accent: palette.magenta }, // white, on
    dropCap: { lines: BOXED, fontFamily: 'Geist Mono', fontWeight: 700, color: col('paper'),
      gap: mm(2.2) } }, // the square the opener draws for it
  { id: 'spec', advancedDesign: spec, palette: { accent: palette.signal }, dropCap: false,
    marginBottom: pt(0), // the hairline under the title is the separator
    margins: { left: mm(SPEC), right: mm(SPEC) }, // a 144 mm text block
    layout: { layoutType: 'double', gutterWidth: mm(7) },
    bodyStyle: { fontFamily: 'Geist', fontSize: pt(9.8), textAlign: 'left' }, // on the grid
    header: { elements: [...specHead('even'), ...specHead('odd')] },
    footer: { elements: [] } }, // no folio at the foot: the head gives the page
  { id: 'quiet', numbered: false, // a coda: it advances no chapter counter
    advancedDesign: quiet, palette: { accent: palette.graphite }, footer: tinyFolio,
    dropCap: { lines: 1, fontFamily: 'Fraunces', fontWeight: 300, fontSize: pt(46), // raised:

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

#Send every chapter to a recto

With 'odd', every chapter opens on a right-hand page and chapters two to five each get a blank verso before them, so the book grows from 7 to 11 pages.

-  breakBefore: { enabled: true, parity: 'any' },
+  breakBefore: { enabled: true, parity: 'odd' },

#Drop the initial three lines

With no fontSize, the size follows the number of lines, so a three-line initial still lines up with the first line's capitals.

-  dropCap: { lines: 5, fontFamily: 'Fraunces', fontWeight: 400, color: col('accent'),
+  dropCap: { lines: 3, fontFamily: 'Fraunces', fontWeight: 400, color: col('accent'),

#Small capitals after the initial

leadIn sets the first words after the letter in small capitals, as many novels do; words: 'line' takes the whole first line.

-    gap: mm(2.5) } });
+    gap: mm(2.5), leadIn: { words: 'line' } } });

Pitfalls

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 →

  • The leads are set ragged, as in a magazine opener; a justified lead beside a five-line initial can open its spaces wide on a line of few words.
  • The magenta box is fitted by hand to Lora 12/17, to Geist Mono's M and to the line's baseline, 0.8 down its line box (BASE). If you change the lead's face, size or leading, the letter, the number of lines or the depth of the field, check the box against the first line's capitals again.
  • Titles never rotate or run vertically: design text is always horizontal. Stack the words with \\ instead, as chapter three does.

Credits

Text
Original prose, CC BY 4.0
Fonts
Lora (SIL OFL 1.1) · Fraunces (SIL OFL 1.1) · Geist (SIL OFL 1.1) · Geist Mono (SIL OFL 1.1)
Sandbox