paint-order

Baseline 2024
新推出

自 ⁨2024 年 3 月⁩ 起,此功能可在最新的裝置和瀏覽器版本上執行。此功能可能不適用於較舊的裝置或瀏覽器。

paint-order CSS 屬性允許您控制繪製文字內容和形狀的填充、描邊(以及繪製標記)的順序。

語法

css
/* Normal */
paint-order: normal;

/* Single values */
paint-order: stroke; /* draw the stroke first, then fill and markers */
paint-order: markers; /* draw the markers first, then fill and stroke */

/* Multiple values */
paint-order: stroke fill; /* draw the stroke first, then the fill, then the markers */
paint-order: markers stroke fill; /* draw markers, then stroke, then fill */

/* Global values */
paint-order: inherit;
paint-order: initial;
paint-order: revert;
paint-order: revert-layer;
paint-order: unset;

如果未指定任何值,則預設的繪製順序是 fillstrokemarkers

當指定一個值時,該值首先被繪製,然後是另外兩個值按照它們預設的相對順序繪製。當指定兩個值時,它們將按照指定的順序繪製,然後是未指定的值。

注意: 對於此屬性,標記僅適用於涉及使用 marker-* 屬性(例如 marker-start)和 <marker> 元素繪製 SVG 形狀的情況。它們不適用於 HTML 文字,因此在這種情況下,您只能確定 strokefill 的順序。

normal

按照正常的繪製順序繪製不同的專案。

strokefillmarkers

按照您希望它們被繪製的順序指定部分或所有這些值。

正式定義

初始值normal
應用於文字元素
繼承性
計算值同指定值
動畫型別離散

正式語法

paint-order = 
normal |
[ fill || stroke || markers ]
此語法反映了 CSS 規範 中的最新標準。並非所有瀏覽器都可能已實現所有部分。有關支援資訊,請參閱瀏覽器相容性

示例

反轉描邊和填充的繪製順序

SVG

html
<svg xmlns="http://www.w3.org/2000/svg" width="400" height="200">
  <text x="10" y="75">stroke in front</text>
  <text x="10" y="150" class="stroke-behind">stroke behind</text>
</svg>

CSS

css
text {
  font-family: sans-serif;
  font-size: 50px;
  font-weight: bold;
  fill: black;
  stroke: red;
  stroke-width: 4px;
}

.stroke-behind {
  paint-order: stroke fill;
}

結果

使用 HTML 反轉描邊和填充的繪製順序

要在 HTML 中控制填充和描邊的順序,您可以使用 -webkit-text-stroke-color-webkit-text-stroke-width CSS 屬性。

HTML

html
<div>stroke in front</div>
<div class="stroke-behind">stroke behind</div>

CSS

css
div {
  font-family: sans-serif;
  font-size: 50px;
  font-weight: bold;
  fill: black;
  padding-top: 10px;
  padding-bottom: 10px;
  -webkit-text-stroke-color: red;
  -webkit-text-stroke-width: 4px;
}

.stroke-behind {
  paint-order: stroke fill;
}

結果

規範

規範
Scalable Vector Graphics (SVG) 2
# PaintOrderProperty

瀏覽器相容性

另見