Sub topic 14
Print styles
This chapter covers how to style a page for printing with the print media type. It assumes you can write basic @media rules, know the CSS cascade, and are comfortable with units such as px, rem, and em.
Media types: screen, print, and all
A media query can begin with a media type. The type says what kind of output device the styles are for. Three types are in current use:
| Type | Applies to |
|---|---|
| `all` | Every output. This is the default when you omit the type. |
| `screen` | Displays such as monitors, phones, and tablets. |
| `print` | Printed pages, print preview, and "Save as PDF". |
Other types, such as tv and handheld, are deprecated. Browsers match them to nothing, so don't use them.
When the user opens the print dialog, the browser re-evaluates the page against the print media type. Rules inside @media print now apply, and rules inside @media screen stop applying. Rules outside any media query still apply to both outputs, so your screen styles carry over to print unless you override them.
The diagram below shows how the browser picks styles.
A basic print stylesheet
A print stylesheet usually does three things. It removes elements that are useless on paper, forces readable colors, and resets layout assumptions made for screens.
@media print {
nav,
.ads {
display: none;
}
body {
color: #000;
background: #fff;
}
}Navigation menus, advertising, and video players can't be used on paper. Hiding them saves ink and space. Setting black text on a white background avoids pale gray text that prints poorly and dark backgrounds that waste toner.
Where to put print rules
You have two options. The first is a block inside your main stylesheet, as above. The second is a separate file linked with the media attribute:
<link rel="stylesheet" href="main.css">
<link rel="stylesheet" href="print.css" media="print">A separate file keeps print concerns apart. The browser still downloads it, but it doesn't block rendering of the screen page. Either approach produces the same cascade behavior, so choose by team preference. Because the print rules come later in the source, they win over equal-specificity screen rules.
Showing link addresses
On paper, a link is not clickable, so the reader loses the destination. You can print the URL after the link text with generated content:
@media print {
a[href^="http"]::after {
content: " (" attr(href) ")";
font-size: 0.9em;
word-break: break-all;
}
}The selector a[href^="http"] matches only links whose href starts with http. This skips in-page anchors such as #top and mailto: links, which would print as noise. The attr(href) function reads the attribute value into the content string.
Controlling page breaks
Browsers split content across pages automatically, and the result can be ugly. A heading may sit alone at the bottom of a page, or a table row may be cut in half. The break properties give you control:
@media print {
h2,
h3 {
break-after: avoid;
}
figure,
pre,
tr {
break-inside: avoid;
}
p {
orphans: 3;
widows: 3;
}
.chapter {
break-before: page;
}
}break-after: avoidasks the browser not to break right after the element, so a heading stays with the paragraph that follows.break-inside: avoidasks the browser to keep the element on one page if it fits.break-before: pageforces the element to start on a new page.orphansis the minimum number of lines left at the bottom of a page.widowsis the minimum number carried to the top of the next page.
These are requests, not commands. If a pre block is taller than a page, the browser must split it anyway. Older code uses page-break-before and page-break-inside. These legacy names still work as aliases, but prefer the break-* forms in new code.
Page size and margins with @page
The @page at-rule sets properties of the printed page itself, not of an element:
@page {
size: A4;
margin: 2cm;
}
@page :first {
margin-top: 4cm;
}The :first pseudo-class targets only the first page, which is useful for a title page or letterhead. Browser support for @page is good for size and margin. Support for margin boxes, which hold page numbers and running headers, is limited. Don't rely on them unless you only target one browser.
The size property can also take landscape, portrait, or explicit dimensions such as size: 21cm 29.7cm. Users can override the paper size in the print dialog, so design for flexible content width.
Units and layout on paper
Some screen-oriented units behave badly in print:
vwandvhdepend on the viewport. In print they refer to the page area, so sizes can be unpredictable. Avoid them.pxworks, but print designers prefer physical units:pt,cm,mm, andin. Body text of11ptor12ptis a common choice.remandemstill work and are fine if you set the root size.
For layout, remove fixed widths and floats designed for a sidebar. A simple reset is often enough:
@media print {
main {
width: auto;
max-width: none;
margin: 0;
}
body {
font: 12pt/1.4 Georgia, serif;
}
}Backgrounds and colors
By default, browsers don't print background colors and images. This protects ink, but it can break designs that rely on a colored badge or a table header shading. If a background carries meaning, you can request it:
@media print {
.badge {
background: #d9d9d9;
-webkit-print-color-adjust: exact;
print-color-adjust: exact;
}
}The prefixed name is still needed for some Chromium and Safari versions. The user can also disable background printing in the dialog, so never put essential information in a background alone. A badge should still have text and a border.
Testing print styles
You don't need to waste paper to test. Use these methods:
- Open browser developer tools, find the rendering options, and set "Emulate CSS media type" to
print. The page redraws with print rules in the normal window, so you can inspect elements. - Open the print preview (
Ctrl+PorCmd+P). This shows pagination,@pagemargins, and break behavior, which emulation alone does not. - Save to PDF and check the output on at least two browsers, since page-break handling differs between them.
Emulation shows rule effects but not page breaks. Print preview shows page breaks. Use both.
Common errors
- Print rules have no effect. The usual cause is specificity. A screen rule such as
.site-header navbeats a plainnavin your print block. Match the specificity or restructure the screen rules. - Hiding with
visibility: hidden. The element still takes up space and leaves a gap. Usedisplay: none. - Blank pages. An element with
break-before: pageright after another forced break, or a very tall element withbreak-inside: avoid, can produce empty pages. - Cut-off content. Fixed widths in
pxwider than the page are clipped. Usewidth: autoor percentages. - Fixed or sticky headers repeating. Elements with
position: fixedprint on every page or overlap content. Reset them toposition: staticfor print.
Exercise
A stylesheet has .site-header nav { display: flex; } for screens. Your print block contains nav { display: none; }, but the navigation still prints. Why, and what is a fix?
Show answer
The screen selector .site-header nav has higher specificity than the plain nav selector, so it wins even though the print rule comes later. Fix it by raising the print selector's specificity, for example .site-header nav { display: none; } inside @media print, or by restructuring the screen rule so it is limited to @media screen.
Exercise
Write a rule that keeps each figure on a single page when possible and keeps every h2 on the same page as the content after it.
Show answer
@media print { figure { break-inside: avoid; } h2 { break-after: avoid; } }
Exercise
Why does DevTools print emulation not replace checking print preview?
Show answer
Emulation applies the print media type to the on-screen page, so it shows which rules apply. It does not paginate the content, so it can't show where page breaks, @page margins, or orphans and widows take effect. Print preview shows those.