Chapter 16: SVG Fill

SVG Fill — one of the most important things when you’re coloring shapes in SVG!

Imagine we’re sitting together with the code editor open, and I’m your patient teacher who’s going to explain everything step by step, like we’re building colorful drawings from scratch. We’ll go slow, with many copy-paste examples.

What does “fill” actually mean in SVG?

fill = the color (or pattern/gradient) that paints the inside of a shape.

  • Applies to: closed shapes like <rect>, <circle>, <ellipse>, <polygon>, <path> (when closed with Z), and also to text (<text>, <tspan>, <textPath>)
  • Does not apply to: open paths like <line>, <polyline> (they have no “inside” — only stroke)
  • Default value: black (#000000)

You set it in two main ways:

  1. As an attribute directly on the element (most common when learning):

    HTML
  2. As a CSS property (very powerful for styling many elements at once):

    CSS
    HTML

Basic fill values (colors)

SVG accepts almost all the same color formats as CSS:

Type Example value Code snippet
Named color red, lime, teal fill=”tomato”
Hex #f44336, #4CAF50 fill=”#673AB7″
RGB rgb(255, 99, 71) fill=”rgb(100, 200, 50)”
RGBA (with opacity) rgba(255, 0, 0, 0.6) fill=”rgba(0, 128, 255, 0.7)”
HSL hsl(120, 100%, 50%) fill=”hsl(45, 100%, 50%)”
currentColor currentColor fill=”currentColor” (inherits from CSS color)

Super useful trick — currentColor This makes SVG icons change color automatically when you style the parent element.

HTML

Hover → color changes magically!

Fill opacity (separate control)

You can control transparency of only the fill (without affecting stroke) using:

  • fill-opacity=”0.5″ (attribute)
  • fill-opacity: 0.7; (CSS)

Example:

HTML

Advanced fills: Gradients & Patterns

The real power starts here — fill can reference paint servers (special definitions in <defs>).

  1. Linear Gradient (straight color transition)
HTML
  1. Radial Gradient (circular/spreading from center)
HTML
  1. Pattern (repeating tiles — like dots, stripes, checkerboard)
HTML

fill-rule — The “which parts are inside?” rule (very important!)

For complex paths (especially ones that cross themselves or have holes), fill-rule decides what counts as “inside”.

Two main values:

Value How it works Best for Default?
nonzero Counts direction of path crossings (clockwise/counterclockwise matters) Most simple shapes, solid icons Yes
evenodd Counts crossings — odd = inside, even = outside (ignores direction) Holes, stars, overlapping shapes No

Classic example — star with hole:

HTML

With evenodd → center hole stays empty. With nonzero → center fills (depending on path direction).

Here are visual comparisons of fill-rule behavior (imagine seeing these side by side):

  • Left: star with evenodd (hole visible)
  • Right: same path with nonzero (center filled)

Quick Cheat Sheet

Goal Use this…
Solid color fill=”purple” or fill=”#673AB7″
Semi-transparent fill fill-opacity=”0.6″
Color changes with parent fill=”currentColor”
Rainbow/striped effect <linearGradient> or <pattern>
Shape with hole / cut-out fill-rule=”evenodd”
No fill (only stroke) fill=”none”

Common beginner mistakes

  • Forget fill → defaults to black (not transparent!)
  • Set fill=”none” but want background → use <rect> behind or CSS background
  • Use fill on <line> or <polyline> → does nothing (they’re open)
  • Gradient/pattern not showing → forgot id or wrong url(#id)
  • Complex path fills wrong → try switching fill-rule

Mini practice for you

  1. Make a rectangle with a horizontal rainbow gradient
  2. Create a circle with radial gradient (white center → dark edge)
  3. Draw a star <polygon> or <path> and use evenodd vs nonzero to see the difference

Paste your code here if you want feedback — I’ll check it like we’re debugging together 😊

Any part still confusing? Gradients? Patterns? currentColor? fill-rule weirdness? Just tell me — we’ll zoom in with more examples until it’s crystal clear! 🚀

You may also like...

Leave a Reply

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