@page

Baseline 2024 *
新推出

自 2024 年 12 月起,此功能可在最新的裝置和瀏覽器版本上使用。此功能可能無法在舊版裝置或瀏覽器上使用。

* 此特性的某些部分可能存在不同級別的支援。

@page 規則是 CSS 中的一種 @規則,用於修改列印頁面的不同方面。它針對並修改頁面的尺寸、方向和外邊距。@page 規則可用於針對列印輸出中的所有頁面,或使用其各種偽類來針對頁面的子集。

語法

css
/* 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-topmargin-rightmargin-bottommargin-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>單位,即 vhvwvminvmax。在此期間,請不要在 @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 規則內部使用。它們各自針對文件列印頁面的不同區域,根據樣式塊中設定的屬性值為列印頁面的區域設定樣式。

css
@page {
  @top-left {
    /* page-margin-properties */
  }
}

@top-left 針對文件的左上角,並根據設定的頁面外邊距屬性應用更改。

其他外邊距 @規則包括:

css
@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 的各個部分被分割成獨立的頁面。

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>
js
const button = document.querySelector("button");

button.addEventListener("click", () => {
  window.print();
});
css
@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

瀏覽器相容性

另見