Showing posts with label CSS Printing. Show all posts
Showing posts with label CSS Printing. Show all posts

Print style sheet - the definitive guide | css-edge.blogspot.com

A print stylesheet formats a web page so when printed, it automatically prints in a user-friendly format. Print stylesheets have been around for a number of years and have been written about a lot. Yet so few websites implement them, meaning we're left with web pages that frustratingly don't properly print on to paper.

It's remarkable that so few websites use print stylesheets as:

  • Print stylesheets enormously improve usability, especially for pages with a lot of content (such as this one!)
  • They're phenomenally quick and easy to set up

Some websites do offer a link to a print-friendly version of the page, but this of course needs to be set up and maintained. It also requires that users notice this link on the screen, and then use it ahead of the regular way they print pages (e.g. by selecting the print button at the top of the screen). Print-friendly versions are however useful when printing a number of web pages at the same time such as an article that spans on to several web pages.

How to set up your print stylesheet

A print stylesheet works in much the same way as a regular stylesheet, except it only gets called up when the page is printed. To make it work, the following needs to be inserted into the top of every web page:

<link rel="stylesheet" href="print.css" type="text/css" media="print" />

The file, print.css is the print stylesheet, and the media="print" command means that this CSS file only gets called up when web pages are printed. (There are many different media you can use for stylesheets, such as for handheld, TV, projection etc. - see a full list of media types for more.)

What to put in your print stylesheet

The CSS commands in the print stylesheet essentially override the CSS commands in the main stylesheet. As such, the only commands you need to put in the print stylesheet are ones to override the CSS commands in the main stylesheet. This means you don't need to repeat any colour or branding CSS commands as they'll already be taken from the main stylesheet.

Generally speaking, you'll want your print stylesheet to make the following happen when users hit that print button:

Remove unwanted items

Usually it's just your organisation logo and page content that you'll want to appear on the printed version of the web page. You'll normally want to remove the header, left column and right column. You may also want to remove the footer (or some of it) from the printed version, unless it contains your contact details.

There may be certain isolated items you'd prefer weren't printed so you can simply assign these class="noprint" in the HTML. To get rid of these items, along with the header and navigation (assuming these are assigned <div id="header"> and <div id="nav">) use the display: none command:

#header, #nav, .noprint {display: none;}

You may also want to remove certain images and adverts, especially animated images as these won't make sense when printed.

Format the page

There's nothing worse than printing off a web page to find the last few words of each line cut off. It's also annoying (and a waste of paper) when the left and right columns are left in, leaving a very narrow space for the content so the web page prints on to 15 pieces of paper.

Generally speaking, the three CSS commands you'll need are:

width: 100%; margin: 0; float: none;

These commands should be applied to any containing elements (<div>s for a CSS layout and <table>s for table layouts) to ensure the content spans the full width of the paper. So, the full CSS command would perhaps be something like:

#container, #container2, #content {width: 100%; margin: 0; float: none;}

Change the font?

Some print stylesheets do change the font size (often to 12pt) but this isn't generally a very good idea. If users increase text size on the screen then the text will print in this larger font size... unless you specify a fixed font size in the print stylesheet.

Other print stylesheets change the font family to a serif font (such as Times New Roman) as this is slightly easier to read from print. Whether you choose to do this or not is up to you as users may be a bit surprised to see a different font printed out.

Do also bear in mind that background images and colours don't print out by default. As such, you may wish to change the colour of text in a light colour so it has a reasonable colour contrast without its background.

Links

Print-outs are often in black and white so do make sure that links have a decent colour contrast. If not, assign links a slightly darker colour in the print out. For example:

a:link, a:visited {color: #781351}

For bonus usability you could include a footnote on the page listing all the URLs from that page, with each link referencing its URL underneath with a number. It's otherwise impossible to know where a link is pointing to when reading a print out from a web page. See this working example and find out how to do this by reading this Improving link display for print article.

Making the print stylesheet

When making the print stylesheet place the print CSS commands into the bottom of your main CSS file. As you keep adding more commands check how your web pages look on the computer screen (don't do this on a live website!). Keep adding commands until you're happy with the appearance, then cut these commands out of the main CSS file and paste into the print stylesheet.

To summarise, your print stylesheet may look similar to this:

/* Remove unwanted elements */
#header, #nav, .noprint
{
display: none;
}
/* Ensure the content spans the full width */
#container, #container2, #content
{
width: 100%; margin: 0; float: none;
}
/* Change text colour to black (useful for light text on a dark background) */
.lighttext
{
color: #000
}
/* Improve colour contrast of links */
a:link, a:visited
{
color: #781351
}

You've now got a print stylesheet! For something this quick and easy to set up that improves usability as much as it does, you'd be mad not to use one!

http://css-edge.blogspot.com

CSS ORPHANS

This property specifies the minimum number of lines of content for the current element that must be left at the bottom of a page in a paged display environment.

Example
p { orphans: 4 }

test text in the paragraph

Possible Values
Value Description
inherit Explicitly sets the value of this property to that of the parent.
[integer] Specifies an integer value representing the minimum number of lines of content that must be left at the bottom of a page.

CSS WIDOWS

This property specifies the minimum number of lines of content for the current element that must be left at the top of a page in a paged display environment.

Example
p { widows: 1 }

test text in the paragraph

Possible Values
Value Description
inherit Explicitly sets the value of this property to that of the parent.
[integer] Specifies an integer value representing the minimum number of lines of content that must be left at the top of a page.

CSS MARKS

Printed documents in the printing industry often carry marks on the page outside the content area. These marks are used to align and trim groups of papers.
This property specifies what sort of marks should be rendered just outside the rendered page box. The characteristics and position of the marks will be browser dependent.

Example
body { marks: crop cross }

test text in the body

Possible Values
Value Description
inherit Explicitly sets the value of this property to that of the parent.
none No page marking will occur.
crop Specifies that crop marks be used to indicate where the page should be cut.
cross Specifies that cross-hair marks be rendered in order to precisely align the current page with other pages carrying ‘cross' marks.

CSS SIZE

This property describes the orientation or dimensions of the page box. The ’size’ property classifies a page box so that it is either ‘relative’ or ‘absolute’. ‘Absolute’ page boxes have a fixed size, whereas ‘relative’ page boxes will be scaled to fit the target paged media. In the case where a page box is smaller than the intended ’size’, CSS2 recommends that it be centered on the page to allow for better alignment of multiple pages.

Example
body { size: 8.5in 11in }

test text in the body

Possible Values
Value Description
inherit Explicitly sets the value of this property to that of the parent.
auto This value defines a ‘relative' page box, setting the values to the size and orientation of the target page.
portrait This value defines a ‘relative' page box, overriding the target page's current default content orientation, using the typical paradigm where content flows from left to right across the short dimension, and flowing down along the long dimension.
landscape This value defines a ‘relative' page box, overriding the target page's current default content orientation, using the landscape method, where content flows from left to right across the long dimension, and flowing down along the short dimension. This method is often used when a given line will contain larger amounts of content than normal.
[length] Giving length values for this property creates a page box with fixed dimensions, eg: an ‘absolute' page box. Setting only one length value with this value creates a square page box of equal height and width. If two values are specified, the first value represents the page width, and the second represents the page height.

CSS PAGE

This property is used to specify a specific page type to use when displaying an element box. If the value specified is different than the one for the rendered element box that precedes it, one or two page breaks should be inserted between them, and the element box should then be rendered on a page box of the indicated type.

Example
@page doublepage { size: 8.5in 11in; page-break-after: left }

body { page: doublepage; page-break-after: right }

test text in the body

Possible Values
Value Description
auto References the current default page.
[identifier] Specifies an identifier for a page type defined in an @page rule.

CSS PAGE-BREAK-AFTER

This property specifies the page-breaking behavior that should occur after an element box and on what side of the page the content that follows should resume on. Page breaks are not allowed in absolutely positioned elements.

Example
p { page-break-after: always }

Test text in the paragraph

Possible Values
Value Description
inherit Explicitly sets the value of this property to that of the parent.
auto Insert a page break before the element as necessary.
avoid Avoid inserting a page break before the current element box.
leftright left: Force one or two page breaks after the current element box until a blank left page is reached.

right: Force one or two page breaks after the current element box until a blank right page is reached.

always Always force a page break before the current element box.
{empty string] No property value is used in this case. A page break is not inserted before the current element box.

CSS PAGE-BREAK-INSIDE

This property specifies the page-breaking behavior that should occur inside an element’s rendering box. Page breaks are not allowed in absolutely positioned elements.

Example
p { page-break-inside: auto }

Test text in the paragraph

Possible Values
Value Description
inherit Explicitly sets the value of this property to that of the parent.
auto Insert page breaks inside the element box as necessary.
avoid Avoid inserting page breaks inside the current element box if possible.

CSS PAGE-BREAK-BEFORE

This property specifies the page-breaking behavior that should occur before an element box and on what side of the page the content that follows should resume on. Page breaks are not allowed in absolutely positioned elements.


CSS2 “suggests” when page-breaking should occur:

  • Page-breaking should occur as few times as possible.
  • Page-breaking should be avoided inside these elements: tables, floated elements and block elements with borders.
  • Pages that are not forced to break should have approximately the same height.

Example
p { page-break-before: always }

Test text in the paragraph

Possible Values
Value Description
inherit Explicitly sets the value of this property to that of the parent.
auto Insert a page break before the element as necessary.
avoid Avoid inserting a page break before the current element box.
leftright left: Force one or two page breaks before the current element box until a blank left page is reached.

right: Force one or two page breaks before the current element box until a blank right page is reached.

always Always force a page break before the current element box.
{empty string] No property value is used in this case. A page break is not inserted before the current element box.