Due to some circumstances, this blog is now up for sale, for more enquires contact: Plushista@gmail.com
Pin It

Styling SVG (Scalable Vector Graphic) with CSS

by Durodola Ridwan | Monday, October 13, 2014 | 51 Comments

Guess most of us are familiar with SVG (Scalable Vector Graphic), but for the novice. It's basically what you work with in Adobe Illustrator. SVG images and their behaviors are defined in XML text files, which means that they can be searched, indexed, scripted, and compressed.

SVG allows three types of graphic objects: vector graphics, raster graphics, and text. And being an XML file, it can be created and edited with any text editor.

SVG working with CSS

Styling SVG work the same way as in regular HTML elements, where HTML cares for content and structure, CSS cares for the looks. In SVG, text and shapes were handled by the markup and the looks can be controlled using presentation attributes and CSS.

There are two possible stylesheet languages for customizing SVG: XSL and CSS. The usage of CSS is very similar to how it is used in HTML.

In SVG, closed polygons, circles, ellipses, rectangles and round-cornered rectangles can be drawn. Also, straight-line paths and paths made up of a series of connected straight-line segments (polylines) are standard. Here's a list of all SVG elements.

Below is a code that produces a rectangular shape:

<rect x="10" y="10" width="150" height="150" fill="grey" stroke="black" stroke-width="2"/>

As seen in the above example, we created a rectangle with the rect element, giving it a specific width and height, then instead of using the background-color property to give it a color (as it won't work on SVG shape), we set the color with a fill attribute. Colors are specified in the same way as in CSS2, i.e. using names like black, in hexadecimal such as #ccc or #dddddd, in decimal like rgb(255,255,127), or as percentages like rgb(100%,100%,50%).

SVG shapes can be filled and/or outlined with color, gradient, or pattern. The color and strength of the rectangle’s outer frame is defined by the attributes stroke and stroke-width.

Inline styles

Instead of using attributes, you can use inline CSS styles with identical property names:

<rect x="10" y="10" width="150" height="150" style="fill: grey; stroke: black; stroke-width: 2px;"/>

You won’t be able to define positions/margins and values for width and height within the inline style attribute. We will simply stick with x and y coordinates as well as width and height attributes.

Note: An !important declaration within a presentation attribute definition is an invalid value.

Also, note that presentation attributes count as low level “author stylesheets”. Meanwhile, style definition from the author (external stylesheets, document stylesheets and inline styles) will override it. Therefore, the rectangle in the following example will be red:

<rect width="150" height="150" fill="grey" style="fill: red;"/>

Internal styles

Just as in HTML that we could work with classes and IDs on any element. In SVG, standard CSS selectors can also be used to apply styles to element types or those with specific IDs or class names, like:

<style type="text/css">
.example {
fill: grey;
stroke: black;
stroke-width: 2;
<rect x="10" y="10" width ="150" height="150" class="example"/>

From the above example, we included our CSS stylesheets within a CDATA construct. This is very important, as CSS style sheets can include characters, such as >, which conflict with XML parsers. Even if your style sheets doesn't include any character, it's still recommended that you make use of the CDATA blocks.

External stylesheets

You might want to separate your stylesheets so they can be easier to maintain or reused elsewhere. Referencing external stylesheet in SVG is similar to the way we do it in HTML. But we will be using xml-stylesheet attribute instead of the link attribute, like:

<?xml-stylesheet type="text/css" href="style.css"?>

Grouping elements

With the <g> or <svg> structural element we can group several SVG shapes to share the same styles. It's as simple as:

<g style="fill: grey; stroke: black; stroke-width: 2;">
<rect x="203" width="150" height="150"/>
<circle cx="120" cy="80" r="76"/>

Using pseudo-classes

Dynamic pseudo-classes such as :hover, :active and :focus and pseudo-classes :first-child, :visited, :link and :lang can be used in SVG – even in combination with the CSS3 property like transition and transform:

<style type="text/css">
.example {
fill: slategrey;
stroke: black;
stroke-width: 2;
transition: all 2s ease;
.example:hover {
fill: blue;
</ style >
<rect x="10" y="10" width ="150" height="150" class="example"/>

The code above will result in color change from slategrey to blue on hover. To make that work properly and for now-outdated web browsers capable of displaying SVG graphics, needed them embedded in <embed>, <object> or <iframe> elements to display them integrated as parts of an HTML webpage instead of using the standard way of integrating images with <img>.

<object type="image/svg+xml" src="example.svg"/>

Using <img> would certainly display the SVG correctly. But hover effects and transitions would be ignored.

SVG with responsive design

We can make an SVG file responsive. It's as simple as:

body {
width: 100%;
height: 100%;
overflow: hidden;
.example {
display: block;
width: 100%;
height: 100%;
margin: auto;
<object class="example" type="image/svg+xml" src="example.svg"/>

The size of the SVG file will be determined by the size of the screen viewing it. The file will appear at the center of the page and at maximum possible values. The quality remain the same regardless.

Also @media, @font-face, @import and @charset rules within stylesheets are supported.

Final words

The capabilities of styling SVG is very large. But in today post, I hope you have learn the basics? Despite the use of SVG on the web was limited by the lack of support in older versions of Internet Explorer (IE). You can enjoy its benefits while it gain more strength.

Go Social:

Subscribe For Free Updates!

*Please confirm the email sent to your inbox after clicking "Sign Up!".

51 comments : Post Yours! Read Comment Policy ▼
We have Zero Tolerance to Spam. Chessy Comments and Comments with Links will be deleted immediately upon our review.

  1. Good article.Thank you so much for your great contribution.These kind of article is improving the writing.Waiting for new stuff.
    best essay writing service

  2. Amazing reading! It is a wonderful technique of spreading knowledge for readers. Keep sharing new things
    the Walking dead jacket at amazon

  3. Male Extra™is one of the natural male enhancement pills that really work by aiding your erection size, controlling your ejaculations.Read complete user reviews here

  4. You’ve written nice post, I am gonna bookmark this page, thanks for info. I actually appreciate your own position and I will be sure to come back here.
    mortal kombat x, a10, roblox

  5. Let’s keep are safe place to play the very best free games for kids! Please click:
    slither.io | wings.io | happy wheels | abcya | fnaf 4 | happy wheels the game free | tetris to play for free!

  6. The best space for your child to relax!: wingsio | slither io | abc

  7. V Tight Gel is a natural vaginal tightening cream. It will tighten your vagina within minutes.

  8. This can help a lot in our programming spcially when we have lightbox wordpress plugin. This can be created and edited with any text editor.

  9. Great post which contains very useful information regarding Auto responder. Thanks for sharing such an excellent post. friv games online | friv online games | papa’s games | Juegos de deportes | Juegos 1

  10. Thanks for taking the time to discuss this, I feel about it and love learning more on this topic. If possible, as you gain expertise, would you mind updating your blog with more information? It is extremely helpful for me. Skandal Ngentot dengan Tetangga Toket Kecil

  11. Let’s keep are safe place to play the very best free games for kids! Please click: wings.io | slither.io | abcya

  12. bokep nikmat This is a very good post. Just wonderful. Nice one bokep terkini

  13. PhenQ is simple and clear that is why the main ingredients and the working is mentioned on each piece of this product so that the customers can get the idea of what actually will be done with their body to reduce their weight.PhenQ diet pills have been the best-selling weight loss option for over a year and its perfect for people who are not losing any weight despite being on a low-calorie diet.

  14. iPhone have a line of smartphones designed and marketed by Apple Inc.Now to use Iphone 7 without any Problem and restrictions imposed by Iphone's IOS.
    You can Download iPhone 7 Jailbreak from CydiaNerd

  15. today I am very happy because they can find a great site and this interesting. I was amazed by you. You are very innovative and full of creativity. This really really rare
    cara menghilangkan benjolan di payudara

  16. Awesome site, thanks for your information guys :)

    We are a community card game biggest and most reliable.
    feel the sensation of thrill to play with us
    get bonus and the biggest jackpot!
    Only in www.SBOQQ.com
    Come and Join us! Domino QQ

  17. article was very nice and helpful at all for me, thank you very much admin and pardon me permission to share articles herein may be useful for all of you in particular who are looking for :

    Cara mengatasi mata merah dan sakit
    Obat maag akut
    Obat tradisional encok

  18. I would like to say thanks for this wonderful information
    Well written & I appreciate your work…
    Windows 8.1 product key

  19. Thank you for sharing! Can you sharing some updates on how you have made this powerful post! Can you sharing some updates on how you have made this powerful post!
    geometry dash | fb login | bloons tower defense 5

  20. Let’s keep are safe place to play the very best free games for kids! Please click: wingsio | slither io

  21. I like the news that you have written in a detail. Thank you>>>>Y8 Arcade
    Y8 Online
    Yepi 2017
    Friv 2017
    Kizi Games
    Y8 Games

  22. Halo bosku.. Bosan asik main judi online kalah-kalah aja? Buruan gabung bersama kami di
    W W W .C U M A P O K E R . C O M dijamin no ROBOT bosku.

    Kami menyediakan 7 jenis game dalam 1 user ID bosku
    -Bandar Poker
    -Capsa Susun
    -Sakong ( New Game yang sedang populer )

    Bonus yang diberikan kepada seluruh member kami yaitu
    -Bonus Turnover sebesar 0,5%
    -Bonus Referal sebesar 15%

    Kami menyediakan minimal deposit dan withdraw yang sangat rendah yakni Rp 15.000

    Kami juga menyediakan game yang tidak kalah populer bosku.

    Jadi, tunggu apa lagi segera bergabung bersama kami dan dapatkan hadiah ratusan juta.
    Yahoo : cumapoker88@yahoo.com
    BBM : 2BE3DCA9
    No HP : +85585569262
    We chat : cumapoker

  23. Post is Excellent .Keep Visiting.I would like to say thanks for this wonderful information. Download FL Studio 12 Crack

  24. Thank you so much admin is already providing the information and sorry here we share permissions by using efficacious herbal ingredients such as using materials Obat flek paru paru

  25. Exploring a better world for providing this marvelous information which i need to know more. Therefore I think there is no harm if we change the unhealthy lifestyle that we've run with a healthy lifestyle. Thanks cara menghilangkan benjolan di payudara

  26. Thank you so much admin is already providing the information to us and here we are sorry permission to share articles may be useful and help you in particular are looking for :

    Cara menyembuhkan gerd
    Cara menyembuhkan batuk menahun
    Cara menyembuhkan pneumonia


About Me

My Photo
Durodola Ridwan is a Professional blogger, SEO Consultant, Certified Web developer/designer and founder of www.realcombiz.com. He love writing about Blogging tips, SEO, and play around with codes. You can connect with him on facebook, twitter @realcombiz, and Google+.

Recent Posts

Let's Connect

Site Links

Copyright © 2014 RealcomBiz. All Rights Reserved.
Powered by Blogger