SVG グラデーション - 線形


SVG グラデーション

グラデーションは、ある色から別な色へのスムーズな遷移です。 さらに、いくつかの色の遷移を、同じ要素に適用することができます。

SVG のグラデーションには、2 つの主要なタイプがあります:


SVG 線形グラデーション - <linearGradient>

<linearGradient> 要素は、線形グラデーションを定義するために使用します。

<linearGradient> 要素は、<defs> タグ内にネストされている必要があります。 <defs> タグは、definitions の略語であり、特別な要素(例えば、グラデーションなど)の定義が含まれます。

線形グラデーションは、水平、垂直方向または傾斜角により定義することができます:


例 1

黄色から赤への水平方向の線形グラデーションを持つ楕円を定義します:

Sorry, your browser does not support inline SVG.

SVG コードは次の通りです:

<svg height="150" width="400">
  <defs>
    <linearGradient id="grad1" x1="0%" y1="0%" x2="100%" y2="0%">
      <stop offset="0%" style="stop-color:rgb(255,255,0);stop-opacity:1" />
      <stop offset="100%" style="stop-color:rgb(255,0,0);stop-opacity:1" />
    </linearGradient>
  </defs>
  <ellipse cx="200" cy="70" rx="85" ry="55" fill="url(#grad1)" />
</svg>
Try it Yourself »

コードの説明:


例 2

黄色から赤への垂直方向の線形グラデーションを持つ楕円を定義します:

Sorry, your browser does not support inline SVG.

SVG コードは次の通りです:

<svg height="150" width="400">
  <defs>
    <linearGradient id="grad2" x1="0%" y1="0%" x2="0%" y2="100%">
      <stop offset="0%" style="stop-color:rgb(255,255,0);stop-opacity:1" />
      <stop offset="100%" style="stop-color:rgb(255,0,0);stop-opacity:1" />
    </linearGradient>
  </defs>
  <ellipse cx="200" cy="70" rx="85" ry="55" fill="url(#grad2)" />
</svg>
Try it Yourself »

例 3

黄色から赤へ水平方向の線形グラデーションを持つ楕円を定義し、楕円の中にテキストを追加します:

SVG Sorry, your browser does not support inline SVG.

SVG コードは次の通りです:

<svg height="150" width="400">
  <defs>
    <linearGradient id="grad3" x1="0%" y1="0%" x2="100%" y2="0%">
      <stop offset="0%" style="stop-color:rgb(255,255,0);stop-opacity:1" />
      <stop offset="100%" style="stop-color:rgb(255,0,0);stop-opacity:1" />
    </linearGradient>
  </defs>
  <ellipse cx="200" cy="70" rx="85" ry="55" fill="url(#grad3)" />
  <text fill="#ffffff" font-size="45" font-family="Verdana" x="150" y="86">
  SVG</text>
</svg>
Try it Yourself »

コードの説明: