Having a dedicated print CSS stylesheet is essential for ensuring that your website's content looks great when printed. This separate stylesheet allows you to customize the layout and design of your website for print, making it easier to read and navigate.
To start, you can use the @media print rule to define the styles for your print CSS stylesheet. This rule applies only to printed pages, so you can use it to make changes that won't affect your website's layout on screen.
Getting Started
To start creating a print CSS stylesheet, begin by relying on the default style sheet of the browser, which handles printed output well by default.
Insert all declarations for printing at the end of your main style sheet and enclose them with a distinct rule. This rule should include all screen styles in the separate @media screen {…} rule and omit the media type for the condensed style sheet.
Using screen styles for printing is not the optimal solution because screen and print are different. Many elements will need to be reset or styled differently so that they look normal on a sheet of paper.
You can separate the declarations for screen and print into two CSS files by setting the media type for the screen output to media="screen" and the media type for printing to media="print", omitting it for the first one.
Basic Styling
You'll want to make sure your text is easy to read when printed, so use dark text on a white background.
Consider using a serif font, like Times New Roman or Garamond, which can be easier to read than sans-serif fonts.
Adjust the text size to 12pt or higher to ensure it's clear and legible.
To make your text even more readable, modify paddings and margins where necessary, using standard units like cm, mm, or in.
Why Do We Need CSS
We need CSS for printing because it allows us to reproduce route directions or timetables in a readable format.
Many people rely on printed web content for things like saving a copy for offline reading or accessing information in areas with poor connectivity.
Printing web receipts for bookkeeping purposes is another important use case for CSS printing control.
A basic style sheet can be completed within hours, making it a practical solution for making your pages printer-friendly.
However, if we don't use CSS for printing, text can be too small, too large, or too faint, making it difficult to read.
Columns can also be too narrow, too wide, or overflow page margins, leading to a cluttered and confusing layout.
This can be especially frustrating if sections are cropped or disappear entirely, making it hard to access important information.
Fortunately, CSS print control has been possible for many years, and there are well-supported and practical options for making your pages printer-friendly.
Here are some common issues with printing web content without CSS:
- 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!
Adding the Styling
To start adding the styling to your print-friendly design, you'll want to consider the basics of printer-friendly styling. Ensure you use dark text on a white background, which is a good starting point.
A serif font, such as Georgia, is often easier to read in print. Adjust the text size to 12pt or higher to make your content more readable.
You'll want to modify paddings and margins where necessary, using standard units like cm, mm, or in. This will help your content flow smoothly on the printed page.
To separate elements in your design, use borders and spacing. A black border on the bottom of an element, for example, can help clearly separate it from the rest of the content.
Consider using borders instead of background colors to save ink and make your design more printer-friendly.
When it comes to font and line size, keep in mind that the size you see on the screen is different from what eyes can read in print. A font size of 14pt is a good starting point, and consider giving titles a larger font size to distinguish them from the content.
Here are some key elements to include in your print stylesheet:
- Body: { background: #fff; color: #000; }
- #wrapper: { clear: both; display: block; float: none; position: relative; }
- #header: { border-bottom: 2pt solid #000; padding-bottom: 18pt; }
These elements will help you get started on creating a print-friendly design that's easy to read and navigate.
Setting Strings
To set strings in paged media, you need to use a property named string-set in the selector that you want to take the content from. This is typically done using the h1 element.
The value of string-set is the name you would like to give this content and then content(). You can then output this as generated content using string().
You can use string-set to write the content to a specific name, like doctitle. This is useful for creating a unique identifier for each chapter title.
Each time an h1 occurs, the content is written to doctitle and then outputted in the top-right margin box of right-hand pages, changing only when another h1 occurs.
Differences Between CSS for the Web
The biggest difference between CSS for the web and for print is that printed documents refer to a page model that is of a fixed size.
On the web, we have no idea of the size of the viewport, but in print, the fixed size of each page has a bearing on everything we do.
We have to consider our document as a collection of pages, paged media, rather than the continuous media that is a web page.
Paged media introduces concepts that make no sense on the web, such as generating page numbers and putting chapter titles in margins.
You might need to create cross-references and footnotes, indexes and tables of content from your document, which can be a lot of work to redo every time you update the copy.
That's where CSS comes in, whose specifications are designed for use in creating paged media.
Links
Links are a crucial part of any online content, and printing them can be a bit tricky.
To make hyperlinks more useful in print, you can use a dark enough color that will show up in grayscale output, such as a dark red, and also boldface and underline the text of the links.
This will ensure that the links stand out, even in a printout.
In a fully CSS2-conformant browser, you can use a rule to insert the URLs of the links after each one, making them fairly useful to anyone who has a copy of the printout and a web browser handy.
This rule restricts the effect to the "content" div, avoiding sticking a URL in the masthead.
The spaces before and after the parentheses are actually part of the rule, and they need to appear in the rule in order to be inserted into the document.
One aesthetic problem with this rule is that it causes the value of a link's href attribute to be inserted into the document verbatim, which can make "rooted" URLs like /issues/144 less useful.
A CSS3 selector, ^=, offers a solution to this problem by selecting elements based on the beginning of their attribute values, and you can use it to select any href that starts with a slash and insert enough text to fill out the value.
Layout and Structure
In a print CSS stylesheet, the layout and structure are crucial for a clean and organized design.
The `@page` rule is used to define the layout of a printed document, allowing you to specify the size, margins, and orientation of the page.
A well-structured stylesheet can make a big difference in the overall look and feel of your printed design.
Full Width
To take up the entire available margin on a printed document, all elements that wrap the content of the page should have a width of 100%.
In some cases, designers might set a width of 900px, which would only allow print to happen in a 900px area. This can limit the layout of the page.
To avoid this, you can use the following CSS code to set the width of these elements to 100%:
#container, #header, #footer {
margin: 0;
width: 100%;
}
This code ensures that the container, header, and footer elements take up the entire available margin on the printed document.
Side by Side
Side by side, we've got a lot to consider when it comes to our sidebar content. It's essential to give it some special treatment to keep it distinct from the main content.
To start, we can give the sidebar a gray top border and a safe buffer of 30 pixels. This will visually separate it from the rest of the page. The last property, display: block, ensures that the border shows up properly.
We can also use a special print property to move the contents of the sidebar to a new page when printed. This is done by setting the property to page-break-before: always. If we do this, we can omit all the other properties.
Breaks
Breaks are a crucial aspect of layout and structure in printing. You can control page breaks using CSS properties like page-break-before, page-break-after, and page-break-inside.
The page-break-before property can be set to always to force a heading to start a new page. For example, setting page-break-before to always on an h1 heading will ensure it's always at the beginning of a page. This is especially useful for chapter titles in books.
The page-break-after property can be used to avoid breaks directly after a heading. By setting page-break-after to avoid, you can prevent a page break from occurring immediately after a heading.
Page-break-inside is another property that can be used to avoid breaking figures and tables. By setting page-break-inside to avoid, you can prevent page breaks from occurring within these elements.
The break-inside property has similar values to page-break-inside, including auto and avoid. The break-before and break-after properties also have several values, including auto, avoid, and page.
Here's a summary of the break-before and break-after values:
- 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
By controlling page breaks, you can create a more polished and professional-looking print layout.
The Html Document
My book is compiled in a single HTML document for simplicity, and I've found it easier to deal with one large document rather than compiling multiple documents in Prince.
You can add a div that contains the cover image and the table of contents for the book, which links to the IDs of the chapters' h1 headings.
The right-hand spread will have the title of the book in the bottom-left margin box, a page counter in the bottom-right, and the chapter's title in the top-right, set using string-set further down in the style sheet.
For the first page, which contains the cover image, you can set generated content to appear by setting it to normal.
You can define counters for chapters and figures, and place the chapter number before the title and the figure number after the title.
Visual Hierarchy
A well-designed visual hierarchy is crucial for a print-friendly website. A List Apart is a great example of this, where the multi-column design is simplified into a single column, full width, mirroring the website's sensible hierarchy.
Article titles and authors are no longer active links, which helps to declutter the design and focus attention on the content. This is a great approach to take, as it allows the reader to focus on the information being presented.
In the case of James Li's personal website, the navigation links are not included in the print version, which is a clever move, as navigation has no value on a printed page unless it's informative in itself.
Elements Can Have Different Emphasis
Elements can have different emphasis in print by using the display:none attribute to hide unnecessary elements. This is especially useful for hiding images, navigation, and sidebars that are not relevant to the printed document.
You can also use the display:none attribute to hide specific elements, like the img#wpstats element shown in Example 2. This will remove the image from the printed document entirely.
Elements can also have different emphasis by adjusting their width and margins. For example, a sidebar that normally has a width limited to a certain space can be given a width of 100% in the print stylesheet, making it take up the full available margin.
Websites Designed Well
A well-designed website for print should simplify its layout, mirroring the website's sensible hierarchy. This is evident in A List Apart's single column design, which mirrors its website's hierarchy.
Article titles and authors are no longer active links, and typography is kept intact with compatible fonts and simple colors. The Morning News has also prepared its content for print without much concern, excluding background images and color.
James Li's personal website is an excellent example of preserving all spacing and key elements. The logo is a part of the printed product, whereas the navigation links are not, as they have no value on a printed page unless they're informative.
TechCrunch's recent redesign tweaked its visual design and small details, including the print layout, which is very clean and minimalistic. The R/GA website pays attention to the spacing of content within, tightening up elements to best use the space.
A strong grid and effective typography add to the effect of a well-designed website for print. Bottlerocket Creative has done well to adapt their typographic style for offline viewing, giving meticulous attention to type.
OmniTI has optimized its content for print by increasing the size of the text and not crowding the images together.
Image and Content Management
When working with images in a print CSS stylesheet, it's essential to consider how they'll be displayed on paper. Users will not want to print decorative and non-essential images and backgrounds.
To manage images effectively, you can hide them by default and only show them if they have a print class. This way, users can easily distinguish between essential and non-essential images.
Ideally, printed images should use dark colors on a light background, but there may be situations where you have darker bitmaps. A CSS filter can be used to invert and adjust colors in the print style sheet.
You can use a CSS filter to invert and adjust colors in the print style sheet, as shown in the example: "A CSS filter can be used to invert and adjust colors in the print style sheet."
Advanced Styling
Advanced Styling is where things get really interesting. You can now apply printer-friendly styling, which is a game-changer for users who want to print your content. Ensure you use dark text on a white background for optimal readability.
A serif font is a great choice for print layouts, as it's often easier to read than sans-serif fonts. Adjust the text size to 12pt or higher to make your content more readable.
If you need to adjust paddings and margins, consider using standard units like cm, mm, or in – they're often more practical than pixels.
Generated Content
Generated content is a crucial tool for adding text to margin boxes. It's the only way to add content to these boxes at all.
We can use generated content to add the title of the book to the bottom-left margin box of right-hand pages. This is a common use case for generated content.
Generated content is particularly important for creating our book, as it allows us to add essential information to the margin boxes.
Media Attribute Hacking
You can temporarily change the media attribute to "screen" to load printer CSS, but don't forget to restore it to "print" once you're done testing.
This trick won't reveal page breaks, so you can use it to test your CSS without messing up your print layout.
Changing the media attribute is a simple way to gain insight into how your CSS is working, without having to create a whole new stylesheet or test environment.
Marginalia Section 7
In the Marginalia Section 7, we're dealing with the styling of the wrapper and content divs. Their left and right margins are set to 5%, creating a 10% blank space on either side of the article.
The existing styles already give us 10% of the page's printable area as blank space, thanks to the 5% margins on each side. This is because the content div is inside the wrapper, and each one has 5% on each side.
To add an extra 5% to the content div's right padding, we can simply add a 15% right padding, which is a standard unit. This will give us the desired effect, but it's worth noting that some browsers may have trouble handling padding.
Another option would be to leave the margin alone and give the left padding a value of 5%. Since the content div doesn't have a visible background, either approach should have the same effect.
Here's a quick rundown of the standard units we can use for paddings and margins:
- cm (centimeters)
- mm (millimeters)
- in (inches)
It's worth keeping in mind that using standard units like these can be more practical than using percentages.
Frequently Asked Questions
What is a print stylesheet?
A print stylesheet is a special set of formatting rules that automatically adjusts a web page's layout for easy printing. It ensures a user-friendly printed version of your website.
Sources
- Quick Tip: Adding a Simple Print Stylesheet to WordPress (tutsplus.com)
- content (mozilla.org)
- page-break-after (mozilla.org)
- page-break-before (mozilla.org)
- break-after (mozilla.org)
- page-break-inside (mozilla.org)
- break-inside (mozilla.org)
- widows (mozilla.org)
- orphans (mozilla.org)
- box-decoration-break (mozilla.org)
- limited browser support (caniuse.com)
- CSS Paged Media (@page) (mozilla.org)
- print styles are often forgotten (evolt.org)
- Designshack (designshack.net)
- description on the Mozilla Developer Network (mozilla.org)
- demo page (github.com)
- Print Preview (github.com)
- media queries (w3.org)
- Paged Media (w3.org)
- Twitter (twitter.com)
- CSS Generated Content for Paged Media Module (w3.org)
- CSS Paged Media Module (w3.org)
- margin boxes (w3.org)
- CSS counters (mozilla.org)
- CSS Generated Content for Paged Media Module (w3.org)
- Prince (princexml.com)
- on GitHub (github.com)
- User Guide for Prince 9.0 (princexml.com)
- CSS Books: Living Standard (whatwg.org)
- CSS Design: Going to Print (alistapart.com)
Featured Images: pexels.com