Skip to main content

CSS Output & Performance

Print Styles

Written by Updated Print Styles exercises

Prepare documents for paper and PDF with print media rules, page breaks, and sensible URL treatment. It covers print media queries, page margins, fragmentation, hiding interface chrome, conserving ink, and preserving destination URLs.

When someone prints your page or saves it as PDF, they get the menu, the ads, the cookie banner and grey text on a dark background, unless you add print styles. A short @media print block hides what is useless on paper and makes the rest readable.

Print styles are just a media query, explained in CSS Media Queries. Text can even be laid out in columns for print with Multi-column Layout.

Hide the interface, save ink

Example

css

@media print {
  nav,
  .site-footer,
  .no-print {
    display: none !important;
  }

  body {
    color: black;
    background: white;
    font: 11pt/1.45 Georgia, serif;
  }

  a[href^="http"]::after {
    content: " (" attr(href) ")";
    overflow-wrap: anywhere;
  }
}

Navigation, footers and anything marked .no-print disappear, and the page becomes black text on white. Printing a dark theme otherwise uses a lot of ink, or comes out unreadable.

Avoid awkward page breaks

Example

css

@media print {
  h2, h3 { break-after: avoid; }
  figure, table, pre { break-inside: avoid; }
  article { break-before: page; }
}

@page {
  margin: 18mm;
}

Break rules are requests, not guarantees; the page formatter may still split an oversized box. Preview real content in the browsers your readers use for printing.

Show where links go

On paper, a link cannot be clicked, so its address is lost. This adds it after the link text:

Print link addresses

css

@media print {
  a[href^="http"]::after {
    content: " (" attr(href) ")";
    font-size: 0.9em;
  }
}

Print CSS Reference

SyntaxPurposeExample
@media printApplies styles only to printed output.@media print { ... }
@pageConfigures the page box.@page { margin: 18mm; }
break-insideControls fragmentation inside an element.break-inside: avoid
print-color-adjustExpresses whether printed colors should be preserved.print-color-adjust: exact

Complete Example

Complete runnable example

html

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Print Styles example</title>
  <style>
    * { box-sizing: border-box; }
    body {
      margin: 0;
      padding: 2rem;
      font-family: system-ui, sans-serif;
      line-height: 1.5;
    }
    main { max-width: 70rem; margin-inline: auto; }
    article, section, aside, form, .card, .panel {
      padding: 1rem;
      border: 1px solid #cbd5e1;
      border-radius: 0.6rem;
    }

@media print {
  nav,
  .site-footer,
  .no-print {
    display: none !important;
  }

  body {
    color: black;
    background: white;
    font: 11pt/1.45 Georgia, serif;
  }

  a[href^="http"]::after {
    content: " (" attr(href) ")";
    overflow-wrap: anywhere;
  }
}

@media print {
  h2, h3 { break-after: avoid; }
  figure, table, pre { break-inside: avoid; }
  article { break-before: page; }
}

@page {
  margin: 18mm;
}
  </style>
</head>
<body>
  <main><nav class="no-print">This navigation is hidden when printed.</nav><article><h1>Printable article</h1><p>Open print preview to test the paged layout.</p><p><a href="https://example.com/reference">Reference link</a></p></article></main>
</body>
</html>

Browser Support

Feature
Chrome
Edge
Firefox
Safari
@media print11213
@page2121918.2

Versions show the first stable desktop release with unprefixed support. Data source: MDN Browser Compatibility Data 8.0.8. A partial-support note is included where it changes how the example behaves.

Test it

Open the browser's print preview (Ctrl + P or Cmd + P) with a long, realistic page. Check that nothing important is hidden, that headings are not left alone at the bottom of a page, and that tables and images are not cut in half. Chrome DevTools can also show the print styles on screen: open the Rendering panel and set "Emulate CSS media type" to print.

Print Styles exercises

Keep together

Do not let a page break split the card.

The HTML your code works on
<div class="card">Card</div>
<h2 class="chapter">Chapter</h2>
<p class="text">Text</p>
<nav class="menu">Menu</nav>

Result

Check your answer to see it here.