Dev Helpers

React / Text formatting

Line-preserving caption

Dev Helpers · April 15, 2026

Captions may contain deliberate newlines from an editor. A whitespace rule can preserve them without injecting HTML into the component.

Worked example

// components/LinePreservingCaption.tsx

export default function LinePreservingCaption({
  text = "Studio notes from the harbour",
}: { text?: string } = {}) {
  return <p style={{ whiteSpace: "pre-line", overflowWrap: "anywhere" }}>{text}</p>;
}

Implementation notes

React treats interpolated strings as text. Keeping line preservation in CSS avoids turning a caption editor into an HTML editor.

Try it yourself

Pass literal angle brackets and several consecutive newlines.

Open the React example →

Editorial date April 15, 2026 · Published December 6, 2026

Related articles

Percentage meter

A percentage meter needs both a bounded visual fill and a meaningful textual value. Handle a zero denominator before dividing.

Vertical typesetting sample

Vertical typesetting changes the reading direction and available inline space. Treat the sample text as content so it remains selectable and accessible.

Square cover crop

A square image crop gives a card grid consistent geometry. The crop anchor determines whether the most important part of a photo survives.