`

CSS Paged Media - @page Rule

阅读更多

Paged media differ from continuous media in that the content of the document is split into one or more discrete pages. Paged media includes paper, transparencies, pages that are displayed on computer screens, etc.

The CSS2 standard introduces some basic pagination control features that let authors help the browser figure out how to best print their documents.

The CSS2 page model specifies how a document is formatted within a rectangular area -- the page box -- that has a finite width and height. These features fall into two groups −

  • CSS2 features that define a particular page layout.
  • CSS2 features that control the pagination of a document.

Defining Pages : the @page rule

The CSS2 defines a "page box", a box of finite dimensions in which content is rendered. The page box is a rectangular region that contains two areas −

  • The page area − The page area includes the boxes laid out on that page. The edges of the page area act as the initial containing block for layout that occurs between page breaks.

  • The margin area − It surrounds the page area.

You can specify the dimensions, orientation, margins, etc., of a page box within an @page rule. The dimensions of the page box are set with the 'size' property. The dimensions of the page area are the dimensions of the page box minus the margin area.

For example, the following @page rule sets the page box size to 8.5 × 11 inches and creates '2cm' margin on all sides between the page box edge and the page area −

<styletype="text/css"><!--
   @page { size:8.5in 11in; margin: 2cm }
   --></style>

You can use the margin, margin-top, margin-bottom, margin-left, and margin-right properties within the @page rule to set margins for your page.

Finally, the marks property is used within the @page rule to create crop and registration marks outside the page box on the target sheet. By default, no marks are printed. You may use one or both of the crop and cross keywords to create crop marks and registration marks, respectively, on the target print page.

Setting Page Size

The size property specifies the size and orientation of a page box. There are four values which can be used for page size −

  • auto − The page box will be set to the size and orientation of the target sheet.

  • landscape − Overrides the target's orientation. The page box is the same size as the target, and the longer sides are horizontal.

  • portrait − Overrides the target's orientation. The page box is the same size as the target, and the shorter sides are horizontal.

  • length − Length values for the 'size' property create an absolute page box. If only one length value is specified, it sets both the width and height of the page box. Percentage values are not allowed for the 'size' property.

In the following example, the outer edges of the page box will align with the target. The percentage value on the 'margin' property is relative to the target size so if the target sheet dimensions are 21.0cm × 29.7cm (i.e., A4), the margins are 2.10cm and 2.97cm.

<styletype="text/css"><!--
   @page {
      size: auto;   /* auto is the initial value */
      margin: 10%;
   }
   --></style>

The following example sets the width of the page box to be 8.5 inches and the height to be 11 inches. The page box in this example requires a target sheet size of 8.5" × 11" or larger.

<styletype="text/css"><!--
   @page {
      size: 8.5in 11in;  /* width height */
   }
   --></style>

Once you create a named page layout, you can use it in your document by adding the page property to a style that is later applied to an element in your document. For example, this style renders all the tables in your document on landscape pages −

<style type="text/css">
   <!--
   @page { size : portrait }
   @page rotated { size : landscape }
   table { page : rotated }
   -->
</style>

Due to the above rule, while printing, if the browser encounters a <table> element in your document and the current page layout is the default portrait layout, it starts a new page and prints the table on a landscape page.

Left, Right, and First pages

When printing double-sided documents, the page boxes on left and right pages should be different. It can be expressed through two CSS pseudo-classes as follows −

<styletype="text/css"><!--
   @page :left {
      margin-left: 4cm;
      margin-right: 3cm;
   }

   @page :right {
      margin-left: 3cm;
      margin-right: 4cm;
   }
   --></style>

You can specify the style for the first page of a document with the :first pseudo-class −

<styletype="text/css"><!--
   @page { margin: 2cm } /* All margins set to 2cm */

   @page :first {
      margin-top: 10cm    /* Top margin on first page 10cm */
   }
   --></style>

Controlling Pagination

Unless you specify otherwise, page breaks occur only when the page format changes or when the content overflows the current page box. To otherwise force or suppress page breaks, use the page-break-before, page-break-after, and page-break-inside properties.

Both the page-break-before and page-break-after accept the auto, always, avoid, left, and right keywords.

The keyword auto is the default, it lets the browser generate page breaks as needed. The keyword always forces a page break before or after the element, while avoid suppresses a page break immediately before or after the element. The left and right keywords force one or two page breaks, so that the element is rendered on a left-hand or right-hand page.

Using pagination properties is quite straightforward. Suppose your document has level-1 headers start new chapters with level-2 headers to denote sections. You'd like each chapter to start on a new, right-hand page, but you don't want section headers to be split across a page break from the subsequent content. You can achieve this using following rule −

<styletype="text/css"><!--
   h1 { page-break-before : right }
   h2 { page-break-after : avoid }
   --></style>

Use only the auto and avoid values with the page-break-inside property. If you prefer that your tables not be broken across pages if possible, you would write the rule −

<styletype="text/css"><!--
   table { page-break-inside : avoid }
   --></style>

Controlling Widows and Orphans

In typographic lingo, orphans are those lines of a paragraph stranded at the bottom of a page due to a page break, while widows are those lines remaining at the top of a page following a page break. Generally, printed pages do not look attractive with single lines of text stranded at the top or bottom. Most printers try to leave at least two or more lines of text at the top or bottom of each page.

  • The orphans property specifies the minimum number of lines of a paragraph that must be left at the bottom of a page.

  • The widows property specifies the minimum number of lines of a paragraph that must be left at the top of a page.

Here is the example to create 4 lines at the bottom and 3 lines at the top of each page −

<styletype="text/css"><!--
   @page{orphans:4; widows:2;}
   --></style>

 

分享到:
评论

相关推荐

    print-css-rocks:CSS Paged Media教程和工具回顾(print-css.rocks的存储库)

    《CSS Paged Media教程与工具探索——以print-css.rocks为例》 在现代网页设计中,我们往往关注于屏幕上的视觉呈现,而忽略了另一种至关重要的形式——打印样式表(Print Stylesheets)。print-css.rocks是一个专门...

    CSS_3样式.pdf

    ### CSS3 样式知识点详解 #### CSS3 背景属性 在 CSS3 中,开发者可以使用一系列增强的功能来定制网页元素的背景效果。这些功能不仅提供了更强大的控制能力,还增强了网页的设计美感。 ##### background - **描述...

    SQL Server 2005分页显示存储过程

    CREATE PROCEDURE proc_paged_with_notin -- @pageIndex 当前页码 -- @pageSize 每页记录数 AS BEGIN SET NOCOUNT ON; DECLARE @timediff DATETIME; DECLARE @sql NVARCHAR(500); SELECT @timediff = GETDATE();...

    CSS Quick Syntax Reference

    How to use CSS property references like universal values, visual media, paged media, and more Who this book is for This book is a handy, pocket quick syntax reference for experienced CSS developers ...

    paged-js:关于paged.js的教程

    **paged-js: 深入理解与应用分页JavaScript库** **一、paged.js简介** paged.js 是一个基于JavaScript的开源库,专为创建印刷级的网页内容而设计。它利用了CSS的`@page`规则,将HTML文档转换为多页布局,非常适合...

    3-paged-hotel-site-new

    这是一个基于HTML的三页酒店网站项目,名为"3-paged-hotel-site-new"。这个项目可能包含主页、关于页面和预订页面,旨在为潜在客户提供一个简洁而有效的在线平台,了解酒店服务并进行预订操作。接下来,我们将深入...

    react-native-paged-contacts, 用于响应本机的分页联系人.zip

    react-native-paged-contacts, 用于响应本机的分页联系人 响应本地分页联系人页面式联系人管理器,用于响应本机。目前只收取 ,只支持获取联系人。 安装 iOS将 RCTPagedContacts.xcodeproj 添加到项目中。在项目目标...

    CSS3.0中文完全参考手册-chm版

    另外,还有一些如用户界面、表格、Paged Media等模块,丰富了网页设计的可能性。 《CSS3.0中文完全参考手册》作为一本全面的参考资料,不仅涵盖了上述所有内容,还可能包含更多实用技巧和案例,是开发者学习和查阅...

    paged-http-stream:将分页的http请求转换为Node.js中的页面流

    npm install paged-http-stream原料药var pages = pager(opts, next) 选项: method :默认为GET uri :要查询的网址。 包括查询字符串。 ...以及可以传递给典型的node.js http请求的任何其他内容(使用 )next = ...

    此内容专用于分页css

    除了基本的CSS属性,还有一些第三方库和框架,如Paged.js,它们提供了更高级的分页功能,包括自动生成分页链接、动态加载内容等。这些库通常会结合JavaScript和CSS,提供更完整、更易于使用的分页解决方案。 总的来...

    CSS2中文翻译(全文)

    7. **Paged Media**:CSS2对打印样式进行了优化,允许开发者定义页面分隔符、页眉和页脚,使得网页在打印时能保持良好的布局。 8. **透明度**:CSS2提供了`opacity`属性,允许调整元素的透明度,为网页动画和交互...

    paged-request:对分页(分页)内容的简化请求

    $ npm install --save paged-request 小心! 有关在v2.0中进行的更改的信息,请参见。 用法 只要用户提供的next()函数返回字符串(要获取的下一个URL),该库就会递归调用.get方法。 看。 例子 const request = ...

    CSS2.0 中文手册

    9. **Paged Media**:CSS2.0还考虑到了打印媒体,可以控制页面分页、页眉页脚、页面边距等,确保打印出来的内容格式良好。 ### 使用CSS2.0中文手册的方法 《CSS2.0中文手册》通常包含以下部分: - **概述**:介绍...

    pdf:关于paged.js的教程

    paged.js是基于CSS Paged Media模块的开源库,该模块允许开发者将网页设计应用于打印和PDF输出。通过模拟打印机的工作方式,paged.js可以将连续的HTML内容分割成单独的页面,确保内容在每个页面上都能得到适当的展示...

    unigui0.83.5.820

    - 0000689: CustomFiles property for ServerModule to add custom CSS and JS files - 0000688: Bug in installer Environment setter - 0000687: "Script" property for TUniForm for adding Custom JS - 0000665...

    CSS: The Definitive Guide(后缀丢失,下载请自行添加.pdf 后查看)

    5. **分页媒体(Paged Media)**: - 针对打印输出进行优化的设计技巧。 - 分页控制及页面布局调整。 #### 作者经验分享 - Eric Meyer以其丰富的经验和清晰、诚实的写作风格,详细探讨了每个CSS属性及其与其他属性...

    pagedjs:在浏览器中显示分页内容,并使用网络技术生成印刷书籍

    Paged.js-分页媒体工具 ... Pagedmedia.org上提供了有关 Media CSS和Paged.js入门的快速概述。 NPM模块 $ npm install pagedjs import { Previewer } from 'pagedjs' ; let paged = new Previewer ( ) ; let flow =

    jqPagination示例

    paged: function(page) { // 在这里处理分页请求,例如通过AJAX获取第page页的数据 } }); }); ``` `paged`参数是一个回调函数,当用户点击分页链接时会触发此函数,参数`page`表示用户请求的页码。 ### 3. ...

    searching-sorting-paged-table

    SearchingSortingPagingTable 该项目是使用版本6.0.8生成的。 开发服务器 为开发服务器运行ng serve 。... 如果您更改任何源文件,该应用程序将自动重新加载。 代码脚手架 运行ng generate component component-name...

    利用javascript实现网页打印

    在CSS中,我们可以使用媒体查询(Media Queries)来定义针对不同设备或打印环境的样式。例如,我们可以隐藏只在屏幕显示的元素,确保它们不会出现在打印版面中: ```css @media print { .no-print { display: ...

Global site tag (gtag.js) - Google Analytics