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
# 指定描邊繪畫

瀏覽器相容性

另見