Margins and Sidenotes Design
Goal: Add left and right margin columns to article pages, containing sidenotes (footnotes repositioned to align with their anchors) and margin boxes (ads, quotes, decorative elements).
Credits & Attribution
This design builds on the work of others:
-
Said Achmiz wrote sidenotes.js, the JavaScript implementation we plan to adapt. The code is MIT licensed. Said’s implementation handles the hard problems: runtime layout optimization, collision avoidance, viewport-aware positioning, and graceful degradation.
-
Gwern Branwen uses sidenotes.js extensively on gwern.net, which served as our primary reference for how sidenotes work in practice. Gwern’s sidenote documentation explains the design rationale.
-
Edward Tufte popularized sidenotes in his books on information design. Tufte CSS by Dave Liepmann is another influential sidenote implementation, though we’re following the gwern.net approach for its more sophisticated positioning algorithm.
Our extensions to this foundation: margin boxes (non-anchored decorative content), two-margin layout, and integration with Django/Pandoc static site generation.
Design Decisions
| Question | Decision |
|---|---|
| Layout approach | CSS Grid three-column layout |
| Sidenote source | Standard Pandoc footnotes, transformed by JS |
| Margin box source | Scattered <aside> elements in body, repositioned by JS |
| Sidenote state | Collapsible/expandable, reader preference via localStorage |
| Positioning priority | Sidenotes first (must align with anchors), boxes fill gaps |
| Collision handling | Sidenotes push each other down; boxes yield to sidenotes |
| Mobile behavior | Swipe-to-reveal margins (see mobile margins), no-JS fallback: margins hidden |
| No-JS fallback | Standard footnotes at bottom, boxes inline in body |
| Front page | Shows full current article with sidenotes and margin boxes (same as article pages) |
HTML Structure
<div class="margins-layout">
<aside class="margin margin-left">
<!-- Sidenotes and boxes positioned here by JS -->
</aside>
<article class="body-text">
<p>Main text with <a class="footnote-ref" href="#fn:1">1</a> inline...</p>
<aside class="margin-box" data-type="quote">
<blockquote>"A pithy saying"</blockquote>
</aside>
<p>More body text...</p>
<aside class="margin-box" data-type="ad">
<img src="/assets/ads/tonic.png" alt="Tonic">
</aside>
<p>Continuing text...</p>
</article>
<aside class="margin margin-right">
<!-- Sidenotes and boxes positioned here by JS -->
</aside>
</div>
<!-- Standard Pandoc footnotes (hidden when sidenotes active, kept for a11y) -->
<div class="footnotes">
<ol>
<li id="fn:1"><p>Footnote content...</p></li>
</ol>
</div>
Margin boxes scattered through the body provide meaningful placement for no-JS/mobile readers. JS moves them to margin columns when available.
Visual Layout
┌─────────────────────────────────────────────────────────────────────┐
│ Since 4000 BCE ALMNCK. January 1st │
╞═════════════════════════════════════════════════════════════════════╡
│ │
│ ┌─────────────┐ LOOK BACK UP ┌─────────────┐ │
│ │ "Buy │ │ 1. The year │ │
│ │ Almanack │ [T]he year ends, the │ ends... │ │
│ │ Brand │ North Pole tips its │ (sidenote │ │
│ │ Tonics" │ deepest bow to the │ aligned │ │
│ │ │ darkness, and we see¹ │ with ¹) │ │
│ │ —Satisfied │ even large language └─────────────┘ │
│ │ Customer │ models have been taking │
│ └─────────────┘ it easy for the winter. ┌─────────────┐ │
│ (margin box) │ ┌─────────┐ │ │
│ But for the faithful │ │ PATENT │ │ │
│ ┌─────────────┐ maintainer of systems, │ │ REMEDY │ │ │
│ │ 2. CD now │ there's still work to │ └─────────┘ │ │
│ │ stands for │ be done, here in the │ (ad box) │ │
│ │ Cretaceous │ cooling embers² of the └─────────────┘ │
│ │ Disk... │ year. │
│ └─────────────┘ │
│ (sidenote ²) │
│ │
└─────────────────────────────────────────────────────────────────────┘
- Both margins hold sidenotes and boxes
- Sidenotes align vertically with their anchor superscripts
- Boxes fill remaining vertical space
- Boxes yield when sidenotes expand
CSS Grid Layout
.margins-layout {
display: grid;
grid-template-columns:
minmax(10rem, 1fr) /* left margin */
minmax(30rem, 43rem) /* body text */
minmax(10rem, 1fr); /* right margin */
gap: 2rem;
max-width: 80rem;
margin: 0 auto;
}
.margin {
position: relative;
font-size: 0.85rem;
line-height: 1.4;
}
/* No-JS / mobile: boxes appear inline */
.margin-box {
margin: 1.5em auto;
max-width: 70%;
padding: 1em;
border-top: 1px solid #1a1a1a;
border-bottom: 1px solid #1a1a1a;
}
/* JS enabled: boxes get absolutely positioned */
.sidenotes-enabled .margin-box {
position: absolute;
width: 100%;
margin: 0;
max-width: none;
}
@media (max-width: 900px) {
.margins-layout {
grid-template-columns: 1fr;
}
.margin {
display: none;
}
}
Sidenote Elements
Generated HTML:
<div class="sidenote" id="sn:1" data-anchor="fnref:1">
<span class="sidenote-number">1</span>
<div class="sidenote-content">
<p>The footnote content...</p>
</div>
</div>
Styling:
.sidenote {
position: absolute;
width: 100%;
font-family: "Cormorant Garamond", Georgia, serif;
font-size: 0.85rem;
line-height: 1.4;
}
.sidenote-number {
font-weight: 600;
margin-right: 0.3em;
}
.sidenote.collapsed .sidenote-content {
display: none;
}
.sidenote.collapsed::after {
content: "...";
color: #666;
}
Margin Box Types
.margin-box[data-type="quote"] {
font-style: italic;
border-left: 2px solid #1a1a1a;
padding-left: 0.5em;
}
.margin-box[data-type="ad"] {
border: 1px solid #1a1a1a;
padding: 0.5em;
text-align: center;
}
.margin-box[data-type="aphorism"] {
font-family: "Fell English", serif;
text-align: center;
}
Reader Preference Toggle
Small control in masthead or floating corner:
<div class="sidenote-toggle">
<button data-mode="expanded" class="active" title="Show sidenotes">¶</button>
<button data-mode="collapsed" title="Hide sidenotes">※</button>
</div>
State persisted to localStorage, applied on page load.
JavaScript Behavior
Initialization
- Add
sidenotes-enabledclass to body - Find all
.footnote-refelements in body - For each footnote, create sidenote element from corresponding
#fn:N - Find all
.margin-boxelements, record their original positions - Move boxes out of body into margin asides
- Run positioning algorithm
- Apply saved reader preference (expanded/collapsed)
Positioning Algorithm
for each sidenote (in document order):
desired_top = anchor element's vertical position
target_margin = alternate left/right (or smarter heuristic)
if overlaps previous sidenote in same margin:
actual_top = previous sidenote bottom + spacing
else:
actual_top = desired_top
place sidenote at actual_top in target_margin
record occupied range
for each margin box (in document order):
original_top = where it appeared in body
find largest gap in preferred margin near original_top
place box in that gap
record occupied range
Event Handling
- Window resize: Debounced recalculation via requestIdleCallback
- Sidenote expand/collapse: Recalculate positions below affected sidenote
- Toggle click: Update all sidenotes, persist preference, reflow boxes
- Hash navigation: Scroll to and highlight targeted sidenote
Full-Bleed Elements
Elements marked .full-bleed span all three columns and act as obstacles:
.full-bleed {
grid-column: 1 / -1;
width: 100%;
margin: 2rem 0;
}
In markdown:
{.full-bleed}
JS detects these and marks their vertical ranges as proscribed for sidenote/box placement.
Progressive Enhancement
| State | Behavior |
|---|---|
| No JS | Footnotes at bottom, boxes inline in body text |
| JS + narrow viewport (non-touch) | Margins hidden, footnotes shown, boxes inline |
| JS + touch device | Swipeable three-column layout (see mobile margins) |
| JS + wide viewport | Full three-column layout with positioned sidenotes and boxes |
File Structure
assets/css/
almanack.css # Extend with margins-layout, sidenote/box styles
assets/js/
sidenotes.js # Adapted from gwern.net, extended for margin boxes
src/almnck/templates/
base.html # Add toggle UI, JS include
units/article.html # Wrap in margins-layout, scatter boxes
Implementation Notes
Decisions made during implementation that differ from or extend the original design:
Django Footnote Markup
Django/Pandoc places the footnote id on the parent <sup> element, not the <a> link itself:
<sup id="fnref:winterbreak"><a class="footnote-ref" href="#fn:winterbreak">1</a></sup>
The JS handles this with: footnoteRef.parentElement?.id
Sidenote Positioning
- Positions are calculated relative to margin container offset, not document absolute
- Sidenotes alternate right/left, starting with right (simple alternation, not content-aware)
- When sidenotes would overlap, later ones push down (collision avoidance works)
Margin Box Positioning (Fixed)
Original algorithm only placed boxes if space existed within 200px of original position. Fixed to always place boxes in whichever margin can position them closest to their original location—even if that’s far down the page.
Click Interactions
| Click Target | Behavior |
|---|---|
| Footnote ref (superscript) | Toggle that sidenote’s collapsed state |
| Sidenote body | Toggle its collapsed state |
| Either | Triggers repositioning of all sidenotes |
When a sidenote is expanded, it gets briefly highlighted (2 seconds) to draw attention.
CSS Fixes Required
/* Override main padding to let grid span full width */
main:has(.margins-layout) {
padding-left: 0;
padding-right: 0;
}
/* Hide footnotes section when sidenotes are active */
.sidenotes-enabled .footnote {
display: none;
}
Margin Box Markup (Simplified)
We simplified to just:
<div class="margin-box">
<strong>TITLE</strong><br>
<em>"Quote text"</em><br>
—Attribution
</div>
No data-type attributes needed for basic functionality. Can be added later for styling variants.
Future Work (Not In Scope)
- ~~Mobile swipe-to-reveal margins~~ → Implemented 2026-01-24 (see mobile margins implementation)
- Per-article author default for expanded/collapsed
- Margin box pool/rotation system
- In-markdown syntax for article-specific boxes
- Cookie-based preference (broader user config system)