Print Styles: @media print, @page, Page Breaks and Margin Boxes
Making pages print well: @media print, hiding screen-only parts, showing link URLs, @page size and margins, page selectors and named pages, break-before and break-inside, orphans and widows, page-number margin boxes, and print-color-adjust.
kvi5hnuCode Sprout team member8 min readOn this page
People still print web pages, or save them as PDF: recipes, tickets, invoices, articles, documentation. Without print styles, they get navigation bars, ads, cookie banners and a layout designed for a screen. A short print stylesheet fixes most of that, and CSS has a full paged media model for documents that need precise pages.
@media print
@media print {
nav, .site-header, .site-footer, .ads, .cookie-banner, video, button { display: none; }
body { font: 11pt/1.5 Georgia, serif; color: #000; background: none; }
main { inline-size: auto; margin: 0; } /* use the full page width */
a { color: inherit; }
a[href^="http"]::after { content: " (" attr(href) ")"; font-size: 0.9em; overflow-wrap: anywhere; }
}
Or keep the print rules in their own file: <link rel="stylesheet" href="print.css" media="print">. Browsers download it anyway, at low priority, so it doesn't slow the page.
What a good print stylesheet does:
- Hides interactive and decorative parts: navigation, menus, forms, buttons, videos, sidebars.
- Uses the full width: removes fixed widths, columns meant for screens, and
position: fixedorstickyelements, which can repeat on every page or cover content. - Shows link destinations, since paper can't be clicked. Tested in Chrome's PDF output:
a[href^="http"]::after { content: " (" attr(href) ")" }prints "the source (https://example.org/x)". Restricting it tohttplinks prints only external addresses. To include links within the site too, usea[href]:not([href^="#"])::after;attr(href)prints the address as written, so a relative link prints as/recipes/lemon-tart, not a full URL. Either way, in-page#links are skipped. - Uses print units:
ptfor type,mmorcmfor page geometry. On paper, they're exact. - Removes backgrounds and shadows, which browsers don't print by default and which waste ink.
Test it with the browser's print preview, or in DevTools with Rendering → Emulate CSS media type: print.

The printed side is page 1 of Chrome's PDF output for this page. Only the external link gets its address, because the selector is a[href^="http"], and the badge keeps its background only because of print-color-adjust: exact.
@page: page size and margins
The @page rule styles the page boxes themselves:
@page {
size: A4; /* or letter, A5, 210mm 297mm, A4 landscape */
margin: 20mm 18mm;
}
| Descriptor | Values |
|---|---|
size | auto, A3–A5, B4, B5, letter, legal, ledger, a width and height, plus portrait or landscape; other names such as A6 are ignored in Chrome, which falls back to the default size |
margin, margin-top … | the page margins, where the margin boxes live |
page-orientation | rotates the page in the output: Chrome and Firefox |
@page works in all browsers; size since Firefox 95 and Safari 18.2. The printer and the user's print dialog can still override the size.
Page selectors and named pages
@page :first { margin-top: 40mm; } /* the first page */
@page :left { margin-left: 25mm; } /* left-hand pages, for double-sided printing */
@page :right { margin-right: 25mm; }
:left and :right aren't supported in Firefox.
A named page lets part of the document use a different page style, through the page property:
@page wide { size: A4 landscape; }
.data-appendix { page: wide; }
Tested in Chrome: an element with page: wide was printed on its own landscape page. A page break is forced wherever the page name changes, so consecutive page: wide elements share landscape pages, and the document returns to the default page after them.
Page breaks
The break-* properties control where pages break, and work in multi-column layout too (article 73):
| Property | Values for print |
|---|---|
break-before, break-after | auto, page, avoid, avoid-page, left, right, recto, verso |
break-inside | auto, avoid, avoid-page |
@media print {
h2 { break-before: page; } /* each chapter starts a new page */
h2, h3 { break-after: avoid; } /* keep headings with their text */
figure, table, pre, blockquote { break-inside: avoid; } /* don't split them */
}
Tested in Chrome's PDF output: h2 { break-before: page } started each section on a new page. avoid values are requests, not guarantees: an element taller than a page must break somewhere. The old page-break-before, page-break-after and page-break-inside properties still work as aliases.
Orphans and widows
orphans is the minimum number of a paragraph's lines left at the bottom of a page before a break; widows is the minimum carried to the top of the next page. Both default to 2:
p { orphans: 3; widows: 3; }
They're supported in Chrome and Safari, not Firefox.
Margin boxes: headers, footers and page numbers

Inside @page, margin box rules put content into the page margin, like running headers and page numbers:
@page {
margin: 20mm;
@top-left { content: "Annual report 2026"; font: 9pt sans-serif; color: #555; }
@bottom-center { content: "Page " counter(page) " of " counter(pages); font: 9pt sans-serif; }
}
@page :first {
@top-left { content: none; }
@bottom-center { content: none; }
}
counter(page) is the current page number, and counter(pages) the total. Tested in Chrome's PDF output: pages 2 to 4 of a four-page document showed "Page 2 of 4" to "Page 4 of 4", and :first removed it from page 1.
Margin boxes are supported in Chrome 131+ only. Firefox and Safari print their own headers and footers, controlled by the user in the print dialog.
Colors and backgrounds: print-color-adjust
Browsers don't print backgrounds by default, to save ink, and may lighten colors. For elements where color carries meaning, like a chart legend, a colored status badge or a code highlight, ask for exact colors:
.legend-swatch, .badge { print-color-adjust: exact; }
economy (the default) lets the browser adjust; exact asks it to keep colors and backgrounds. It's supported everywhere, unprefixed in Chrome since 136; the user's "background graphics" setting can still override it.
body { font: 0.8rem system-ui, sans-serif; margin: 1rem; }
.page { margin-top: 0.5rem; padding: 0.75rem; border-radius: 8px; background: #1f2933; color: #f5f7fa; }
nav { padding: 0.4rem; margin-bottom: 0.5rem; background: #8a3b12; border-radius: 4px; }
a { color: #ffb38a; }
button { font: inherit; margin-top: 0.5rem; }
h1 { margin: 0 0 0.4rem; font-size: 1.3rem; }
/* the same rules a print stylesheet would hold, applied with a class for this preview */
.page.print { background: #fff; color: #000; font: 10pt/1.5 Georgia, serif; border: 1px solid #ccc; border-radius: 0; }
.page.print nav, .page.print button { display: none; }
.page.print a { color: inherit; }
.page.print a[href]:not([href^="#"])::after { content: " (" attr(href) ")"; font-size: 0.85em; overflow-wrap: anywhere; }
The checkbox applies the print rules with a class, since a preview can't switch to print media. The relative link gets its address printed, and the in-page #method link doesn't. On a real site, the same rules go inside @media print.
What's current
@pagewithsizeand margins: all browsers (Safari 18.2).- Margin boxes and
counter(pages): Chrome 131+. - Named pages with
page: all browsers. print-color-adjust: all browsers, unprefixed.- For complex print documents, like books with footnotes and cross-references, dedicated paged-media tools that render HTML and CSS to PDF support more of the specification than browsers.
Common questions
Why don't my backgrounds print?
Browsers skip backgrounds by default. Use print-color-adjust: exact where color matters; the user's print settings can still override it.
How do I start each section on a new page?
h2 { break-before: page; } inside @media print.
How do I add page numbers?
@page { @bottom-center { content: counter(page); } }, in Chrome. In other browsers, the user's print dialog controls headers and footers.
Why is my fixed header printed on every page?
position: fixed elements repeat on each printed page; tested in Chrome's PDF output, a fixed box appeared on both pages of a two-page print. Hide them, or set position: static, in the print styles.
What happens to a long table that spans several pages?
The <thead> and <tfoot> rows repeat at the top and bottom of every printed page. Tested in Chrome's PDF output, a 79-row table split over two A5 pages had its header and footer row on both. That's one more reason to mark up header rows as <thead> rather than styling the first <tr>.
Are transitions and animations printed?
Print shows the page as it is at that moment, frozen. Tested in Chrome's PDF output, a transition that had just started printed at its end value, but an animation 30 seconds long printed at its current frame, still nearly transparent. Add *, *::before, *::after { animation: none !important; } to the print styles, so nothing is caught halfway through a fade-in. This assumes the base styles describe the visible state; an element whose base style is opacity: 0 and that relies on animation-fill-mode: forwards would disappear.
Summary
@media printhides screen-only parts, uses the full width, and shows link URLs with::afterandattr(href).@pagesetssizeandmargin;:first,:left,:rightand named pages (thepageproperty) vary them.break-before,break-afterandbreak-insidecontrol page breaks;orphansandwidowscontrol split paragraphs.- Margin boxes like
@bottom-centerwithcounter(page)add headers, footers and page numbers, in Chrome. print-color-adjust: exactkeeps meaningful colors; test with print preview and media emulation.
Practice questions
Write print styles that hide the navigation and footer, and keep figures from splitting across pages.
Answer
@media print {
nav, footer { display: none; }
figure { break-inside: avoid; }
}
Print an A4 page with 2cm margins, and a page number in the bottom-right corner area, except on the first page.
Answer
@page {
size: A4;
margin: 2cm;
@bottom-right { content: counter(page); }
}
@page :first {
@bottom-right { content: none; }
}
Margin boxes work in Chrome 131+.
Why does this print the URL after in-page links like #section-2 too, and how do you fix it?
a::after { content: " (" attr(href) ")"; }
Answer
It matches every link. Restrict it to external links with a[href^="http"]::after { … }, or exclude only in-page links with a[href]:not([href^="#"])::after { … }.
A wide table should be printed in landscape while the rest of the document stays portrait. How?
Answer
Use a named page:
@page wide { size: A4 landscape; }
.wide-table { page: wide; }
The table starts on a new landscape page, and the document continues in portrait after it.
Review quiz
Test yourself on Part 11 with the CSS Part 11 review quiz.
Previous: Feature Queries in Depth: @supports, selector() and font-tech()
Next: Font Families: Generic Families, Font Stacks and System Fonts

More in CSS Course
Quiz: CSS Delicate Details
Twenty-five questions on the edge cases that surprise experienced developers: invalid custom properties, shorthands that reset values, layer order with !important, containing blocks, margin collapsing, specificity of :is() and :where(), intrinsic minimum sizes, and more, each checked in Chrome.
Quiz: CSS Interview Questions
Thirty front-end interview questions: cascade, specificity and layers, box model and margin collapsing, stacking contexts, positioning, flexbox and grid, responsive design, custom properties, performance, accessibility, modern selectors and architecture.
Quiz: CSS Course Final Exam
Thirty questions across the whole CSS course: syntax and error handling, selectors, the cascade and layers, values and units, color, the box model, backgrounds, positioning, flexbox, grid, responsive design, typography, generated content, effects, transforms, motion and architecture.
Quiz: CSS Architecture, Tooling and Debugging (CSS Part 17)
Twenty questions on modular CSS and BEM, layering a stylesheet, design tokens and theming, pattern libraries and safe refactoring, preprocessors and build tools, the at-rules reference, and debugging common CSS problems.
Discussion
Sign in to ask a question or share what you learned.