@page
Baseline 2024 *
新推出
@page 规则是 CSS 中的一种 @规则,用于修改打印页面的不同方面。它针对并修改页面的尺寸、方向和外边距。@page 规则可用于针对打印输出中的所有页面,或使用其各种伪类来针对页面的子集。
语法
/* Targets all the pages */
@page {
size: 8.5in 9in;
margin-top: 4in;
}
/* Targets all even-numbered pages */
@page :left {
margin-top: 4in;
}
/* Targets all odd-numbered pages */
@page :right {
size: 11in;
margin-top: 4in;
}
/* Targets all selectors with `page: wide;` set */
@page wide {
size: a4 landscape;
}
@page {
/* margin box at top right showing page number */
@top-right {
content: "Page " counter(pageNumber);
}
}
页面属性
@page 规则只能包含页面描述符和外边距 @规则。以下描述符已被至少一个浏览器实现:
margin-
指定页面外边距。也可以使用单独的外边距属性,如
margin-top、margin-right、margin-bottom和margin-left。 页面方向-
指定页面的方向。这不会影响页面的布局;旋转是在布局之后应用于输出介质的。
size-
指定页面盒子的包含块的目标尺寸和方向。在一般情况下,当一个页面盒子渲染到一个页面上时,它也表示目标页面的尺寸。
规范提到以下 CSS 属性可以通过 @page 规则应用于页面盒子。但这些属性尚未被任何用户代理支持。
其余页面属性
| 特性 | CSS 属性 |
|---|---|
| bidi 属性 | direction |
| background 属性 | background-color |
| background-image | |
| background-repeat | |
| background-attachment | |
| background-position | |
| background | |
| border 属性 | border-top-width |
| border-right-width | |
| border-bottom-width | |
| border-left-width | |
| border-width | |
| border-top-color | |
| border-right-color | |
| border-bottom-color | |
| border-left-color | |
| border-color | |
| border-top-style | |
| border-right-style | |
| border-bottom-style | |
| border-left-style | |
| border-short-style | |
| border-top | |
| border-right | |
| border-bottom | |
| border-left | |
| border | |
| counter 属性 | counter-reset |
| counter-increment | |
| color | color |
| font 属性 | font-family |
| font-size | |
| font-style | |
| font-variant | |
| font-weight | |
| font | |
| height 属性 | height |
| min-height | |
| max-height | |
| line height | line-height |
| margin 属性 | margin-top |
| margin-right | |
| margin-bottom | |
| margin-left | |
| margin | |
| outline 属性 | outline-width |
| outline-style | |
| outline-color | |
| outline | |
| padding 属性 | padding-top |
| padding-right | |
| padding-bottom | |
| padding-left | |
| padding | |
| quotes | quotes |
| text 属性 | letter-spacing |
| text-align | |
| text-decoration | |
| text-indent | |
| text-transform | |
| white-space | |
| word-spacing | |
| visibility | visibility |
| width 属性 | width |
| min-width | |
| max-width |
描述
@page 规则定义了页面盒子的属性。@page 规则可以通过 CSS 对象模型接口 CSSPageRule 进行访问。
备注: W3C 正在讨论如何处理与视口相关的<length>单位,即 vh、vw、vmin 和 vmax。在此期间,请不要在 @page 规则中使用它们。
相关属性
@page 规则允许用户为规则指定一个名称,然后在声明中通过 page 属性来调用。
page-
允许选择器使用用户定义的命名页面
正式语法
@page =
@page <page-selector-list>? { <declaration-rule-list> }
<page-selector-list> =
<page-selector>#
<page-selector> =
[ <ident-token>? <pseudo-page>* ]!
<pseudo-page> =
: [ left | right | first | blank ]
其中 <page-body> 包括:
- 页面属性
- 页面外边距属性
以及 <pseudo-page> 代表以下这些伪类:
外边距 @规则
外边距 @规则在 @page 规则内部使用。它们各自针对文档打印页面的不同区域,根据样式块中设置的属性值为打印页面的区域设置样式。
@page {
@top-left {
/* page-margin-properties */
}
}
@top-left 针对文档的左上角,并根据设置的页面外边距属性应用更改。
其他外边距 @规则包括:
@top-left-corner
@top-left
@top-center
@top-right
@top-right-corner
@bottom-left-corner
@bottom-left
@bottom-center
@bottom-right
@bottom-right-corner
@left-top
@left-middle
@left-bottom
@right-top
@right-middle
@right-bottom
页面外边距属性
页面外边距属性是一组 CSS 属性,可以在任何单独的外边距 @规则中设置。它们包括:
页面外边距属性
| 特性 | CSS 属性 |
|---|---|
| bidi 属性 | direction |
| background 属性 | background-color |
| background-image | |
| background-repeat | |
| background-attachment | |
| background-position | |
| background | |
| border 属性 | border-top-width |
| border-right-width | |
| border-bottom-width | |
| border-left-width | |
| border-width | |
| border-top-color | |
| border-right-color | |
| border-bottom-color | |
| border-left-color | |
| border-color | |
| border-top-style | |
| border-right-style | |
| border-bottom-style | |
| border-left-style | |
| border-short-style | |
| border-top | |
| border-right | |
| border-bottom | |
| border-left | |
| border | |
| counter 属性 | counter-reset |
| counter-increment | |
| content | content |
| color | color |
| font 属性 | font-family |
| font-size | |
| font-style | |
| font-variant | |
| font-weight | |
| font | |
| height 属性 | height |
| min-height | |
| max-height | |
| line height | line-height |
| margin 属性 | margin-top |
| margin-right | |
| margin-bottom | |
| margin-left | |
| margin | |
| outline 属性 | outline-width |
| outline-style | |
| outline-color | |
| outline | |
| padding 属性 | padding-top |
| padding-right | |
| padding-bottom | |
| padding-left | |
| padding | |
| quotes | quotes |
| text 属性 | letter-spacing |
| text-align | |
| text-decoration | |
| text-indent | |
| text-transform | |
| white-space | |
| word-spacing | |
| 垂直对齐 | vertical-align |
| visibility | visibility |
| width 属性 | width |
| min-width | |
| max-width | |
| z-index | z-index |
命名页面
命名页面能够在打印时以声明的方式执行逐页布局和添加分页符。
命名页面可以使用 page 属性来应用。这允许用户为打印布局创建不同的页面配置。
相关示例可以在 page 示例中找到。
示例
使用 size 属性更改页面方向
此示例展示了如何将 <section> 元素分割成单独的页面,并以横向格式打印,每个页面都有 20% 的外边距。点击打印按钮将启动一个打印对话框,其中 HTML 的各个部分被分割成独立的页面。
<button>Print page</button>
<article>
<section>
<h2>Header one</h2>
<p>Paragraph one.</p>
</section>
<section>
<h2>Header two</h2>
<p>Paragraph two.</p>
</section>
<section>
<h2>Header three</h2>
<p>Paragraph three.</p>
</section>
</article>
const button = document.querySelector("button");
button.addEventListener("click", () => {
window.print();
});
@page {
size: landscape;
margin: 2cm;
}
section {
page-break-after: always;
break-after: page;
}
@media print {
button {
display: none;
}
}
@page 伪类示例
有关示例,请参阅 @page 的各种伪类。
规范
| 规范 |
|---|
| CSS 分页媒体模块第 3 级 # at-page-rule |
| CSS 逻辑属性和值第 1 级 # page |
浏览器兼容性
加载中…
另见
page属性@page的size描述符- CSS 分页媒体模块
- [META] CSS Paged Media Module Level 3 Bugzilla,用于跟踪该主题的进展(基于页面的计数器等)