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

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

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

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

    主站蜘蛛池模板: 日韩免费观看的一级毛片| 99麻豆久久久国产精品免费| 亚洲精品字幕在线观看| 911精品国产亚洲日本美国韩国| 中文字幕a∨在线乱码免费看| 亚洲欧洲日本在线| 日韩精品无码免费视频| 日韩电影免费在线观看| 台湾一级毛片永久免费| 亚洲人成电影网站国产精品| 成年网在线观看免费观看网址| 皇色在线视频免费网站| 亚洲中文字幕AV在天堂| 毛片免费在线视频| 亚洲电影中文字幕| 国产91免费视频| 成人午夜亚洲精品无码网站| 亚洲成a∨人片在无码2023| 在线观看免费污视频| 欧洲美女大片免费播放器视频| 亚洲人成人无码网www国产| 中文字幕成人免费高清在线| 亚洲AV无码不卡无码| 黄在线观看www免费看| 亚洲av色影在线| 麻豆国产精品免费视频| 鲁死你资源站亚洲av| 精品亚洲一区二区三区在线观看 | 野花高清在线电影观看免费视频| 精品亚洲AV无码一区二区 | 美女内射毛片在线看免费人动物| 精品亚洲成在人线AV无码| 一本久久综合亚洲鲁鲁五月天 | 国产成人亚洲综合网站不卡| 久久国产色AV免费观看| 亚洲高清国产AV拍精品青青草原| 4虎1515hh永久免费| 国产亚洲情侣久久精品| 午夜亚洲AV日韩AV无码大全| 成年女人男人免费视频播放 | 一级午夜a毛片免费视频|