Print Styles
A print stylesheet is CSS written inside an @media print block that controls how a page looks when printed or saved as a PDF, letting you hide navigation and ads, set page margins, and manage where content breaks across pages.
Learn Python, JavaScript, Java and more with free interactive lessons, real projects and AI-powered help. Beginner-friendly.
Part of the free HTML & CSS course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
By the end of this lesson you'll be able to give any page a clean printed (and "Save as PDF") version — hiding the nav and ads, setting paper margins, controlling where pages break, and printing link URLs so the paper is actually useful on its own.
A print stylesheet is like the "export to PDF" filter on a document. Your web page is full of things that only make sense on a screen — a sticky navbar, ads, a cookie banner, a video player, a "Buy now" button. None of that belongs on a sheet of paper.
@media print is the filter that runs the moment someone hits Print. It strips away the digital chrome, flattens the colours so they don't drink ink, and leaves a clean, readable document behind — the same content, dressed for paper instead of for a browser. You write it once, and every printout (or saved PDF) comes out looking professional.
Everything you wrap in @media print {' … '} applies only when the page is printed or shown in print preview. On screen those rules do nothing, so they can never break your normal layout. The single most important thing print CSS does is hide the chrome : display: none on the nav, ads, and buttons so they don't waste paper.
You can't see print CSS by looking at the page. To check your work, open print preview with Ctrl+P (Windows/Linux) or Cmd+P (Mac) — no paper required.
The @page rule styles the printed sheet itself — its margins and its size . @page {' margin: 2cm; '} gives every page a 2cm border of white space; size: A4 landscape picks the paper and orientation. Use real print units here — cm , mm , in , or pt — never vh / vw , because a printout has no viewport.
You can even target specific pages: @page :first styles only page one (handy for a bigger top margin on a cover page), and @page :left / :right style even and odd pages for double-sided binding.
On screen content scrolls forever; on paper it has to be sliced into pages, and by default the browser slices wherever it runs out of room — sometimes mid-table or mid-heading. Three properties let you take control: break-before: page forces a new page before an element, break-after: page forces one after , and break-inside: avoid tells the browser "don't split this element across two pages" — perfect for tables, images, and cards.
For the fine detail, orphans and widows set the minimum number of lines a paragraph may leave at the bottom or carry to the top of a page, so you never strand a single lonely line.
A link is useless on paper — you can't click it, and "see our report" tells the reader nothing about where to look. The fix is generated content: a::after {' content: " (" attr(href) ")"; '} . The attr(href) function reads the URL out of the link's markup and prints it in brackets after the text, so the printout shows both the words and the address.
Scope it to a[href^="http"] if you only want to expand external links and leave in-page anchors like #top alone.
By default browsers drop background colours and images when printing — it saves ink and keeps text legible on white paper. Most of the time that's exactly what you want, so prefer borders over coloured fills to show structure. But sometimes a background is the point: a coloured invoice header, a code block, a status badge.
To force a background to print, set print-color-adjust: exact (add the -webkit- prefix for Chrome and Safari). It tells the browser "print these colours as I designed them, don't optimise them away." Use it sparingly — only on the few elements that genuinely need their colour.
Here's a complete article that uses every idea from this lesson at once: it hides the nav and ads, sets page margins, prints link URLs, keeps the figure whole, forces the references onto a fresh page, and keeps just the one background it needs. Read the comments, then open print preview to see the screen version melt into a clean document.
This receipt prints with its toolbar and a cramped layout. Add a print block that hides the toolbar and gives the page sensible margins. Fill in the blanks marked ___ , then open print preview and check the comments.
This report's links print as plain text (no URL) and its table can split across pages. Add generated content to show the URLs and a rule to keep the table whole. Fill in the blanks, then verify in print preview.
Support is faded now — only an outline is given. Write the print stylesheet for this invoice yourself. Use the worked examples in sections 1–6 as your reference if you get stuck.
You can now give any page a clean, professional printout. The essentials:
Next up: CSS Performance Optimization , where you'll make your styles render as fast as they look good.
Practice quiz
Which at-rule wraps CSS that applies only when a page is printed?
- @media screen
- @media print
- @page print
Answer: @media print. @media print { … } applies its rules only when the page is printed or shown in print preview.
How do you preview your print styles without using paper?
- Open print preview with Ctrl+P / Cmd+P
- View the page source
- Resize the window
- Disable CSS
Answer: Open print preview with Ctrl+P / Cmd+P. Print CSS is invisible on screen; open print preview (Ctrl+P / Cmd+P) or emulate the print media type in DevTools.
What is the most common job of a print stylesheet?
- Add animations
- Increase font sizes
- Load web fonts
- Hide screen-only chrome like nav, ads, and buttons with display: none
Answer: Hide screen-only chrome like nav, ads, and buttons with display: none. The single most important thing print CSS does is hide screen-only chrome with display: none so it does not waste paper.
Which rule sets the printed page's margins and paper size?
- @paper
- @page
- @sheet
- @margin
Answer: @page. The @page rule styles the printed sheet itself — for example @page { margin: 2cm; size: A4 landscape; }.
Which units are appropriate for print layouts?
- cm, mm, in, pt
- vw and vh
- px only
- em and rem only
Answer: cm, mm, in, pt. Print has no viewport, so use real print units (cm, mm, in, pt); avoid viewport units like vw/vh.
Which property keeps a table or figure from splitting across two printed pages?
- break-before: page
- page-break: keep
- break-inside: avoid
- overflow: hidden
Answer: break-inside: avoid. break-inside: avoid tells the browser not to split that element across a page boundary.
Which property forces a new page before an element (e.g. starting a chapter)?
- break-after: avoid
- break-before: page
- break-inside: avoid
- page-start: new
Answer: break-before: page. break-before: page forces a page break before the element; break-after: page forces one after it.
How do you print a link's URL after its text on paper?
- a { content: url; }
- a { print-url: true; }
- a::before { href: show; }
- a::after { content: " (" attr(href) ")"; }
Answer: a::after { content: " (" attr(href) ")"; }. Generated content with attr(href) — a::after { content: " (" attr(href) ")"; } — pulls the URL into the printout.
Why are background colors usually missing in a printout, and how do you force one to print?
- Browsers cache them; use background-cache
- Browsers strip them to save ink; opt in with print-color-adjust: exact
- They print by default; nothing to do
- They require a <canvas>
Answer: Browsers strip them to save ink; opt in with print-color-adjust: exact. Browsers drop backgrounds by default to save ink; print-color-adjust: exact (plus the -webkit- prefix) forces them to print.
What do the and properties control in print?
- Page numbering
- Image quality
- The minimum lines left at the bottom or carried to the top of a page
- Font substitution
Answer: The minimum lines left at the bottom or carried to the top of a page. orphans and widows set the minimum number of lines a paragraph may leave at the bottom or carry to the top of a page.