Chapter 23: SVG Linear Gradients

SVG Linear Gradients are one of the most beautiful and commonly used ways to fill shapes, text, paths, strokes — anything that accepts a fill or stroke — with a smooth color transition that goes in a straight line from one point to another.

Instead of one solid color, you get a smooth fade from color A → color B → color C, like a sunrise, a metallic shine, a rainbow stripe, or a modern button background.

1. Why Use Linear Gradients in SVG?

  • They look professional and modern (used in almost every UI design in 2026)
  • Completely vector — zoom forever, still perfect (no pixelation)
  • You can apply them to fill, stroke, even text
  • Reusable — define once in <defs>, use on many elements
  • Animatable — change colors, angle, position with CSS or SMIL
  • Very lightweight compared to images

2. Basic Structure – The Two Main Tags

You need two elements working together:

  1. <linearGradient> → defines the gradient itself (lives inside <defs>)
  2. fill=”url(#myGrad)” or stroke=”url(#myGrad)” → applies it to any shape

Basic skeleton:

XML

3. Key Attributes of <linearGradient>

Attribute What it means Default Most common values Notes
id Unique name to reference later “grad1”, “sunrise” Must have!
x1 Starting X position (0%–100% or absolute) 0% 0%, 50%, 20% Left edge of gradient line
y1 Starting Y position 0% 0%, 100%, 30% Top edge
x2 Ending X position 100% 100%, 0%, 80% Right edge
y2 Ending Y position 0% 0%, 100%, 70% Bottom edge
gradientUnits Coordinate system objectBoundingBox objectBoundingBox, userSpaceOnUse objectBoundingBox = relative to shape size (most used)
spreadMethod What happens outside gradient line pad pad, reflect, repeat pad = solid color at ends

4. Your First Linear Gradient – Horizontal Fade (Copy-Paste Now!)

HTML

Result: Rectangle filled with smooth left-to-right color fade: red → orange-yellow → blue

5. Important Variations Everyone Should Know

A. Vertical Gradient (Top to Bottom)

XML

B. Diagonal Gradient (Top-left to Bottom-right)

XML

C. Gradient on Stroke (Border)

XML

→ Rainbow border!

D. More Stops (Multi-color Smooth Transition)

XML

E. Using Absolute Units (Not Percentage)

XML

→ Gradient line is fixed in SVG coordinates (not relative to shape size)

6. Teacher’s Quick Tips (Hyderabad Student Style 😄)

  • Always put <linearGradient> inside <defs>
  • Use percentages (0% to 100%) for most cases — easiest and responsive
  • Add stop-opacity=”0.7″ on any <stop> if you want transparent areas
  • spreadMethod=”reflect” → gradient bounces back and forth (mirror effect)
  • spreadMethod=”repeat” → repeats the gradient like stripes
  • Common mistake: Forget id or wrong #id in url() → gradient invisible!
  • Pro trick: Animate <stop> offset or color with CSS → moving gradient effect

Understood SVG Linear Gradients fully now? This is one of the most beautiful and most-used features in modern web design.

Tell me what you want next:

  • Animated linear gradient (moving shine)?
  • Gradient on text or stroke only?
  • Diagonal + multi-stop example project?
  • Compare with radialGradient?
  • Or full metallic button with linear + radial combo?

Just say — we can build any gradient style together right now! 🚀

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *