lqxue

常用链接

统计

book

tools

最新评论

CSS 控制打印

From:http://martybugs.net/articles/print.cgi

Web Page Printability With CSS
author: Martin "mpot" Pot
date: 7 January 2005


Introduction

This article describes how to use CSS media types to make your website much more printer-friendly. An overview of CSS media types is given, and sample HTML and CSS code is provided, detailing how to implement CSS media types for improving printability.

Why?

Most webpages do not print very well, with large amounts of the printed page being wasted with banners, navigation menus, advertising, and other unnecessary content. When attempting to print out the main text of a webpage, you often have to resort to copying the text into Word or another editor, and then printing it.

Some websites make use of a link at the bottom of the page, linking to a "printable version" of the same page. However, this requires additional effort on the part of the web developer.

There is a much easier way to make your pages print better, and that's via the use of CSS media types.

For example, all content on MartyBugs.Net has been designed to be printer-friendly. When printing pages from this site using a modern browser, elements such as the left menu column and the right column (where used) will be hidden, thus ensuring there's more room on the printed page for the main content.

page viewed on screen
page as displayed on a 1024x768 screen

The image above shows how one of the pages on this website would be displayed on a computer screen, at a resolution of 1024x768.

printed page
printed page, optimised with CSS media types
printed page,
no print optimisation

printed page,
optimised for printing

The left image above shows how the same page will look when printed, if all the page content is printed. Notice how little room there is in the centre of the page for the main content.

The right image above shows the same page, but the navigation and other content on the left and right hand sides of the page is not printed. CSS media types have been used to hide this content when the page is printed.

CSS Media Types

The primary aim of the CSS media types is to allow the web-page author to define different styles for a page, depending on the media being used to display the page. CSS 2.1 supports numerous media types, including all (suitable for all devices), screen (computer screens), print (pages viewed on-screen in print-preview mode and printed pages).
Other less-common media types are also supported, such as braille, embossed, handheld, projection, speech, etc, but we won't be discussing any of these.

CSS media types are defined using the rule in your CSS style-sheet, or in your in-line CSS style definitions. For example, to define different font sizes for the print and screen media types, as well as a common line-height to be used for both, the CSS definition is as follows:

  @media print {
body { font-size: 10pt }
}
@media screen {
body { font-size: 13px }
}
@media screen, print {
body { line-height: 1.2 }
}

The CSS media types allow you to specify different CSS properties for any element, so it will be displayed differently on the screen than when printed. For the purposes of this article, the only thing we're interested in is to hide some page elements when the page is printed.

Refer to the W3C CSS 2.1 Media Definitions for more details on CSS media types.

Using CSS Media Types

The web pages on this website are all generated on-the-fly, using server-side Perl templates. All pages have a number of common elements, namely:
  • a banner across the top
  • navigation menu on the left side
  • main content in the centre
  • advertising and other content on the right side
  • footer across the bottom
When someone prints out a page from this website, CSS media types are used to hide a number of elements on the page, including the navigation menu, advertising, and the navigation content in the footer.

To provide a working demonstration of how CSS media types can be used in this way, a demonstration web page was coded, using tables to define the banner area across the top of the page, with a column down the left-hand side for the navigation menu, a column down the right-hand side for other content, and a footer across the bottom of the page.

This layout is one of the most commonly used page layouts on the internet at the present - hence my choice to use it as an example.

the example page
the example page

Firstly, we define the CSS styles for the page:

  <style type="text/css">
@media print {
.noprint { display: none; }
}

</style>

Note that the CSS styles can also be defined using a separate CSS file, but example page has the CSS defined in-line for simplicity (and to keep the example to a single file).

The above CSS definition is defining a style called noprint, and will only be applied to printed content. Setting the display property to none means any content using this CSS style will not be displayed when printed, but will be displayed for all other media types.

There are a number of components on this page that we don't want printed, namely the columns on the left-hand side and on the right-hand side. The noprint style is applied to the table cells holding this content, thus ensuring they won't be visible when the page is printed.

<table border='1' cellpadding='4' cellspacing='0' width='100%' >
<tr height='60'><td colspan='3'>
banner place-holder
</td></tr>
<tr><td width='150' valign='top' class='noprint'>
left-side content place-holder
</td><td>
<b>main content</b>
[snip!]
</td><td width='150' valign='top' class='noprint'>
right-side content place-holder
</td></tr><tr><td colspan='3'>
footer content place-holder
</td></tr>
</table>

This will result in the columns on the left and right-hand sides being hidden when the page is printed, thus providing more space on the printed page for the content itself.

Note that this style needs to be applied to the table cells themselves, and not just to the content in these table cells, else the (empty) cells will still show up on the printed copy.

Testing Your CSS

The easiest way to test CSS styles for print media is to use a web browser which has print preview capabilities. The print preview will give you a fairly accurate representation of how the page will look when it is printed, and is a much more efficient way to test your media styles, rather than actually printing out pages on a printer.

Most popular web browsers should support print preview functionality. The FireFox browser has print preview functionality, although it lacks any zoom/unzoom capabilities.

As an alternative to using print preview, you can print the web page to a PDF file. There are numerous (costly, freeware, and anywhere in between) software packages for doing this. I recommend using the freeware and open-source PDFCreator, which is a Windows printer driver which allows you to create PDFs from any Windows application, just by selecting the PDFCreator driver as the virtual printer from that application.

Also be sure to use the W3C's free CSS Validation Service to check your CSS definitions.

References


posted on 2010-03-12 10:55 lqx 阅读(544) 评论(0)  编辑  收藏 所属分类: html


只有注册用户登录后才能发表评论。


网站导航:
博客园   IT新闻   Chat2DB   C++博客   博问