lqxue

          導(dǎo)航

          <2010年3月>
          28123456
          78910111213
          14151617181920
          21222324252627
          28293031123
          45678910

          留言簿(5)

          隨筆分類

          文章分類

          收藏夾

          隨筆檔案

          文章檔案

          相冊(cè)

          閱讀排行榜

          評(píng)論排行榜

          常用鏈接

          統(tǒng)計(jì)

          book

          tools

          最新評(píng)論

          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 閱讀(561) 評(píng)論(0)  編輯  收藏 所屬分類: html

          主站蜘蛛池模板: 涞源县| 麻阳| 黄冈市| 阳山县| 广元市| 灌云县| 大同县| 宣化县| 岳阳县| 股票| 韶关市| 台东县| 江门市| 名山县| 满城县| 池州市| 册亨县| 肃宁县| 太和县| 峨眉山市| 广州市| 桑日县| 南涧| 连南| 青神县| 普格县| 临武县| 林州市| 吉安市| 南皮县| 山丹县| 晋宁县| 乐清市| 隆尧县| 彝良县| 乌兰浩特市| 宣恩县| 苏尼特左旗| 碌曲县| 三明市| 翁牛特旗|