Full answer
People still print or save as PDF: certificates, fee receipts, timetables and articles. A print stylesheet changes the page for paper without touching the screen version. Write it with @media print { ... }, or a separate file loaded with media="print".
Common jobs:
- Hide things that make no sense on paper: navigation, buttons, video, ads, cookie banners.
- Use dark text on a white background and drop large background images. Browsers often skip backgrounds by default to save ink.
- Show where links go:
a[href]::after { content: " (" attr(href) ")"; }. Skip it for fragment links andjavascript:links. - Control page breaks:
break-inside: avoidkeeps a card or table row together,break-after: pageforces a new page, andbreak-after: avoidon headings keeps a heading with the text after it. - Set page size and margins with
@page { margin: 2cm; }. - Units from the paper world, such as
pt,cmandin, make sense in print. Layouts based onvwdo not.
You can test without printing. DevTools has a print media emulation option, and the checks for this example emulate the media type.
On screen the navigation and the Print button are visible. When the page is printed, they disappear, the link shows its full address after the text, each receipt stays on one page, and the heading stays with the content that follows.
Always preview with the browser's print dialog, because margins, headers and footers differ between browsers.
<style>
body { font: 16px/1.5 Georgia, serif; }
nav, .no-print { display: block; background: #eee; padding: 8px; }
.receipt { border: 1px solid #333; padding: 8px; margin-bottom: 8px; }
@page { margin: 2cm; }
@media print {
nav, .no-print { display: none; }
body { color: #000; background: #fff; font-size: 12pt; }
a[href^="http"]::after { content: " (" attr(href) ")"; font-size: 10pt; }
.receipt { break-inside: avoid; }
h2 { break-after: avoid; }
}
</style>
<nav id="nav">Home | Fees | Contact</nav>
<h2 id="h2">Fee receipts</h2>
<div class="receipt" id="r1">Receipt 1001 for Asha</div>
<p>Read the <a id="link" href="https://example.com/policy">refund policy</a>.</p>
<button class="no-print" id="btn" type="button">Print</button>The check used print media emulation in Chrome, not a real printer or print preview, so margins, headers and footers were not checked. Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.
Key points
- @media print applies only when printing or saving as PDF.
- Hide navigation, buttons and ads for paper.
- a[href]::after with attr(href) prints link addresses.
- break-inside, break-after and @page control pagination.
- Do not depend on background colors for meaning in print.
Common mistakes
- Printing the page with the sticky header and fixed elements that repeat or cover content on every page.
- Forgetting break-inside: avoid, so a table row or card is split across two pages.
- Showing href for every link, including # links and javascript: links, which print as noise.
Follow-ups
- How do you force a new page before each chapter?
- How can you show a link's address next to its text in print?
Tips
- Mention emulating print media in DevTools. It proves you test the result.
- Say that backgrounds are often not printed by default, so important information must not depend on them.