The last couple of weeks I’ve been walking through some of the basics of scalable vector graphics. First I showed how to include SVG in HTML and then I showed how to create the basic shapes SVG provides.
In both posts I used the fill and stroke properties in order to see the what we were creating. Today I want to talk in more detail about fills and strokes and the properties associated with each.
Fill Properties
As you probably expect, the fill property takes a color and fills the interior of the shape (or line) you created with that color. Here it is added to a rectangle and circle
[code type=html]
[/code]
You could also set the values of fill in your CSS. Below I set the fill on different classes, but you could set it on the SVG property directly too if you prefer.
[code type=css]
.rectangle {fill: red;}
.circle {fill: #039;}
[/code]
By default fill will be black or #000000 so if you don’t want a shape to be filled with color you need to explicitly set it to none.
[code type=html]
[/code]
What’s considered inside the shape (or line) depends on the fill-rule property which can have the values nonzero or evenodd.
[code type=html]
fill-rule=”nonzero”
fill-rule=”evenodd”
[/code]
I’m not even going to pretend I completely follow how the two algorithms work. Here are the definitions from the spec. Below each is an example image of each algorithm, also from the spec.
nonzero—This rule determines the “insideness” of a point on the canvas by drawing a ray from that point to infinity in any direction and then examining the places where a segment of the shape crosses the ray. Starting with a count of zero, add one each time a path segment crosses the ray from left to right and subtract one each time a path segment crosses the ray from right to left. After counting the crossings, if the result is zero then the point is outside the path. Otherwise, it is inside.
evenodd—This rule determines the “insideness” of a point on the canvas by drawing a ray from that point to infinity in any direction and counting the number of path segments from the given shape that the ray crosses. If this number is odd, the point is inside; if even, the point is outside.
What I find confusing is the last example in each of the SVGs. I’m not following why they look the same, when the second examples look different. If anyone knows, I’d appreciate knowing too.
You can also set the fill-opacity of any fill.
[code type=html]
fill-opacity=”0.5″
[/code]
It takes a value between 0 (transparent) and 1 (opaque) and works just like the CSS opacity property and I’m guessing no further explanation is necessary.
Stroke Properties
You use fill properties to add color inside the element. Stroke properties add color around the element. They define the outline or contour of an element.
There are a few more stroke properties than fill properties, and quite honestly I find them a bit more interesting. If you read the previous posts in this series, you will have seen two already, stroke and stroke-width. The first takes a color as a value and the second takes a unit of measurement as a value.
[code type=html]
stroke=”blue”
stroke=”#347559
stroke-width=”3px”
stroke-width=”1em”
stroke-width=”2%”
[/code]
When a percentage is used for the stroke-width value, it’s a percentage of the current viewport.
[code type=html]
[/code]
There’s also a stroke-opacity property, which I again hope is self-explanatory.
[code type=html]
stroke-opacity=”0.25″
[/code]
It doesn’t end there. The stroke-linecap
property gives you some control over the shape at the ends of lines. Your choices are butt, square, and round. You can see an example of each in the image below.
The stroke-linejoin
property is similar but controls the joints between line segments. You might set it when working with a polyline. It also takes three values, miter, round, and bevel, which you can see in the image below.
If you set miter as the linejoin value and it the lines meet at a sharp angle, it’s possible for the miter to extend beyond the thickness of the stroke.
The stroke-miterlimit
property sets a limit on the ratio between miter-length and stroke-width. It expects a number greater than or equal to 1. When the limit is exceeded the stroke is converted to a bevel.
Dashed lines are possible with the stroke-dasharray
property. It allows you to set both the length of each dash and the space between dashes.
[code type=html]
[/code]
The first value is the length of the dash and the second value is the space between each dash. If you only supply a single value (the last example above), it will be used twice resulting in equal dash length and space between dashes.
The stroke-dasharray can do more. Even though I’ve shown it taking a single set of values, it can accept a comma separated list of value pairs.
[code type=html]
[/code]
The dash will follow the pattern set by the different pairs of line length and space and then it will repeat until it runs out room to display. In the example above, I set the last space to 30 to make it easier to see the pattern repeating.
One last property is stroke-dashoffset
, which allows you set the distance into the dash pattern to start the dash. Acceptable values are any unit of measurement and again if a percent is used, it’s a percent of the viewport.
[code type=html]
[/code]
Read the previous paragraph again. The stroke-dashoffset property works opposite to my initial expectation. My instinct was to think setting the offset would delay the start of the pattern by the amount set in the offset. What happens is the reverse. The offset is how far into the pattern to start it.
Here’s an example of the same pattern with and without an offset applied. Hopefully the visually makes it clear what’s going on.
Shapes and Lines
At the start of this post I mentioned you could fill both a shape and a line, which may have sounded a little strange. How would you fill a line, after all? It depends on the line.
Other than the possibly confusing nonzero and evenodd algorithms, you probably don’t need my help to understand what gets filled and what gets stroked on a basic shape like a rectangle or circle. The stroke is the shape’s outline and the fill is everything inside that outline.
What about lines, though? Most lines won’t take any fill. What you see is the line’s stroke. There’s nothing inside to fill. However, when the line is a polyline if can be filled. The polyline be treated as though it were a polygon with the exact same points for each line segment.
For example both the polyline and polygon below will look exactly the same.
[code type=html]
[/code]
If you add a stroke, however, you can see the polygon completes the shape with a line directly from end point to start point, where the polyline doesn’t show the line. The fills are the same though.
[code type=html]
[/code]
SVG Properties and CSS
For most of the examples in this series. I’ve been setting SVG properties as attributes on different SVG elements. Earlier in this post I showed how you could set values on fill in your CSS file. You can set more SVG properties in CSS. Not all, but you can set both fills and strokes in CSS.
You can add the CSS inline as below.
[code type=html]
[/code]
You can also set styles directly in your CSS file.
[code type=html]
[/code]
[code type=css]
.example {
fill: teal;
stroke: red;
stroke-width: 5px
}
[/code]
Just remember to set fill as opposed to background color and set stroke and stroke-width, instead of border-color and border-width.
Plenty of other SVG properties can be written as CSS properties instead of having to add them as attributes on an SVG element. The W3C maintains a table that lists all the different SVG properties that can be styled in CSS.
Closing Thoughts
As I hope you can see, adding fills and strokes to basic shapes and lines is rather easy. If you’ve worked with background-colors and borders in CSS, much of what’s here probably seemed like review.
I find the fill-rule algorithms a bit confusing, but it will always be easy enough to test the two possibilities and see which one gives you what you want.
The opacity of both fill and stroke can be set and stroke also provides properties for the shape of the line at the end points or where line segments join. You can also turn solid strokes into dashed strokes.
You now have enough information to create simple SVGs. You know how to create, fill, and stroke simple shapes like rectangles and circles or build up more complex shapes with polygons. You can do similar with simple and complex lines.
Still it’s all rather basic. There’s a lot more you can do with SVG than fill circles and rectangles with with color. For example, instead of a solid fill you can fill shapes with gradients or patterns made up of other SVG elements.
Before we get there though, I want to look at something I mentioned when talking about shapes. I mentioned paths as another way to create them. The next couple of weeks I’ll look at how to create lines, curves, and shapes, using paths.
Download a free sample from my book, Design Fundamentals.
Hi,
This seems really unintuitive, if I create a stroke with alpha, then the fill is visible underneath half of it!
Instead of stroke colour, a fill colour, I have those + a 3rd colour where the stroke is over the fill!
Example
https://www.svgshare.com/i/VPP.svg
Thank you for creating this article!
Some of the HTML code blocks are not showing the code but instead get rendered so the SVG’s appear twice.
Can those be fixed so the examples are clear?
Thank you!