allenfrostline

How to Include Page Breaks to Markdown Files?


2024-01-20

It’s really strange but I have never thought about this question until recently, when I tried to export a Markdown file to PDF and realized myself to be adjusting the number <br> tags so that some answers don’t show up on the same page as questions. “Wait a minute”, I actually said out loud, “have I been doing this the whole time”?

Both you and I know this is stupid and should stop. Luckily there is a quite elegant solution. Since majority of Markdown previewers are HTML renderers, we can utilize the CSS property1 page-break-after by adding this line to where we need the page break:

<div style="page-break-after:always"></div>

This invisible line will do nothing to your existing Markdown document, but will break apart whatever is before and after it when you print the preview. Having been hacking my way all these years, I’m now embarrassingly happy with this “new” trick.


  1. It seems MDN is confident that break-after has depreciated page-break-after. However, it’s apparently not the case on the latest Safari (break-after didn’t even work when I tried it). ↩︎