From EduTechWiki - Reading time: 5 min
CSS 2.1 includes a few properties and an @-rule that allows for better printing. In this short tutorial we shall introduce most of that. CSS3, under the name of CSS Paged Media Module Level 3 includes much more sophisticated features for creating print documents. So far, these are not introduced in this text.
As explained in the CSS media and alternative style sheets tutorial, there are three ways for defining alternative styles:
(1) Use @media rules in a stylesheet
@media print body {
font-family: "Palatino Linotype", "Book Antiqua", Palatino, serif;
font-size: 1em;
color: #333333;
margin-top: 2cm;
margin-right: 2cm;
margin-bottom: 1.5cm;
margin-left: 2cm
}
(2) Define an alternative stylesheet in the HTML
<link rel="stylesheet" href="print-style.css" type="text/css" media="print" />
(3) Define an alternative stylesheet in the CSS
@import url(print-style.css) print;
<style type="text/css">
@import url(print-style.css) print;
</style>
Most often, you will have to do the following:
(1) Remove unwanted items, e.g.
#navigation, .do-not-print, #menu {display:none}
(2) Contents should not float and width should be set to 100% or auto
(3) Contrasts should be optimized for a printer.
Simple minimalistic example
Taken from CSS Design: Going to Print by Eric Meyer, May 2002:
body { background: white; }
#menu { display: none; }
#wrapper, #content {
width: auto;
border: 0;
margin: 0 5%;
padding: 0;
float: none !important;
}
In addition, you could print out URLs like this:
a[href^="http://"]:after, a[href^="ftp://"]:after {
content: " (" attr(href) ")";
color: blue;
font-size: small;
}
Instead of defining margins, you can use the @page directive. “Authors can specify the margins of a page box inside an @page rule. An @page rule consists of the keyword "@page", followed by an optional page selector, followed by a block containing declarations and at-rules. Comments and white space are allowed, but optional, between the @page token and the page selector and between the page selector and the block. The declarations in an @page rule are said to be in the page context.” (Paged media, CSS 2.1 Specification]
The @page rule takes an optional pseudo-selector for first, left, and right pages. You then can define margins. However, you can't use px and pt's since these are useless units for a printer. Examples:
/* Default left, right, top, bottom margin is 2cm */
@page { margin: 2cm }
/* First page, 10 cm margin on top */
@page :first {
margin-top: 10cm;
}
/* Left pages, a wider margin on the left */
@page :left {
margin-left: 3cm;
margin-right: 2cm;
}
@page :right {
margin-left: 2cm;
margin-right: 3cm;
}
You should specify when page breaks must occur and when they should not occur. Modern browsers implement three CSS constructs: page-break-before, page-break-after, page-break-inside, orphans and widows.
Note that these are recommendations, i.e. a browser generating the print document should but does not need to use these heuristics:
Below are some typical use cases.
section {page-break-before: always;}
h1 {page-break-before: always;}
section {page-break-after: always;}
h1 {page-break-after: avoid;}
p {page-break-inside: avoid;}
p {orphans:3; widows:2;}
While multiple columns can make sense on web page styles that aim at larger screens - in the CSS media and alternative style sheets tutorial we explain how to target wide screen areas- it makes a lot of sense when printing on A4 or US letter paper. CSS3, in the CSS Multi-column Layout Module offers the possibility to define multiple columns in a quite flexible way.
Details need to be written, search for a tutorial on the web or see the specification ...
Example code:
body {
column-count: 2;
column-gap: 2em;
column-rule: thin solid black;
}
/* This would create an h1 title that spans across the page */
h1 {
column-span: all
break-before: column;
break-inside: avoid-column;
break-after: avoid-column;
}
Support for this module, is so far quite bad. However, experimental proprietary extensions do work as of spring 2013, e.g.
-moz-column-count:3; /* Firefox */
-webkit-column-count:3; /* Safari and Chrome */
Print this or better just "preview print" in order to see the stylesheet effect.
Highlights from the HTML code:
<link rel="stylesheet" href="just-so-stories.css" type="text/css">
<link rel="stylesheet" href="just-so-stories-print.css" type="text/css" media="print">
.........
<p class="noprint"><a href="#copyright">Copyright information</a></p>
.........
<h2 class="noprint">Copyright Information</h2>
"Highlights" from the print CSS:
/* ------- Pagination */
h1, h2 {
page-break-after: avoid;
page-break-before: always;
}
p {
orphans:3;
widows:3;
}
.noprint, pre.copyright {
display:none;
}
Disclaimer: Rendering of this example could be improved in many ways. I just teach some CSS, I don't do websites :) - Daniel K. Schneider (talk) 19:00, 6 May 2013 (CEST)