CSS for print tutorial: Difference between revisions

The educational technology and digital learning wiki
Jump to navigation Jump to search
Line 67: Line 67:
</source>
</source>


== Adapt style for printing ==
== Adapt styles for printing (minimal solution) ==


Most often, you will have to do the following:
Most often, you will have to do the following:
Line 99: Line 99:
  }
  }
</source>
</source>
In addition, you could print out URLs like this:
<source lang="CSS">
a[href^="http://"]:after, a[href^="ftp://"]:after
{
  content: " (" attr(href) ")";
  color: blue;
  font-size: small;
}
</source>
== Using pagination ==
=== @Page property ===
Instead of defining margins, you can use the @page directive.
=== Page breaks ===
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 and page-break-inside
* Each of these can take the following values: auto,always,avoid,left,right
Below are some typical use cases.
; page-break-before
: defines if page breaks should occur before the tag, typically titles
: Example
<source lang="CSS">
section {page-break-before: always;}
h1 {page-break-before: always;}
</source>
; page-break-after
: defines if page breaks should occur after a tag
<source lang="CSS">
section {page-break-after: always;}
h1 {page-break-after: avoid;}
</source>
; page-break-inside
: defines if page breaks should occur inside an element.
<source lang="CSS">
p {page-break-inside: avoid;}
</source>
== Links ==
; Specifications
* [http://www.w3.org/TR/CSS21/page.html Paged media] Chapter 13 for CSS 2.1
; Other tutorials
* [http://alistapart.com/article/goingtoprint/ CSS Design: Going to Print], by Eric Meyer, A list Apart, May 10, 2002
* [http://www.onextrapixel.com/2009/05/05/how-to-create-a-simple-print-css-for-your-site/ How To Create A Simple Print CSS For Your Site] May 5th, 2009 by Terrance
* [http://www.webcredible.co.uk/user-friendly-resources/css/print-stylesheet.shtml Print stylesheet - the definitive guide], by Paul Paul McCarthy], undated.
* [http://www.transmissionzero.co.uk/computing/css-for-printing/ CSS for Printed Media], Transmission Zero, undated.
* [http://designm.ag/tutorials/how-to-properly-design-a-print-css-stylesheet/ How to Properly Design a Print CSS Stylesheet] by Jake Rocheleau, December 29, 2011

Revision as of 16:58, 6 May 2013

Draft

This article or section is currently under construction

In principle, someone is working on it and there should be a better version in a not so distant future.
If you want to modify this page, please discuss it with the person working on it (see the "history")

<pageby nominor="false" comments="false"/>

Introduction

Learning goals
  • Be able to style text-centric HTML pages (e.g. articles, tutorials, novels)
Prerequisites
Level and target population
  • Beginners
Teaching materials
Remarks
  • This tutorial is intended for students in educational technology or any other field that is technology intensive. For people who need less, there exist many easy CSS tutorials on the web. This text is intended for students who also must learn principles and who are willing to learn CSS by doing a project, looking at CSS code and online reference manuals.
  • Ideally, a teacher also should assign a text formatting task, during or before assigning this tutorial for reading).

Linking stylesheet for printing

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

The @import at-rule allows to specify a media type, i.e. you may use this strategy to load various CSS variants from a single CSS file or within a HTML script section.

Inside a CSS file:

@import url(print-style.css) print;

Inside an HTML file

<style type="text/css">
@import url(print-style.css) print;
</style>

Adapt styles for printing (minimal solution)

Most often, you will have to do the following:

(1) Remove unwanted items, e.g.

 #navigation, .do-not-print, #menu {display:none}
Of course, you will have to adapt this to your CSS. #navigation is an example of an element ID and .do-not-print and example of a class.

(2) Contents should not float and width should be 100% or auto

(3) Contrasts should be optimized for a printer.

E.g. text should be black and the background should be white, or at least print ok on a printer with grey levels.

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;
}

Using pagination

@Page property

Instead of defining margins, you can use the @page directive.

Page breaks

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 and page-break-inside
  • Each of these can take the following values: auto,always,avoid,left,right


Below are some typical use cases.

page-break-before
defines if page breaks should occur before the tag, typically titles
Example
 section {page-break-before: always;}
 h1 {page-break-before: always;}
page-break-after
defines if page breaks should occur after a tag
 section {page-break-after: always;}
 h1 {page-break-after: avoid;}
page-break-inside
defines if page breaks should occur inside an element.
 p {page-break-inside: avoid;}


Links

Specifications
Other tutorials