search
HomeWeb Front-endCSS TutorialHow to Create Printer-friendly Pages with CSS

How to Create Printer-friendly Pages with CSS

In this article, we review the art of creating printer-friendly web pages with CSS.

Key Takeaways

  1. The Importance of Printer-Friendly Pages: Despite the digital age, there’s a significant need for printer-friendly web pages. Printing web pages is essential for various purposes, such as printing travel tickets, offline reading, and providing accessible information for those who find it difficult to use screens. The article emphasizes the necessity of optimizing web pages for printing to improve accessibility and user experience.

  2. CSS for Print: CSS plays a crucial role in making web pages printer-friendly. The article outlines how to use CSS to create print stylesheets that ensure web content is presented in an optimal format when printed. This includes using specific media queries for print, adjusting layout and styles, and ensuring that the printed page is legible and well-organized.

  3. Practical Tips and Techniques: The article provides a wealth of practical advice on creating effective print stylesheets. Key recommendations include removing unnecessary elements, linearizing layouts, using appropriate fonts and sizes, managing page breaks, and including supplementary content for printed versions. These tips help web developers ensure that their sites can be easily and efficiently printed, saving ink and paper while maintaining the integrity of the web content.

Why Do We Need CSS for Printing?

“Who prints web pages?” I hear you cry! Relatively few pages will ever be reproduced on paper. But consider:

  • printing travel or concert tickets
  • reproducing route directions or timetables
  • saving a copy for offline reading
  • accessing information in an area with poor connectivity
  • using data in dangerous or dirty conditions — for example, a kitchen or factory
  • outputting draft content for written annotations
  • printing web receipts for bookkeeping purposes
  • providing documents to those with disabilities who find it difficult to use a screen
  • printing a page for your colleague who refuses to use this newfangled t’internet nonsense.

Unfortunately, printing pages can be a frustrating experience:

  • text can be too small, too large, or too faint
  • columns can be too narrow, too wide, or overflow page margins
  • sections may be cropped or disappear entirely
  • ink is wasted on unnecessary colored backgrounds and images
  • link URLs can’t be seen
  • icons, menus, and advertisements are printed which could never be clicked!

Many developers advocate web accessibility, yet few remember to make the printed web accessible!

Converting responsive, continuous media to paged paper of any size and orientation can be challenging. However, CSS print control has been possible for many years, and a basic style sheet can be completed within hours. The following sections describe well-supported and practical options for making your pages printer-friendly.

Print CSS can either be:

  1. Applied in addition to screen styling. Taking your screen styles as a base, the printer styles override those defaults as necessary.
  2. Applied as separate styles. The screen and print styles are entirely separate; both start from the browser’s default styles.

The choice will depend on your site/app. Personally, I use screen styles as a print base most of the time. However, I have used separate style sheets for applications with radically different outputs — such as a conference session booking system which displayed a timetable grid on-screen but a chronological schedule on paper.

A print style sheet can be added to the HTML

after the standard style sheet:
<span><span><span><link> rel<span>="stylesheet"</span> href<span>="main.css"</span> /></span>
</span><span><span><span><link> rel<span>="stylesheet"</span> media<span>="print"</span> href<span>="print.css"</span> /></span>
</span></span></span>

The print.css styles will be applied in addition to screen styles when the page is printed.

To separate screen and print media, main.css should target the screen only:

<span><span><span><link> rel<span>="stylesheet"</span> media<span>="screen"</span> href<span>="main.css"</span> /></span>
</span><span><span><span><link> rel<span>="stylesheet"</span> media<span>="print"</span> href<span>="print.css"</span> /></span>
</span></span></span>

Alternatively, print styles can be included within an existing CSS file using @media rules. For example:

<span>/* main.css */
</span><span>body {
</span>  <span>margin: 2em;
</span>  <span>color: #fff;
</span>  <span>background-color: #000;
</span><span>}
</span>
<span>/* override styles when printing */
</span><span><span>@media print</span> {
</span>
  <span>body {
</span>    <span>margin: 0;
</span>    <span>color: #000;
</span>    <span>background-color: #fff;
</span>  <span>}
</span>
<span>}
</span>

Any number of @media print rules can be added, so this may be practical for keeping associated styles together. Screen and print rules can also be separated if necessary:

<span>/* main.css */
</span>
<span>/* on-screen styles */
</span><span><span>@media screen</span> {
</span>
  <span>body {
</span>    <span>margin: 2em;
</span>    <span>color: #fff;
</span>    <span>background-color: #000;
</span>  <span>}
</span>
<span>}
</span>
<span>/* print styles */
</span><span><span>@media print</span> {
</span>
  <span>body {
</span>    <span>margin: 0;
</span>    <span>color: #000;
</span>    <span>background-color: #fff;
</span>  <span>}
</span>
<span>}
</span>

Testing Printer Output

It’s not necessary to kill trees and use outrageously expensive ink every time you want to test your print layout! The following options replicate print styles on-screen.

The most reliable option is the print preview option in your browser. This shows how page breaks will be handled using your default paper size.

Alternatively, you may be able to save or preview the page by exporting to a PDF.

Developer Tools

The DevTools (F12 or Cmd/Ctrl Shift I) can emulate print styles, although page breaks won’t be shown.

In Chrome, open the Developer Tools and select More Tools, then Rendering from the three-dot icon menu at the top right. Change the Emulate CSS Media to print at the bottom of that panel.

In Firefox, open the Developer Tools and click the Toggle print media simulation icon on the Inspector tab’s style pane:

How to Create Printer-friendly Pages with CSS

Hack Your Media Attribute

Presuming you’re using a tag to load printer CSS, you could temporarily change the media attribute to screen:

<span><span><span><link> rel<span>="stylesheet"</span> href<span>="main.css"</span> /></span>
</span><span><span><span><link> rel<span>="stylesheet"</span> media<span>="print"</span> href<span>="print.css"</span> /></span>
</span></span></span>

Again, this won’t reveal page breaks. Don’t forget to restore the attribute to media="print" once you finish testing.

Remove Unnecessary Sections

Before doing anything else, remove and collapse redundant content with display: none;. Typical unnecessary sections on paper could include navigation menus, hero images, headers, footers, forms, sidebars, social media widgets, and advertising blocks (usually anything in an iframe):

<span><span><span><link> rel<span>="stylesheet"</span> media<span>="screen"</span> href<span>="main.css"</span> /></span>
</span><span><span><span><link> rel<span>="stylesheet"</span> media<span>="print"</span> href<span>="print.css"</span> /></span>
</span></span></span>

It may be necessary to use display: none !important; if CSS or JavaScript functionality is showing elements according to particular UI states. Using !important isn’t normally recommended, but we can justify its use in a basic set of printer styles which override screen defaults.

Linearize the Layout

It pains me to say this, but Flexbox and Grid rarely play nicely with printer layouts in any browser. If you encounter issues, consider using display: block; or similar on layout boxes and adjust dimensions as necessary. For example, set width: 100%; to span the full page width.

Printer Styling

Printer-friendly styling can now be applied. Recommendations:

  • ensure you use dark text on a white background
  • consider using a serif font, which may be easier to read
  • adjust the text size to 12pt or higher
  • modify paddings and margins where necessary. Standard cm, mm, or in units may be more practical.

Further suggestions include …

Adopt CSS Columns

Standard A4 and US Letter paper can result in longer and less readable lines of text. Consider using CSS columns in print layouts. For example:

<span>/* main.css */
</span><span>body {
</span>  <span>margin: 2em;
</span>  <span>color: #fff;
</span>  <span>background-color: #000;
</span><span>}
</span>
<span>/* override styles when printing */
</span><span><span>@media print</span> {
</span>
  <span>body {
</span>    <span>margin: 0;
</span>    <span>color: #000;
</span>    <span>background-color: #fff;
</span>  <span>}
</span>
<span>}
</span>

In this example, columns will be created when there’s at least 37em of horizontal space. There’s no need to set media queries; additional columns will be added on wider paper.

Use Borders Instead of Background Colors

Your template may have sections or call-out boxes denoted by darker or inverse color schemes:

How to Create Printer-friendly Pages with CSS

Save ink by representing those elements with a border:

How to Create Printer-friendly Pages with CSS

Remove or Invert Images

Users will not want to print decorative and non-essential images and backgrounds. You could consider a default where all images are hidden unless they have a print class:

<span>/* main.css */
</span>
<span>/* on-screen styles */
</span><span><span>@media screen</span> {
</span>
  <span>body {
</span>    <span>margin: 2em;
</span>    <span>color: #fff;
</span>    <span>background-color: #000;
</span>  <span>}
</span>
<span>}
</span>
<span>/* print styles */
</span><span><span>@media print</span> {
</span>
  <span>body {
</span>    <span>margin: 0;
</span>    <span>color: #000;
</span>    <span>background-color: #fff;
</span>  <span>}
</span>
<span>}
</span>

Ideally, printed images should use dark colors on a light background. It may be possible to change HTML-embedded SVG colors in CSS, but there will be situations where you have darker bitmaps:

How to Create Printer-friendly Pages with CSS

A CSS filter can be used to invert and adjust colors in the print style sheet. For example:

<span><span><span><link> rel<span>="stylesheet"</span> href<span>="main.css"</span> /></span>
</span><span><span><span><link> rel<span>="stylesheet"</span> media<span>="screen"</span> href<span>="print.css"</span> /></span>
</span></span></span>

The result:

How to Create Printer-friendly Pages with CSS

Add Supplementary Content

Printed media often requires additional information which would not be necessary on-screen. The CSS content property can be employed to append text to ::before and ::after pseudo-elements. For example, display a link’s URL in brackets after the text:

<span><span><span><link> rel<span>="stylesheet"</span> href<span>="main.css"</span> /></span>
</span><span><span><span><link> rel<span>="stylesheet"</span> media<span>="print"</span> href<span>="print.css"</span> /></span>
</span></span></span>

Or you can add print-only messages:

<span><span><span><link> rel<span>="stylesheet"</span> media<span>="screen"</span> href<span>="main.css"</span> /></span>
</span><span><span><span><link> rel<span>="stylesheet"</span> media<span>="print"</span> href<span>="print.css"</span> /></span>
</span></span></span>

For more complex situations, consider using a class such as print on elements which should only be visible when printed,. For example:

<span>/* main.css */
</span><span>body {
</span>  <span>margin: 2em;
</span>  <span>color: #fff;
</span>  <span>background-color: #000;
</span><span>}
</span>
<span>/* override styles when printing */
</span><span><span>@media print</span> {
</span>
  <span>body {
</span>    <span>margin: 0;
</span>    <span>color: #000;
</span>    <span>background-color: #fff;
</span>  <span>}
</span>
<span>}
</span>

The CSS:

<span>/* main.css */
</span>
<span>/* on-screen styles */
</span><span><span>@media screen</span> {
</span>
  <span>body {
</span>    <span>margin: 2em;
</span>    <span>color: #fff;
</span>    <span>background-color: #000;
</span>  <span>}
</span>
<span>}
</span>
<span>/* print styles */
</span><span><span>@media print</span> {
</span>
  <span>body {
</span>    <span>margin: 0;
</span>    <span>color: #000;
</span>    <span>background-color: #fff;
</span>  <span>}
</span>
<span>}
</span>

Note: most browsers display the URL and current date/time on the printed page’s header and/or footer, so there’s rarely a need to generate this information in code.

Page Breaks

The CSS3 properties break-before and break-after allow you control how page, column, or region breaks behave before and after an element. Support is excellent, but older browsers may use the similar page-break-before and page-break-after properties.

The following break-before and break-after values can be used:

  • auto: the default — a break is permitted but not forced
  • avoid: avoid a break on the page, column or region
  • avoid-page: avoid a page break
  • page: force a page break
  • always: an alias of page
  • left: force page breaks so the next is a left page
  • right: force page breaks so the next is a right page

Example: force a page break immediately prior to any

heading:
<span><span><span><link> rel<span>="stylesheet"</span> href<span>="main.css"</span> /></span>
</span><span><span><span><link> rel<span>="stylesheet"</span> media<span>="screen"</span> href<span>="print.css"</span> /></span>
</span></span></span>

Note: be wary of over-using page breaks. Ideally, printer output should use as few pages as possible.

The break-inside (and older page-break-inside) property specifies whether a page break is permitted inside an element. The commonly supported values:

  • auto: the default — a break is permitted but not forced
  • avoid: avoid an inner break where possible
  • avoid-page: avoid an inner page break where possible

This can be preferable to specifying page breaks, since you can use as little paper as possible while avoiding page breaks within grouped data such as tables or images:

<span>/* print.css */
</span><span>header<span>, footer, aside, nav, form, iframe, .menu, .hero, .adslot</span> {
</span>  <span>display: none;
</span><span>}
</span>

The widows property specifies the minimum number of lines in a block that must be shown at the top of a page. Imagine a block with five lines of text. The browser wants to make a page break after line four so the last line appears at the top of the next page. Setting widows: 3; breaks on or before line two so at least three lines carry over to the next page.

The orphans property is similar to widows except it controls the minimum number of lines to show at the bottom of a page.

The box-decoration-break property controls element borders across pages. When an element with a border has an inner page break:

  • slice: the default, splits the layout. The top border is shown on the first page and the bottom border is shown on the second page.
  • clone: replicates the margin, padding, and border. All four borders are shown on both pages.

Finally, CSS Paged Media (@page) has limited browser support but provides a way to target specific pages so alternative margins or breaks can be defined:

<span><span><span><link> rel<span>="stylesheet"</span> href<span>="main.css"</span> /></span>
</span><span><span><span><link> rel<span>="stylesheet"</span> media<span>="print"</span> href<span>="print.css"</span> /></span>
</span></span></span>

The CSS page break properties can be placed within your screen or print styles because they only affect printing, but I recommend using them in print CSS for clarity.

Be aware that page break control is little more than a suggestion to the browser. There’s no guarantee a break will be forced or avoided because layout is restricted to the confines of the paper.

Printing Pains

Control over printing web media will always be limited, and results can vary across browsers. That said:

  • printer-friendly style sheets can be retro-fitted to any site
  • most printer styling will work in the majority of browsers
  • print styles will not affect or break existing functionality
  • the development costs are minimal.

Adding a few page breaks and removing unnecessary sections will delight users and raise your site above competitors. Add it to your to-do list!

For more advanced CSS knowledge, read our book CSS Master, 3rd Edition.

FAQs About Creating Printer-friendly Pages with CSS

Creating a CSS print stylesheet is important for controlling the appearance of web pages when they’re printed. Let’s end by covering some of the frequently asked questions about how to create printer-friendly pages with CSS.

What is CSS for print?

It’s possible to print web pages directly from your browser, but a printed web page often won’t look like the page you see on the screen. Web pages are styled with CSS, and CSS can also be used to provide styling for the printed page. However, web page styles don’t usually translate well to print, so it’s important to write CSS styles specifically for the printed page. CSS for print is a set of styles specifically designed to help printers format the layout of the printed page.

How can I use CSS for print?

Web page CSS will automatically apply to the printed version of a web page, but often with unexpected or unwanted results. CSS for print recognizes the unique constraints of the printed page, in contrast to the more flexible browser environment. Setting styles for print involves thinking about how elements will best lay out on a printed page. That may include hiding elements that are not relevant to print, such as menus and the like, styling links in a way that makes the URL is visible on the printed page, and removing background images and font colors that may not be relevant to a printed version of the web page.

How do I set up a CSS print stylesheet?

There are two basic ways to serve up print styles in CSS: via a separate stylesheet, or via a media query. CSS print styles can be stored in a separate stylesheet that’s linked to a web page in the

section of the document:



CSS print styles can be placed within a stylesheet, along with styles for other media, via media queries:

@media print {
/* print styles here */
}

What are some common use cases for print stylesheets?

Common use cases for print stylesheets include:
– Adjusting font sizes and styles for readability on paper.
– Removing or hiding certain elements that are not relevant when printed (such as navigation menus).
– Ensuring that images and background colors do not print by default.
– Specifying page breaks to control how content is divided across pages.

How can I hide specific elements in the print version?

You can hide specific elements in the print version using CSS by setting the display property to none.
For example:

@media print {
.hide-on-print {
display: none;
}
}

How do I specify page breaks in a print stylesheet?

You can specify page breaks using the page-break-before and page-break-after CSS properties. For example:

@media print {
.page-break {
page-break-before: always;
}
}

Can I change the page margins for printed documents?

Yes, you can change the page margins for printed documents using the @page rule in your print stylesheet. For example:

@page {
margin: 1cm;
}

How can I ensure that images and background colors don’t print?

You can prevent images and background colors from printing by using CSS properties like background-image and background-color with the none value in your print stylesheet. For example:

@media print {
img {
display: none;
}

body {
background-color: white;
}
}

Is it possible to change the font styles and sizes for printing?

Yes, you can change font styles and sizes for printing by specifying different styles within your print stylesheet. For example:

@media print {
body {
font-size: 12pt;
font-family: Arial, sans-serif;
}
}

How do I test my print stylesheet before printing?

You can test your print stylesheet by using your web browser’s print preview feature. Most modern browsers allow you to see how the page will look when printed without actually printing it.

The above is the detailed content of How to Create Printer-friendly Pages with CSS. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
What is CSS Grid?What is CSS Grid?Apr 30, 2025 pm 03:21 PM

CSS Grid is a powerful tool for creating complex, responsive web layouts. It simplifies design, improves accessibility, and offers more control than older methods.

What is CSS flexbox?What is CSS flexbox?Apr 30, 2025 pm 03:20 PM

Article discusses CSS Flexbox, a layout method for efficient alignment and distribution of space in responsive designs. It explains Flexbox usage, compares it with CSS Grid, and details browser support.

How can we make our website responsive using CSS?How can we make our website responsive using CSS?Apr 30, 2025 pm 03:19 PM

The article discusses techniques for creating responsive websites using CSS, including viewport meta tags, flexible grids, fluid media, media queries, and relative units. It also covers using CSS Grid and Flexbox together and recommends CSS framework

What does the CSS box-sizing property do?What does the CSS box-sizing property do?Apr 30, 2025 pm 03:18 PM

The article discusses the CSS box-sizing property, which controls how element dimensions are calculated. It explains values like content-box, border-box, and padding-box, and their impact on layout design and form alignment.

How can we animate using CSS?How can we animate using CSS?Apr 30, 2025 pm 03:17 PM

Article discusses creating animations using CSS, key properties, and combining with JavaScript. Main issue is browser compatibility.

Can we add 3D transformations to our project using CSS?Can we add 3D transformations to our project using CSS?Apr 30, 2025 pm 03:16 PM

Article discusses using CSS for 3D transformations, key properties, browser compatibility, and performance considerations for web projects.(Character count: 159)

How can we add gradients in CSS?How can we add gradients in CSS?Apr 30, 2025 pm 03:15 PM

The article discusses using CSS gradients (linear, radial, repeating) to enhance website visuals, adding depth, focus, and modern aesthetics.

What are pseudo-elements in CSS?What are pseudo-elements in CSS?Apr 30, 2025 pm 03:14 PM

Article discusses pseudo-elements in CSS, their use in enhancing HTML styling, and differences from pseudo-classes. Provides practical examples.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool