stroke

**stroke** 屬性是一個表示屬性,用於定義用於繪製形狀輪廓的顏色(*或任何 SVG 繪製伺服器,如漸變或圖案*)。

注意:作為表示屬性,stroke 可以用作 CSS 屬性。有關更多資訊,請參閱 stroke

您可以將此屬性與以下 SVG 元素一起使用

示例

基本顏色和漸變描邊

html
<svg viewBox="0 0 20 10" xmlns="http://www.w3.org/2000/svg">
  <!-- Simple 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
可動畫

規範

規範
可縮放向量圖形 (SVG) 2
# SpecifyingStrokePaint

瀏覽器相容性

BCD 表格僅在啟用 JavaScript 的瀏覽器中載入。