<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    lqxue

    常用鏈接

    統(tǒng)計

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

    主站蜘蛛池模板: 亚洲精品成人无限看| 中文字幕亚洲男人的天堂网络| 亚洲视频在线免费播放| 亚洲色成人四虎在线观看| 亚洲国产精品激情在线观看| 久章草在线精品视频免费观看| 77777午夜亚洲| 久久精品亚洲乱码伦伦中文| 日韩精品久久久久久免费| 国产AV无码专区亚洲AV蜜芽| 久久久青草青青亚洲国产免观| 久久WWW免费人成人片| 国产成人无码免费网站| 国产成人亚洲精品| 亚洲成AV人片在线观看无| 超pen个人视频国产免费观看| 日本一区二区免费看| 亚洲AV网一区二区三区| 精品亚洲国产成AV人片传媒| 四虎精品亚洲一区二区三区| 亚洲国产精品免费观看| 在线观看免费无码视频| 亚洲а∨精品天堂在线| 亚洲欧洲国产日韩精品| 亚洲精品偷拍视频免费观看 | 免费观看一区二区三区| 亚洲日韩精品A∨片无码加勒比| 久久精品国产亚洲AV麻豆~| 国产免费卡一卡三卡乱码| ww在线观视频免费观看| 中文字幕在线免费播放| 亚洲AV无码成人网站在线观看 | 亚洲国产午夜精品理论片在线播放 | 亚洲日本VA午夜在线影院| 亚洲AV成人片色在线观看高潮| 亚洲av无码乱码在线观看野外 | 免费无码又爽又刺激高潮视频| 一级人做人a爰免费视频| 亚洲国产精品网站在线播放 | 色多多A级毛片免费看| 一本天堂ⅴ无码亚洲道久久|