Margin Box Positioning Preferences
Goal: Allow margin boxes to express placement preferences (vertical position, horizontal side, priority) while preserving the existing collision-avoidance system.
Preference Syntax
Preferences are expressed via data-margin attribute with hyphen-separated tokens:
<div class="margin-box" data-margin="top-right-high">...</div>
<div class="margin-box" data-margin="left">...</div>
<div class="margin-box" data-margin="bottom-low">...</div>
Tokens:
| Type | Options | Default (omitted) |
|---|---|---|
| Vertical | top, bottom, near |
near if inline, no-preference if external |
| Horizontal | left, right |
no preference |
| Priority | high, low |
normal |
Order doesn’t matter: right-top-high = top-right-high
Parsed preferences object:
{
vertical: 'top' | 'bottom' | 'near' | null, // null = no preference
horizontal: 'left' | 'right' | null,
priority: 'high' | 'normal' | 'low'
}
Defaults:
- Inline boxes (defined in body text): { vertical: 'near', horizontal: null, priority: 'normal' }
- External boxes (from site-wide pool): { vertical: null, horizontal: null, priority: 'normal' }
Placement Direction
Vertical preferences determine placement direction, not zones:
top: Place from the top of the margin, filling downwardbottom: Place from the bottom of the body text, filling upwardnear: Anchor to the definition point in body text- no preference: Fill largest remaining gaps
Priority System
Placement order (earlier = better position):
- Sidenotes — always first, must align with anchors
- High priority boxes — placed next, locked in position
- Normal priority boxes — placed next, flexible for redistribution
- Low priority boxes — placed last, most flexible
Within each tier, document order determines precedence.
Priority interaction with direction:
For top boxes (filling down):
- top-high: gets the very top
- top-normal: placed below high
- top-low: fills in below those
For bottom boxes (filling up):
- bottom-high: gets the very bottom
- bottom-normal: placed above high
- bottom-low: fills in above those
High priority claims the extremes; lower priority gets pushed toward the middle.
Side Selection
When a box has no horizontal preference:
topboxes: choose whichever margin allows higher placementbottomboxes: choose whichever margin allows lower placementnearboxes: use existing alternation heuristic (or closest to anchor)- no-preference boxes: fill wherever gaps exist
When horizontal preference is specified, respect it (may result in suboptimal vertical position).
Boundary Constraint
Margin content should not extend below the end of body text:
bottomboxes fill upward from the body text’s end, not from infinite space- Sidenotes may violate this constraint if necessary to stay near their anchors
- All other content respects the boundary
Redistribution Phase
After initial placement, normal and low priority boxes are redistributed for even spacing:
- High priority boxes remain locked
- Calculate gaps between all placed elements (sidenotes + boxes)
- Shift flexible boxes to balance spacing
- Preserve relative order within each margin
- Respect the “don’t overhang body text” constraint
Placement Algorithm
1. SIDENOTES (unchanged, highest priority)
For each footnote in document order:
- Target position = anchor's vertical offset
- If collision with earlier sidenote, push down
- Alternate left/right margins
- Record occupied space
2. MARGIN BOXES by priority tier (high → normal → low)
Within each tier, partition by vertical preference:
a. TOP boxes (document order):
- Start at top of margin
- Place each box in first available slot, filling down
- If no horizontal preference, pick margin allowing highest placement
b. BOTTOM boxes (document order):
- Start at bottom of body text
- Place each box in first available slot, filling up
- If no horizontal preference, pick margin allowing lowest placement
c. NEAR boxes (document order):
- Target position = definition point in body
- Find closest available slot to target
d. NO-PREFERENCE boxes (document order):
- Fill largest remaining gaps
Repeat for next priority tier.
3. REDISTRIBUTE (normal and low priority only)
- High priority boxes are locked
- Calculate gaps between all placed elements
- Shift flexible boxes to even out spacing
- Preserve relative order within each margin
- Do not extend below body text
Examples
<!-- Premium ad: top-right, won't be moved -->
<div class="margin-box" data-margin="top-right-high">
<img src="/ads/premium.png" alt="Premium Sponsor">
</div>
<!-- Pull quote: wants to be near its context, flexible for redistribution -->
<div class="margin-box" data-margin="near">
<em>"The moon has no atmosphere to scatter light..."</em>
</div>
<!-- Decorative closer: bottom of page, very flexible -->
<div class="margin-box" data-margin="bottom-low">
❧ Finis coronat opus
</div>
<!-- External ad from pool: prefers left side, normal priority -->
<div class="margin-box" data-margin="left">
<img src="/ads/tonic.png" alt="Buy Almanack Tonics">
</div>
<!-- Inline box with no preferences: algorithm's choice -->
<div class="margin-box">
<strong>Did you know?</strong> The regolith is 4-5 meters deep.
</div>
Implementation Notes
Parsing data-margin
function parseMarginPreferences(element, isInline = true) {
const attr = element.dataset.margin || '';
const tokens = attr.toLowerCase().split('-').filter(Boolean);
const verticalOptions = ['top', 'bottom', 'near'];
const horizontalOptions = ['left', 'right'];
const priorityOptions = ['high', 'low'];
return {
vertical: tokens.find(t => verticalOptions.includes(t))
|| (isInline ? 'near' : null),
horizontal: tokens.find(t => horizontalOptions.includes(t)) || null,
priority: tokens.find(t => priorityOptions.includes(t)) || 'normal'
};
}
Changes to sidenotes.js
- Add
parseMarginPreferences()function - Modify box collection to parse and store preferences
- Rewrite
positionMarginBoxes()to implement tiered placement - Add
redistributeBoxes()phase after initial placement - Update side-selection logic for
top/bottomboxes
CSS (unchanged)
No CSS changes required — positioning remains absolute within margin containers.
Future Considerations
- Markdown syntax for specifying preferences inline (e.g.,
{.margin-box margin="top-high"}) - Pool management UI for site-wide boxes
- Analytics on which positions perform best (for ads)