stroke

Baseline 广泛可用 *

此特性已经十分成熟,可在许多设备和浏览器版本上使用。自 2020 年 7 月以来,它已在各大浏览器中可用。

* 此特性的某些部分可能存在不同级别的支持。

stroke 属性是一个表示属性,用于定义用于绘制形状轮廓的颜色(或任何 SVG 绘画服务器,如渐变或图案)。

注意:作为一个表示属性,stroke 还有一个对应的 CSS 属性:stroke。当两者都被指定时,CSS 属性具有更高的优先级。

你可以将此属性与以下 SVG 元素一起使用

示例

基本颜色和渐变描边

html
<svg viewBox="0 0 20 10" xmlns="http://www.w3.org/2000/svg">
  <!-- Basic color stroke -->
  <circle cx="5" cy="5" r="4" fill="none" stroke="green" />

  <!-- Stroke a circle with a gradient -->
  <defs>
    <linearGradient id="myGradient">
      <stop offset="0%" stop-color="green" />
      <stop offset="100%" stop-color="white" />
    </linearGradient>
  </defs>

  <circle cx="15" cy="5" r="4" fill="none" stroke="url(#myGradient)" />
</svg>

输出如下:

context-stroke 示例

在此示例中,我们使用 <path> 元素定义了三个形状,每个形状都设置了不同的 strokefill 颜色。我们还通过 <marker> 元素将一个 <circle> 元素定义为一个标记。每个形状都通过 marker CSS 属性应用了该标记。

<circle> 元素设置了 stroke="context-stroke"fill="context-fill"。由于它在形状的上下文中被设置为标记,因此这些属性使它在每种情况下都能继承 <path> 元素上设置的 fillstroke 值。

html
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 300 90">
  <style>
    path {
      stroke-width: 2px;
      marker: url("#circle");
    }
  </style>
  <path d="M 10 44.64 L 30 10 L 70 10 L 90 44.64 L 70 79.28 L 30 79.28 Z"
        stroke="red" fill="orange" />
  <path d="M 100 44.64 L 80 10 L 120 10 L 140 44.64 L 120 79.28 L 80 79.28 Z"
        stroke="green" fill="lightgreen" />
  <path d="M 150 44.64 L 130 10 L 170 10 L 190 44.64 L 170 79.28 L 130 79.28 Z"
        stroke="blue" fill="lightblue" />
  <marker id="circle" markerWidth="12" markerHeight="12"
          refX="6" refY="6" markerUnits="userSpaceOnUse">
    <circle cx="6" cy="6" r="3" stroke-width="2"
            stroke="context-stroke" fill="context-fill"  />
  </marker>
</svg>

输出如下:

注意:当元素被 <use> 元素引用时,元素也可以使用 context-strokecontext-fill 来继承 strokefill 值。

用法说明

<paint>
默认值 none
可动画的

规范

规范
Scalable Vector Graphics (SVG) 2
# 指定描边绘画

浏览器兼容性

另见