<linearGradient>
Core element
Define reusable paint inside <defs> with a unique id.

SVG <linearGradient> blends colours along a straight vector. This tutorial covers defining gradients in <defs>, <stop> offsets, direction with x1/y1/x2/y2, applying paint with url(#id), five worked examples, and how linear gradients compare to radial ones and patterns.
Core element
Define reusable paint inside <defs> with a unique id.
Colour stops
Set offset, stop-color, and optional stop-opacity.
x1 y1 → x2 y2
The gradient vector controls left-right, top-bottom, or diagonal blends.
Apply paint
Use fill or stroke with url(#yourId) on any shape.
Coordinate space
objectBoundingBox (default) or userSpaceOnUse for fixed coords.
Outlines & rings
Paint strokes with the same gradient for badges and UI rings.
SVG linear gradients blend two or more colours along a straight line. They add depth and polish to icons, buttons, charts, and illustrations — all while staying crisp as vectors.
You define a <linearGradient> once (usually in <defs>), list colour <stop>s, then reference it with url(#id) on fill or stroke.
Raster gradient images blur when scaled. SVG gradients stay sharp, stay tiny in markup, and can be shared across many shapes with one definition.
One gradient id can fill or stroke many shapes.
x1/y1 to x2/y2 set left-right, top-bottom, or diagonal.
Two stops for simple blends; add more for multi-colour ramps.
Same paint works for solid fills and outlined rings.
In short: define stops along a direction vector, then paint shapes with url(#id).
Basic form of an SVG linear gradient:
<svg width="300" height="120">
<defs>
<linearGradient id="g" x1="0%" y1="0%" x2="100%" y2="0%">
<stop offset="0%" stop-color="#f97316" />
<stop offset="100%" stop-color="#3b82f6" />
</linearGradient>
</defs>
<rect x="20" y="30" width="260" height="60" rx="12" fill="url(#g)" />
</svg> | Name | Type | Description |
|---|---|---|
<linearGradient> | Element | Defines a linear colour blend; place inside <defs> with an id. |
x1, y1 | Length / % | Start of the gradient vector. |
x2, y2 | Length / % | End of the gradient vector (direction). |
<stop> | Element | A colour at a position along the vector. |
offset | % / number | Stop position, e.g. 0%, 50%, 100%. |
stop-color | Colour | Colour at that stop. |
stop-opacity | 0–1 | Optional transparency of the stop. |
gradientUnits | Keyword | objectBoundingBox (default) or userSpaceOnUse. |
| Effect | Vector | Notes |
|---|---|---|
| Left → Right | x1="0%" y1="0%" x2="100%" y2="0%" | Most common UI bar fill |
| Top → Bottom | x1="0%" y1="0%" x2="0%" y2="100%" | Vertical blends and fades |
| Diagonal | x1="0%" y1="0%" x2="100%" y2="100%" | Corner-to-corner polish |
<!-- 1. Define -->
<defs>
<linearGradient id="btnGrad" x1="0%" y1="0%" x2="100%" y2="0%">
<stop offset="0%" stop-color="#f97316" />
<stop offset="100%" stop-color="#3b82f6" />
</linearGradient>
</defs>
<!-- 2. Apply -->
<rect fill="url(#btnGrad)" /> | Goal | Code |
|---|---|
| Define gradient | <linearGradient id="g" x1="0%" y1="0%" x2="100%" y2="0%">...</linearGradient> |
| Two colour stops | <stop offset="0%" stop-color="red" /> + 100% stop |
| Fill a shape | fill="url(#g)" |
| Stroke a shape | stroke="url(#g)" stroke-width="8" |
| Fade out | stop-opacity="0" on the end stop |
| Fixed coordinates | gradientUnits="userSpaceOnUse" |
All paint SVG shapes — pick the paint that matches the look you want.
straight blendColours along a vector — UI bars & buttons
centre-outHighlights, glows, and orbs
repeating tileTextures, grids, and motifs
one colourSimplest fill when no blend is needed
Reach for <linearGradient> when colour should travel in a clear direction.
Primary CTAs, progress bars, and pill fills with subtle depth.
Brand accents and outlined rings with gradient strokes.
Series fills, heat scales, and axis emphasis.
Hero shapes and card headers that stay resolution-independent.
For centre-out highlights, use radial gradients instead.
Key benefit: one definition, many shapes — crisp colour blends at every scale.
Five starter snippets using <linearGradient>. Click View Output for a preview, or Try It Yourself to edit live.
Define a gradient, fill a shape, then change the direction.
A classic left-to-right fill using two colour stops on a rounded rectangle.
<svg width="300" height="200" viewBox="0 0 300 200">
<defs>
<linearGradient id="gradientID" x1="0%" y1="0%" x2="100%" y2="0%">
<stop offset="0%" stop-color="red" />
<stop offset="100%" stop-color="blue" />
</linearGradient>
</defs>
<rect x="20" y="20" width="260" height="160" rx="18" fill="url(#gradientID)" />
</svg> The gradient lives in <defs>. fill="url(#gradientID)" paints the rectangle with that reusable paint.
Change only x1/y1/x2/y2 to rotate the blend direction.
<svg width="320" height="200" viewBox="0 0 320 200">
<defs>
<linearGradient id="lr" x1="0%" y1="0%" x2="100%" y2="0%">
<stop offset="0%" stop-color="#ef4444" />
<stop offset="100%" stop-color="#3b82f6" />
</linearGradient>
<linearGradient id="tb" x1="0%" y1="0%" x2="0%" y2="100%">
<stop offset="0%" stop-color="#22c55e" />
<stop offset="100%" stop-color="#0ea5e9" />
</linearGradient>
<linearGradient id="diag" x1="0%" y1="0%" x2="100%" y2="100%">
<stop offset="0%" stop-color="#a855f7" />
<stop offset="100%" stop-color="#f97316" />
</linearGradient>
</defs>
<rect x="10" y="20" width="90" height="160" rx="12" fill="url(#lr)" />
<rect x="115" y="20" width="90" height="160" rx="12" fill="url(#tb)" />
<rect x="220" y="20" width="90" height="160" rx="12" fill="url(#diag)" />
</svg> Think of x1,y1 → x2,y2 as an arrow. Horizontal, vertical, and diagonal arrows produce the three common UI directions.
Multi-stop ramps, gradient strokes, and soft fades.
Add a middle stop for richer blends — great for brand and chart scales.
<svg width="300" height="100" viewBox="0 0 300 100">
<defs>
<linearGradient id="ramp" x1="0%" y1="0%" x2="100%" y2="0%">
<stop offset="0%" stop-color="#22c55e" />
<stop offset="50%" stop-color="#0ea5e9" />
<stop offset="100%" stop-color="#a855f7" />
</linearGradient>
</defs>
<rect x="20" y="25" width="260" height="50" rx="14" fill="url(#ramp)" />
</svg> Each <stop> places a colour at an offset. Two stops are enough for most UI; three or more create richer ramps.
Apply the same linear gradient to a circle’s stroke for badge and ring UI.
<svg width="240" height="160" viewBox="0 0 240 160">
<defs>
<linearGradient id="strokeGrad" x1="0%" y1="0%" x2="100%" y2="0%">
<stop offset="0%" stop-color="#22c55e" />
<stop offset="50%" stop-color="#0ea5e9" />
<stop offset="100%" stop-color="#a855f7" />
</linearGradient>
</defs>
<circle cx="120" cy="80" r="48" fill="none"
stroke="url(#strokeGrad)" stroke-width="10" />
</svg> stroke="url(#strokeGrad)" paints the outline. Use fill="none" when you only want the ring.
Fade a fill into transparency for soft edges and overlay effects.
<svg width="300" height="140" viewBox="0 0 300 140">
<defs>
<linearGradient id="fade" x1="0%" y1="0%" x2="100%" y2="0%">
<stop offset="0%" stop-color="#2563eb" stop-opacity="1" />
<stop offset="100%" stop-color="#2563eb" stop-opacity="0" />
</linearGradient>
</defs>
<rect x="0" y="0" width="300" height="140" fill="#e2e8f0" />
<rect x="20" y="30" width="260" height="80" rx="12" fill="url(#fade)" />
</svg> Same stop-color with falling stop-opacity creates a soft dissolve — useful for vignettes and overlays.
Real-world places where SVG linear gradients show up every day.
Subtle fills that feel more dimensional than flat colour.
Example: primary CTA with brand-to-accent blend.
Track fills that read as energy or completion.
Example: green-to-cyan upload meter.
Gradient strokes on circles for status and awards.
Example: multi-colour outline around an avatar.
Series fills and heat-style colour scales.
Example: area chart fill fading toward the baseline.
Consistent directional accents across an icon set.
Example: shared diagonal gradient on logo marks.
Panel tops and hero shapes without exporting PNGs.
Example: soft left-to-right header strip.
Pro Tip: keep gradient ids unique per inline SVG on a page — duplicate ids can make the wrong paint apply.
Why paint with SVG gradients instead of raster images or CSS alone.
Stays sharp on retina screens without extra assets.
One definition paints many shapes via url(#id).
Exact vectors for horizontal, vertical, or diagonal blends.
Outline rings and badges with the same paint system.
Swap stop colours or animate offsets without new image files.
Pro Tip: for UI, two close tones often look more premium than a rainbow of stops.
Follow these practices for clean, scalable SVG gradients.
Put gradients in <defs> (or earlier in the SVG) before url(#id).
Names like btnGrad or strokeGrad are easier to maintain.
Extra stops are fine for charts; keep UI fills restrained.
Match gradient vectors across an icon set for a cohesive look.
Default objectBoundingBox scales with the shape; use userSpaceOnUse for fixed coords.
Pro Tip: percentage vectors (0%–100%) are easiest when you stay on the default bounding-box units.
Avoid these mistakes when your gradient refuses to show up.
fill="url(#missing)" paints nothing useful.
→ Match the id on <linearGradient> exactly.
Multiple SVGs on one page sharing the same id can collide.
→ Give each gradient a unique id per document.
If start and end points are identical, the blend collapses.
→ Ensure x1,y1 and x2,y2 differ.
Percentages behave differently under userSpaceOnUse.
→ Stick to default units until you need fixed coordinates.
SVG uses <stop> elements, not CSS linear-gradient() strings inside attributes.
→ Define SVG gradients with elements, then reference via url(#id).
Pro Tip: if a fill is blank, check the id spelling, that stops exist, and that the gradient is defined in the same SVG document.
Gradients should live in <defs> so they can be reused across multiple shapes.
Each <stop> specifies a colour at a position using offset. Two stops are enough for most gradients.
Think of the values as an arrow. Left-to-right is 0% 0% → 100% 0%. Top-to-bottom is 0% 0% → 0% 100%.
Use fill="url(#id)" to fill shapes or stroke="url(#id)" to paint outlines.
Linear gradients make SVG graphics feel more modern and dimensional while staying crisp at any scale.
<defs> and reference them with url(#id).x1/y1/x2/y2 set the blend direction.offset + stop-color (and optional stop-opacity) build the ramp.fill and stroke.objectBoundingBox — gradients scale with the shape.Quick Takeaway: define stops along a vector, give the gradient an id, paint with url(#id). Keep ids unique on the page.
SVG <linearGradient> is supported in all modern browsers — and has been for many years — as part of SVG 1.1 paint servers.
Use linear gradients in inline SVG or external .svg files. Stops, direction vectors, and url(#id) paints work consistently across Chrome, Firefox, Safari, Edge, and mobile browsers.
Bottom line: Safe for production sites. Prefer inline SVG or cached .svg assets; no polyfill is required for current browsers.
SVG linear gradients are reusable paint: define <linearGradient> with colour stops, set the direction vector, then apply with fill or stroke via url(#id).
Practice the five examples above, then continue to SVG Radial Gradients when colour should radiate from a centre point.
Keep ids unique, prefer 2–3 stops for UI, and use consistent directions across an icon set.
gradientUnits="userSpaceOnUse" when you need fixed coordinatesstroke for elegant outlines and ringsbtnGrad, strokeGrad)fill/strokePaint shapes with directional colour blends.
Reusable paint in defs
APIoffset + stop-color
Colourx1,y1 → x2,y2
Vectorfill or stroke
Applybbox vs user space
CoordsAn SVG <linearGradient> is reusable paint — define it once in <defs> with an id, then apply it with fill="url(#id)" or stroke="url(#id)". You can also animate stop-color or the gradient vector with CSS or SMIL for living brand accents.
Learn centre-out colour blends for highlights, glows, and orbs.
8 people found this page helpful