Lesson 12
Objective: Learn the different methods to apply styles to SVG elements
So far in these lessons we have used SVG attributes and values for applying styles to a shape. You can use other methods for faster and easier styling.
The styling methods are:
Let's look at these one by one using a simple circle as an example.
This is what we have been working with so far. It is verbose and typing intensive, but we stuck with that while you were learning SVG basics. Now we can look at some short cuts.
<circle cx="100" cy="100" r="50" stroke="red" stroke-width="5" fill="red" fill-opacity="0.5" />
You can use one style="" attribute and put all the presentation attributes and values in using CSS colon and semi-colon syntax as shown here.
<circle cx="300" cy="100" r="50" style="fill:blue; stroke:black; stroke-width:5; fill-opacity:0.5;" />
This simplifies styling a lot and keeps all the styles organized in a single attribute.
You can define CSS in the SVG and then apply styles using the CSS class attribute. This is useful if a drawing has a number of shapes that use the same styling. It can reduce the file size considerably if styles are not repeated on each element.
To do this you have to tell the SVG that the CSS is separate from the SVG text. You need to insert the style element containing CDATA after the opening SVG element.
<style type="text/css" > <![CDATA[ /*CSS Goes Here*/ ]]> </style>
So our circle SVG with the styles applied to the element will look like this:
<style type="text/css" > <![CDATA[ .red-circle { fill: blue; stroke: black; stroke-width: 5; fill-opacity: 0.5; } ]]> </style> <circle class="red-circle" cx="100" cy="300" r="50" />
When you have a lot of SVG in a book or training manual that uses the same styles it is probably useful to use standardized styling in the external CSS file loaded by the HTML file.
IGP:Digital Publisher has a number of default styles built in that can be used immediately in any SVG. This means you only need a class statement and put in the CSS properties and everything is ready to go. Alternatively you can create a custom project CSS which will be even more terse.
<circle class="l5 s-red f-red fo-5" cx="300" cy="300" r="50" />
You can immediately see the file size benefit of this approach. The downside is that the CSS has to be loaded separately. The advantage is it is only loaded once and can be applied to dozens of shapes in a book section.
View the default IGP:Digital Publisher SVG Style List.
Here we have four red circles, each with styles applied using each of the methods above. The outcome is identical.
There is one more very powerful property with SVG styling. If a style has been defined in an earlier SVG it can be used again in any following SVG drawings. This circle uses the CSS style that was embedded in the previous SVG image.
Have a look at it and understand how this technique can allow you to create really small, fast-loading SVG. Put all your styles in the first SVG illustration in a book section and all the following images can inherit the styles.
Here are our standard SVG Cheatsheets you can download, print out and have ready when needed. There is also a text file containing annotated quickstarts for all major shapes and techniques.
SVG Editor. Access a full SVG Editor online at any time.