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

浏览器兼容性

另见