Showcase and discover digital art at yex

Follow Design Stacks

Subscribe to our free newsletter to get all our latest tutorials and articles delivered directly to your inbox!

SVG Basics – Coloring Objects in SVG

Coloring Objects in SVG

All SVG shapes have two attributes named ‘stroke’ and ‘fill’ which are used to paint them. The ‘stroke’ or border attribute determines the paint of the outside edge of the object while the ‘fill’ value determines the inner paint. These attributes need a paint value. A paint value includes colors and two “non-color” values you can use.

Nothing is shown. Painting with invisible ink!
Results in the ‘color’ attribute being used to define the color. Included for compatibility with other non-SVG CSS documents (like HTML CSS). Some color requiring attributes include this.
List 1. Paint Values. Non-color values that are included in paint. (%paint)
SVG defines everything from ‘aliceblue’ to ‘yellowgreen’.
A functional way to define the color. Either values in the range from 0 to 255 or percentages can be used. Mixing percents and values is illegal.
This is the same way colors are defined in HTML.
A reference to a color gradient or pattern. Described later…
List 2. Color Values. The different ways to define a color (%color and %paint)

The difference between a paint and color in SVG is simply that paints can use the non-color values. Attributes needing a color can only use a value in ‘List 2’. In general, attributes needing a color end in ‘-color’.

Opacity effects the blending of colors being drawn to the screen and colors already there. Opacity can be applied to a group or an individual graphic element. Opacity is defined as a value from 0 to 1 or a percentage. A value of 1 or greater is opaque and no color shows from below. A value of 0 or less results in nothing being drawn. It is generally inherited and defaults to 1.

Applies to…
The overal opacity of the element.
Everything but gradient stops
The opacity of the fill paint.
Elements with the attribute ‘fill’
The opacity of the stroke paint.
Elements with the attribute ‘stroke’
The opacity of the gradient stop.
Gradient stops
Attributes which effect opacity

A shape with ‘fill-opacity=”50%”‘ and ‘opacity=”50%”‘ is filled with color at 25% opacity.

If you need greater control of the opacity then use a gradient or a mask. Gradients can give you smooth transitions between opaque and transparent sections but they only apply to things you can stroke or fill. Masks give the greatest control as you can arbitrarily change the opacity of points on groups and individual elements.