HTML SVG <linearGradient>

Beginner
9 min read
Updated: Nov 2026
5 examples
Live preview

Definition and Usage

The SVG <linearGradient> element defines a reusable linear color blend. Put it in <defs>, give it an id, add <stop> colors, then paint shapes with fill="url(#id)" or stroke="url(#id)".

Direction comes from the vector x1, y1, x2, y2 (default left to right). For circular blends, see radial gradients.

Baseline · Widely available.

<linearGradient> and <stop> are stable across modern browsers since about July 2015.

Deprecated: xlink:href.

Do not reuse gradients with xlink:href. Prefer a full local definition, or the modern href attribute when you need a template reference.

Not painted alone.

The gradient definition is invisible until a shape references it with url(#id).

Syntax

linear-gradient.html
<svg width="240" height="120" viewBox="0 0 240 120"
     xmlns="http://www.w3.org/2000/svg"
     role="img" aria-label="Rectangle with linear gradient fill">
  <defs>
    <linearGradient id="lg1" x1="0%" y1="0%" x2="100%" y2="0%">
      <stop offset="0%" stop-color="#2563eb" />
      <stop offset="100%" stop-color="#facc15" />
    </linearGradient>
  </defs>
  <rect x="20" y="25" width="200" height="70" rx="10" fill="url(#lg1)" />
</svg>

Pattern: define stops → unique id → apply with url(#id).

Attributes

Core gradient pieces:

NameDescriptionTry it
idHandle for url(#…)Try it
x1 y1 x2 y2Gradient vector (default L→R)Try it
<stop> offsetPosition of a color along the vectorTry it
stop-color / stop-opacityColor and transparency of a stopTry it
gradientUnitsobjectBoundingBox (default) or userSpaceOnUseTry it
spreadMethodpad, reflect, or repeatTry it
fill / stroke url(#id)Apply the gradient to a shapeTry it

Global Attributes

The <linearGradient> tag also supports the Global Attributes in HTML (for example id, class, lang, title) when used in an HTML document.

Live Preview

Change the end of the gradient vector. Start stays at (0%, 0%).

100
0

Defaults match MDN: horizontal from 0% to 100%.

SVG

x2="100%" y2="0%"

HTML Examples

Short, copy-ready snippets. Use Try It Yourself for a live editor.

Example 1 — Basic Left-to-Right Fill

Two stops paint a rectangle horizontally.

HTML
<svg width="240" height="120" viewBox="0 0 240 120"
     xmlns="http://www.w3.org/2000/svg"
     role="img" aria-label="Left to right linear gradient">
  <defs>
    <linearGradient id="hGrad" x1="0%" y1="0%" x2="100%" y2="0%">
      <stop offset="0%" stop-color="#2563eb" />
      <stop offset="100%" stop-color="#facc15" />
    </linearGradient>
  </defs>
  <rect x="20" y="25" width="200" height="70" rx="10" fill="url(#hGrad)" />
</svg>
Try It Yourself

How It Works

  1. Define <linearGradient id="hGrad"> in <defs>.
  2. Add two <stop> colors at 0% and 100%.
  3. Keep y1/y2 at 0% for a horizontal blend.
  4. Apply with fill="url(#hGrad)".

Example 2 — Vertical Gradient

Set x2="0%" and y2="100%" for top to bottom.

HTML
<svg width="160" height="180" viewBox="0 0 160 180"
     xmlns="http://www.w3.org/2000/svg"
     role="img" aria-label="Top to bottom linear gradient">
  <defs>
    <linearGradient id="vGrad" x1="0%" y1="0%" x2="0%" y2="100%">
      <stop offset="0%" stop-color="#16a34a" />
      <stop offset="100%" stop-color="#0f172a" />
    </linearGradient>
  </defs>
  <rect x="30" y="20" width="100" height="140" rx="12" fill="url(#vGrad)" />
</svg>
Try It Yourself

How It Works

  1. Keep x1 and x2 equal so the blend is vertical.
  2. Set y2="100%" to point the vector downward.
  3. Stops still use offset along that vector.
  4. Reuse the same fill="url(#…)" pattern.

Example 3 — Multiple Stops

Three colors create a richer blend.

HTML
<svg width="240" height="100" viewBox="0 0 240 100"
     xmlns="http://www.w3.org/2000/svg"
     role="img" aria-label="Three-stop linear gradient">
  <defs>
    <linearGradient id="tri" x1="0%" y1="0%" x2="100%" y2="0%">
      <stop offset="0%" stop-color="#2563eb" />
      <stop offset="50%" stop-color="#a855f7" />
      <stop offset="100%" stop-color="#ea580c" />
    </linearGradient>
  </defs>
  <rect x="20" y="25" width="200" height="50" rx="8" fill="url(#tri)" />
</svg>
Try It Yourself

How It Works

  1. Add as many <stop> as you need.
  2. Order by offset from 0% to 100%.
  3. Mid stops control the transition colors.
  4. Optional stop-opacity fades a stop.

Example 4 — Gradient on Stroke

Paint a thick outline instead of a fill.

HTML
<svg width="200" height="160" viewBox="0 0 200 160"
     xmlns="http://www.w3.org/2000/svg"
     role="img" aria-label="Circle stroked with a linear gradient">
  <defs>
    <linearGradient id="ring" x1="0%" y1="0%" x2="100%" y2="0%">
      <stop offset="0%" stop-color="#2563eb" />
      <stop offset="100%" stop-color="#16a34a" />
    </linearGradient>
  </defs>
  <circle cx="100" cy="80" r="45"
          fill="none" stroke="url(#ring)" stroke-width="14" />
</svg>
Try It Yourself

How It Works

  1. Define the gradient as usual.
  2. Set fill="none" so only the outline shows.
  3. Use stroke="url(#ring)" instead of fill.
  4. Increase stroke-width so the blend is easy to see.

Example 5 — Diagonal Gradient

Point both x2 and y2 to 100% for a corner-to-corner blend.

HTML
<svg width="220" height="140" viewBox="0 0 220 140"
     xmlns="http://www.w3.org/2000/svg"
     role="img" aria-label="Diagonal linear gradient rectangle">
  <defs>
    <linearGradient id="diag" x1="0%" y1="0%" x2="100%" y2="100%">
      <stop offset="0%" stop-color="#7c3aed" />
      <stop offset="100%" stop-color="#facc15" />
    </linearGradient>
  </defs>
  <rect x="20" y="20" width="180" height="100" rx="10" fill="url(#diag)" />
</svg>
Try It Yourself

How It Works

  1. Start at top-left with 0% 0%.
  2. End at bottom-right with 100% 100%.
  3. Stops blend along that diagonal vector.
  4. Optional: gradientTransform="rotate(...)" can tilt further.

Edge Cases & Pitfalls

Most broken gradients are ids, missing url(), or unit surprises.

id

url(#id) must match

Typos or duplicate ids mean a solid black/default fill instead of your blend.

units

objectBoundingBox is default

Percent vectors scale with the shape. Use userSpaceOnUse when you need absolute coordinates.

stops

Need at least two stops

One stop alone cannot blend. Add distinct offsets and colors.

stroke

Thin strokes hide blends

When using stroke="url(#id)", raise stroke-width so the gradient band is visible.

Browser Support

SVG <linearGradient> is supported in every modern browser.

✓ Baseline · Widely available

SVG <linearGradient>

Widely available since about July 2015. Chrome, Firefox, Safari, Edge, and Opera support linearGradient, stop, and url(#id) paint servers. Internet Explorer 9+ supports basic SVG gradients.

98% Global support (approx.)
Google Chrome All modern
Full support
Mozilla Firefox All modern
Full support
Apple Safari All modern
Full support
Microsoft Edge All modern
Full support
Opera All modern
Full support
Samsung Internet All modern
Full support
Internet Explorer 9+
Supported
<linearGradient> Widely supported

Bottom line: Safe for production icons, charts, and UI fills. Prefer local defs with unique ids over deprecated xlink:href reuse.

Key Takeaways

  • Define in <defs> with an id.
  • Direction: x1, y1, x2, y2.
  • Colors: <stop offset stop-color>.
  • Apply: fill or stroke url(#id).

One line: stops in <linearGradient>, paint with url(#id).

Default CSS Settings

Gradients are paint servers — they have no visible box of their own. Useful defaults to remember:

defaults.css
/* Conceptual defaults */
linearGradient {
  /* x1=0% y1=0% x2=100% y2=0%  → left to right */
  /* gradientUnits: objectBoundingBox */
  /* spreadMethod: pad */
}

/* Apply from CSS if needed */
.shape {
  fill: url(#hGrad);
}

Prefer presentation attributes in these lessons so the paint stays inside the SVG markup.

Frequently Asked Questions

Define a <linearGradient> in <defs> with an id and <stop> elements, then use fill="url(#id)" or stroke="url(#id)" on a shape.
They set the gradient vector. Defaults are left-to-right: x1="0%" y1="0%" x2="100%" y2="0%".
offset places that color along the vector, usually as a percentage such as 0%, 50%, or 100%.
Yes. Use stroke="url(#id)" and set stroke-width so the stroke band is visible.
Default gradientUnits is objectBoundingBox, so the gradient scales with the shape. Use userSpaceOnUse for fixed coordinates.
Use linear for directional blends. Use radial when color should radiate from a center point.

Next: SVG Radial Gradients

Learn cx, cy, and r for center-out color blends.

SVG Radial Gradients →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful