<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

    主站蜘蛛池模板: 全免费a级毛片免费看无码| 国产成人一区二区三区视频免费| 亚洲国产中文在线视频| 久久综合亚洲色一区二区三区| 在线免费观看亚洲| 日韩精品无码免费一区二区三区 | 亚洲AV无码乱码在线观看| 亚洲日韩精品无码专区网站| 久久亚洲国产伦理| 亚洲中文字幕乱码一区| 国产高清视频免费在线观看| 国产四虎免费精品视频| 免费一级特黄特色大片在线观看 | 亚洲国产精品不卡在线电影| 亚洲爆乳无码精品AAA片蜜桃| 曰韩无码AV片免费播放不卡| 无码中文字幕av免费放| 中文字幕在线亚洲精品| 在线综合亚洲欧洲综合网站| 久久青草免费91线频观看不卡 | 最近免费中文字幕大全免费版视频| 亚洲AV中文无码乱人伦| 亚洲一级片免费看| 亚洲国产综合精品中文第一区 | 美女黄色免费网站| 国产成人免费高清激情明星| 中文字幕专区在线亚洲| 亚洲AV无码国产精品永久一区| 亚洲大片免费观看| 亚洲中文字幕日产乱码高清app| 久久青草免费91线频观看不卡| 久久精品国产亚洲av麻豆图片| 无码av免费网站| 亚洲三区在线观看无套内射| 精品无码专区亚洲| 午夜时刻免费入口| 亚洲一区二区三区丝袜| 亚洲一区日韩高清中文字幕亚洲| 一级毛片免费播放| 免费国产在线精品一区 | 在线a毛片免费视频观看|