Writing
Print CSS
Making web pages print cleanly: print media queries, page size and margins, hiding what should not print, and exact-size output for PDFs.
Why print breaks
Print a web page and you will usually get a mess: the navigation bar across the top of every page, the sidebar squeezed into a narrow column, background colours either missing or ink-heavy, and content cut off at the page edge. The reason is simple. The page was designed for a screen, and the printer is not a screen. It has no hover, no scroll, no viewport width, and no back button. It has paper, which is a fixed size, and ink, which costs money.
Most sites never think about print at all, which is why most printouts look like an afterthought. But print is not a rare edge case. People print boarding passes, invoices, recipes, articles, forms and test pages. A page that prints badly is a page that fails at the moment someone needed it most: the moment they wanted it on paper.
Print CSS is the fix. It is a small set of CSS features, a media query, a page rule, and a handful of properties, that lets a page restyle itself for paper. It is not a separate skill from web CSS. It is web CSS, applied to a different medium.
@media print basics
The core of print CSS is the print media query. Everything inside it applies only when the page is printed, or shown in print preview. Everything outside it applies on screen. That separation is the whole technique: one page, two presentations, switched by the medium.
@media print {
body {
font-size: 12pt;
color: #000;
background: #fff;
}
}
A few things to notice. Font sizes in print are usually set in points, not pixels, because points are a print unit: 12pt is a comfortable reading size on paper. Colours are set explicitly, because browsers strip backgrounds by default when printing, and a page that relies on a background colour for contrast can become unreadable. And the print rules are short. Most of the work is removing things, not adding them.
Start with the structure. A print stylesheet usually does three things: hide what should not print, widen what should, and set the page geometry. The rest is detail.
@page size and margins
The @page rule controls the printed page itself: its size and its margins. This is where A4 and US Letter enter the picture, and where the A4 vs US Letter guide becomes practical rather than theoretical.
@page {
size: A4;
margin: 20mm 15mm;
}
The size property sets the page size the document is designed for. Set it to A4, and the browser will lay out the page for A4. Set it to Letter, and it will lay out for Letter. If the size in the CSS does not match the paper in the tray, the print dialog will scale or crop, which is exactly the problem the A4 guide describes. The fix is the same in both places: make the size match the paper.
Margins matter more in print than on screen. A screen has no edge, but paper does, and content that runs to the edge of the page gets cut off by the printer's unprintable margin. Set explicit margins in the @page rule, and keep important content inside them. Twenty millimetres top and bottom, fifteen at the sides, is a sensible default for A4. For US Letter, three quarters of an inch all round is the equivalent.
You can also set different margins for the first page, or for left and right pages, using @page :first and @page :left and @page :right. That is useful for documents, less so for web pages, but the capability is there when you need it.
Hiding what should not print
Navigation, sidebars, footers, cookie banners, social buttons, search boxes, and anything else that exists to move the reader around the site has no place on paper. A printed navigation bar is useless: you cannot click paper. A printed cookie banner is worse than useless: it takes up space and says nothing.
@media print {
.site-header,
.site-nav,
.site-footer,
.no-print {
display: none;
}
.wrap {
max-width: none;
padding: 0;
}
}
Hide the chrome, then widen the content. A screen layout with a 720px content column and wide margins wastes paper. In print, let the content use the full printable width. Remove the max-width, remove the side padding, and let the text fill the page. The reader printed the page to read it, not to admire the whitespace.
Links need attention too. A link on paper is just underlined text, and the reader cannot follow it. For a printed document, it is worth showing the URL after the link text, so the reader can type it in later. That is a matter of taste and of document length: a page with fifty links becomes unreadable if every one carries its URL. For a short document, though, it is genuinely useful.
Exact-size output for PDFs
The hardest print CSS problem is exact-size output: a page that must print at a precise physical size, with elements at precise positions. A test page is the extreme case. Its alignment grid, its colour blocks and its text sizes are all placed at exact millimetre positions, and if the print is scaled by even a few percent, the sheet stops measuring what it claims to measure.
The rules for exact-size output are strict. Set the @page size to match the paper exactly. Set margins explicitly, and account for the printer's unprintable margin. Use millimetres or points for sizes, not pixels, because pixels have no fixed physical size. And tell the reader to print at 100 percent, with no scaling, because no CSS can stop a print dialog from scaling the page if the reader asks it to.
This is the craft behind the tool's output: exact-size PDFs in A4 and US Letter, designed so that what you print is what was designed. The PDFs are the reliable route, because a PDF carries its page size with it, and the print dialog can be told to print it at actual size. A web page printed from the browser is always at the mercy of the browser's print settings, which is why the sheets are PDFs, not web pages.
Testing without wasting paper
You do not need to print a hundred test pages to get print CSS right. The browser's print preview shows exactly what the printer will produce, including the page breaks, the margins, and the hidden elements. Use it as the primary test: make a change, open print preview, look at it, repeat. Print preview is fast, free, and honest.
For a second check, save as PDF from the print dialog. The PDF shows the same layout, and it can be inspected at any zoom level, which is useful for checking fine details like small text and thin lines. It also shows whether the page size is right: a PDF saved from an A4 layout should be an A4 PDF, and a Letter layout should be a Letter PDF. If the PDF is the wrong size, the @page rule is wrong.
Print one real page at the end, on the paper you intend to use, to check the things a screen cannot show: how the ink sits on the paper, whether the colours are right, and whether the margins are comfortable. One real print is worth a dozen previews for those questions. But the previews should come first, because they catch the structural problems, the missing content, the wrong page breaks, the elements that should be hidden, before any paper is wasted.
Related reading on this site: the printer help hub for the guides that use exact-size output, the guide to A4 and US Letter for the paper-size problem, and Fast Static Sites for why this site puts its content in the payload.