Skip to content
Tutorials Center
My learning

Topics

CSS Reference ยท Properties for Special Contexts

Printing CSS

A practical guide to printing with CSS: @media print, hiding navigation, showing link URLs, page breaks, @page sizes, colors and how to test print output.

Printing, and saving to PDF, uses the same HTML and CSS as the screen, but with the print medium active. Styles inside @media print apply only then. A good print stylesheet removes what makes no sense on paper (menus, buttons, ads), makes text dark on white, shows where links go and avoids splitting content badly across pages.

You can write print rules in a separate file with <link rel="stylesheet" media="print" href="print.css"> or inside your main stylesheet. The second is easier to keep in sync.

CSS
@media print {
  nav, .ads, button { display: none; }
  body { color: black; background: white; font-size: 12pt; }
}

Try it

A preview frame cannot print, so this example has a button that toggles a print-preview class. The class applies the same rules as the @media print block, so you can see the effect. In your own pages, drop the class and keep only the @media print version.

Screen version versus print version

What a print stylesheet usually does

Hide the interface

Navigation, search boxes, sidebars, video players and buttons are useless on paper. Hide them with display: none inside @media print.

Use readable type and plain colors

Set a dark text color and a white background, and size text in points (pt), the unit of paper: 11pt to 12pt is typical. Browsers usually do not print background colors and images unless the user turns that on, so never let important information depend on them. If something must keep its color (a chart legend, a badge), ask for it with print-color-adjust: exact, which is still only a request.

A printed page cannot be clicked. This rule appends each external URL:

CSS
@media print {
  a[href^="http"]::after { content: " (" attr(href) ")"; }
}

Control page breaks

Use break-inside, break-before and break-after to keep figures, table rows and headings with their content, and orphans and widows to avoid single lonely lines at page edges.

CSS
@media print {
  figure, tr, pre { break-inside: avoid; }
  h2, h3 { break-after: avoid; }
}

Set up the page itself

The @page rule defines the paper size and margins, size picks the format and orientation, and page lets a section use a different page type.

CSS
@page { size: A4; margin: 2cm; }

Usage notes

  • Test in print preview. Use the browser's print preview or "Save as PDF" and check each browser you support. Details such as margins, headers and footers differ.
  • Use physical units (cm, mm, in, pt) for print layouts, and avoid fixed pixel widths that overflow the paper. A fluid layout usually prints well; wide tables are the main risk.
  • Layout. Flexbox and grid print fine, but very complex layouts can break in odd places. Simplify to a single column for print.
  • Don't hide content that matters. If something is useful on paper (an address, a coupon code), keep it visible.
  • Avoid position: fixed and sticky headers; they can repeat or overlap on every page, depending on the browser.

Browser support

@media print, break-* and @page margins are widely supported; page margin boxes, named pages and some size behaviors vary. Check compatibility tables and test your actual print output.