Print Styles

What You'll Learn

  • Create print-friendly stylesheets using the @media print query
  • Master the @page rule for controlling page margins, size, and orientation
  • Hide non-essential elements like navigation and ads when printing
  • Show link URLs after anchor text in printed documents
  • Control page breaks with page-break-before, page-break-after, and page-break-inside
  • Optimize colors, fonts, and images for print to save ink and improve readability
  • Handle headers, footers, and repeating table headings in multi-page prints

Introduction to Print Styles

Print styles optimize web pages for printing on paper, transforming screen-optimized designs into printer-friendly documents. While printing web pages may seem like an afterthought in today's digital world, many users still need to print important content like articles, invoices, tickets, and documentation. Well-designed print styles save ink, improve readability, and ensure critical information transfers to paper effectively.

Print stylesheets address unique challenges: removing unnecessary navigation and ads, converting colors to print-friendly alternatives, showing link destinations, preventing awkward page breaks, and adjusting typography for readability on paper. These considerations make the difference between a professional printed document and a mess of cropped content and wasted paper.

Basic Print Media Query

The @media print query targets styles that only apply when printing. Everything inside this block is ignored for screen display and only takes effect when the page is printed or viewed in print preview.

/* Screen styles (default) */ body { font-family: system-ui, sans-serif; color: #333; background: #f5f5f5; } /* Print-only styles */ @media print { body { font-family: Georgia, serif; font-size: 12pt; color: black; background: white; } }

The @page Rule

The @page rule controls the page box properties including margins, size, and orientation. This is essential for creating properly formatted printed documents with appropriate spacing.

/* Basic page setup */ @page { margin: 2cm; size: A4 portrait; } /* Different margins for first page */ @page :first { margin-top: 3cm; } /* Left and right pages (for book printing) */ @page :left { margin-left: 3cm; margin-right: 2cm; } @page :right { margin-left: 2cm; margin-right: 3cm; } /* Landscape orientation */ @page { size: A4 landscape; } /* Custom page size */ @page { size: 210mm 297mm; } /* Page numbering */ @page { @bottom-right { content: "Page " counter(page); } }

Hiding Non-Essential Content

Navigation, ads, sidebars, and interactive elements should typically be hidden when printing to save ink and focus on the main content. Use display: none to hide these elements completely.

@media print { /* Hide common non-essential elements */ nav, .sidebar, .advertisement, .social-share, .comments, .video-embed, .no-print, button, footer { display: none !important; } /* Hide interactive elements */ input[type="submit"], input[type="button"], .tooltip, .dropdown-menu { display: none; } }

Showing Link URLs

Links are interactive on screen but meaningless on paper. Show the full URL after each link so readers know where it points. Use the attr() function to insert the href value.

@media print { /* Show URLs after links */ a[href]::after { content: " (" attr(href) ")"; font-size: 0.8em; color: #666; } /* Don't show URLs for anchor links */ a[href^="#"]::after, a[href^="javascript:"]::after { content: ""; } /* Handle long URLs that might break layout */ a[href]::after { word-wrap: break-word; word-break: break-all; } /* Optional: Show URLs below link text */ a[href] { position: relative; } a[href]::after { display: block; margin-top: 0.25em; font-style: italic; } }

Controlling Page Breaks

Page breaks can split content awkwardly. Control where breaks occur to keep related content together and avoid orphaned headings.

@media print { /* Keep headings with following content */ h1, h2, h3, h4, h5, h6 { page-break-after: avoid; page-break-inside: avoid; } /* Don't break inside these elements */ pre, blockquote, table, .card, .code-block { page-break-inside: avoid; } /* Force page break before major sections */ .chapter { page-break-before: always; } /* Prevent orphans and widows */ p { orphans: 3; /* Min lines at bottom of page */ widows: 3; /* Min lines at top of page */ } /* Modern syntax (better browser support coming) */ .element { break-inside: avoid; break-before: page; break-after: avoid; } }

Optimizing Colors and Backgrounds

Colored backgrounds waste ink and can make text harder to read when printed. Convert to print-friendly colors and remove unnecessary decorative elements.

@media print { /* Remove all backgrounds and convert to black text */ * { background: transparent !important; color: black !important; box-shadow: none !important; text-shadow: none !important; } /* Keep important visual structure */ .chart, .diagram, .important-callout { border: 1px solid black !important; } /* Convert colored text to black */ h1, h2, h3, p, li, td { color: black !important; } /* Light borders instead of heavy ones */ table, th, td { border: 1px solid #333; } }

Typography for Print

Print typography differs from screen typography. Serif fonts are more readable on paper, and appropriate sizing in points ensures consistent output regardless of printer resolution.

@media print { /* Serif fonts for better print readability */ body { font-family: Georgia, "Times New Roman", serif; font-size: 12pt; line-height: 1.5; } /* Appropriate heading sizes */ h1 { font-size: 24pt; line-height: 1.2; } h2 { font-size: 18pt; line-height: 1.2; } h3 { font-size: 14pt; line-height: 1.3; } h4, h5, h6 { font-size: 12pt; line-height: 1.3; } /* Readable body text */ p { font-size: 12pt; line-height: 1.5; margin-bottom: 12pt; } /* Smaller text for captions and footnotes */ .caption, .footnote { font-size: 10pt; } }

Table Printing

Tables require special attention in print to ensure headers repeat on each page and rows stay together. Proper table styling prevents data from becoming unreadable across page breaks.

@media print { table { border-collapse: collapse; width: 100%; } /* Repeat table header on each page */ thead { display: table-header-group; } /* Repeat table footer on each page */ tfoot { display: table-footer-group; } /* Prevent rows from breaking across pages */ tr { page-break-inside: avoid; } /* Visible borders */ th, td { border: 1px solid black; padding: 0.5em; } th { background: #f0f0f0 !important; font-weight: bold; } }

Images for Print

Images should be optimized for print to prevent breaking across pages and ensure proper sizing. High-resolution images may need different treatment than decorative images.

@media print { /* Prevent images from breaking across pages */ img { page-break-inside: avoid; max-width: 100% !important; height: auto !important; } /* High quality rendering for print */ img { image-rendering: -webkit-optimize-contrast; image-rendering: crisp-edges; } /* Hide decorative images */ .decorative, .icon, .background-image { display: none; } /* Keep content images */ .content img, figure img { display: block; margin: 1em auto; } /* Print captions */ figcaption { font-size: 10pt; font-style: italic; text-align: center; margin-top: 0.5em; } }

Complete Print Stylesheet Template

Here's a comprehensive print stylesheet template you can adapt for most projects. It covers all common print optimization scenarios.

@media print { /* ==================== RESET & BASICS ==================== */ * { background: transparent !important; color: black !important; box-shadow: none !important; text-shadow: none !important; } body { margin: 0; padding: 0; font-family: Georgia, serif; font-size: 12pt; line-height: 1.5; } /* ==================== PAGE SETUP ==================== */ @page { margin: 2cm; size: A4 portrait; } @page :first { margin-top: 3cm; } /* ==================== HIDE NON-ESSENTIAL ==================== */ nav, aside, footer, .no-print, .sidebar, .ads, .social-share, video, iframe, button { display: none !important; } /* ==================== TYPOGRAPHY ==================== */ h1, h2, h3, h4, h5, h6 { page-break-after: avoid; page-break-inside: avoid; } h1 { font-size: 24pt; } h2 { font-size: 18pt; } h3 { font-size: 14pt; } p { orphans: 3; widows: 3; } /* ==================== LINKS ==================== */ a[href]::after { content: " (" attr(href) ")"; font-size: 0.8em; } a[href^="#"]::after, a[href^="javascript:"]::after { content: ""; } /* ==================== IMAGES ==================== */ img { max-width: 100% !important; page-break-inside: avoid; } /* ==================== TABLES ==================== */ table { border-collapse: collapse; } thead { display: table-header-group; } tr { page-break-inside: avoid; } th, td { border: 1px solid black; } /* ==================== CODE BLOCKS ==================== */ pre, code { border: 1px solid #999; page-break-inside: avoid; white-space: pre-wrap; } /* ==================== AVOID BREAKS ==================== */ blockquote, pre, .card { page-break-inside: avoid; } }

Key Takeaways