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

Styling SVG (Scalable Vector Graphic) with CSS

by Unknown | Monday, October 13, 2014 | 37 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:

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



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:

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

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:

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


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">
<![CDATA[
.example {
fill: grey;
stroke: black;
stroke-width: 2;
}
]]>
</style>
<svg>
<rect x="10" y="10" width ="150" height="150" class="example"/>
</svg>

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"/>
</g>




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">
<![CDATA[
.example {
fill: slategrey;
stroke: black;
stroke-width: 2;
transition: all 2s ease;
}
.example:hover {
fill: blue;
}
]]>
</ style >
<svg>
<rect x="10" y="10" width ="150" height="150" class="example"/>
</svg>


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:

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

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!".

37 comments : Post Yours! Read Comment Policy ▼
PLEASE NOTE:
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

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

    ReplyDelete
  3. 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.

    ReplyDelete
  4. 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

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

    ReplyDelete
  6. 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

    ReplyDelete
  7. 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

    ReplyDelete
  8. 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

    ReplyDelete
  9. 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
    -AduQ
    -BandarQ
    -Poker
    -Bandar Poker
    -Capsa Susun
    -Domino
    -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.
    -FISH HUNTER ( TEMBAK IKAN )
    -SABUNG AYAM ( COCK FIGHT )
    -JOKER SLOT

    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

    ReplyDelete
  10. a given article is very interesting and very useful for me, thank you very much admin and pardon me permission to share the article here :

    Cara mengatasi mata berair dan berpasir
    Cara menyembuhkan batuk kering
    Cara menyembuhkan abses payudara

    ReplyDelete
  11. Very Fantastic article, I really like your point of view!
    Good Job And continue to make useful articles.
    Sent from Honest Jvzoo Reviews
    Sent from Scarcity Maximizer Review

    ReplyDelete
  12. I was recommended this blog by my cousin. I’m not sure whether this post is written by him as no
    one else know such detailed about my difficulty.
    You’re amazing! Thanks!

    Berita terkini ahok

    PKR

    Bandar Q

    LayarKaca21

    ReplyDelete
  13. This comment has been removed by the author.

    ReplyDelete
  14. Hi to every single one, it’s actually a fastidious
    for me to pay a visit this web page, it consists of important Information.

    agen bonus terbesar
    induk poker
    domino88
    free chip domino
    bonus domino terbesar
    link alternatif domino

    ReplyDelete
  15. An interesting dialogue is worth comment. I believe that you should write more on this subject, it may not be a taboo subject but generally people are not enough to talk on such topics. To the next. Cheers


    Misteri
    judi poker
    Bandar Sakong
    SejarahQQ
    Bosdomino
    Agen Sakong

    ReplyDelete
  16. Replies
    1. Very good post. I certainly love this website. Keep writing!

      subway surf  | launcher | geometry dash online |

      Delete
  17. I really like your blog! Continue to write more! Very interesting!
    bloxorz

    ReplyDelete
  18. Hi, after reading this amazing post i am too happy to
    share my know-how here with mates. Stranded Deep PC Game

    ReplyDelete
  19. Pretty section of content. I just stumbled upon your web site
    and in accession capital to assert that I get in fact enjoyed account your
    blog posts. Anyway I will be subscribing to your feeds and even I achievement
    you access consistently quickly.UsbFix 2018 Crack Patch

    ReplyDelete
  20. Daftarkan diri anda dan bergabung bersama kami sekarang juga di VipjudiQQ
    cukup dengan 1 ID bisa merasakan 7 permainan sekaligus lohhhh...
    rasakan sensasi fairplay 100% player vs player no BOT's no ADMIN !

    ReplyDelete
  21. I saw this site and read this article.it is helpful for me.thanks for writing. I create this site I want to you see this site and leave a coment.thanks!
    google-sketchup

    ReplyDelete

Recent Posts

Let's Connect

Site Links

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