打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Styling Scalable Vector Graphic (SVG) with CSS

Today we are going to continue our discussion on Scalable Vector Graphic (SVG), and as we have pointed out in our previous post, one of the advantages of using SVG is that it can be styled with CSS.



SVG Styling Properties


Styling SVG basically work the same way as in regular HTML elements, they in fact shared some common properties as well. However, there are other properties that are intended specifically for SVG object which have their own specification apart from CSS.


For instance, in regular HTML element, we can add background color either with background-color or background CSS property. In SVG, it is a little bit different; the background color is specified with the fill property instead. Also, element’s border is specified with stroke property and is not with the border like we did in regular HTML, you can see the complete list here.


If you have been working with vector editor like Adobe Illustrator quite long, you can guess quickly that the property naming mechanism in SVG is originated from the editor.



SVG Style Implementation


We can use one of the following ways to style SVG element;


Recommended Reading: Reviewing CSS Style Priority Level


Presentation Attributes


If you have seen all SVG properties list, they all can be added directly on the element to alter the element’s presentation. The following example shows how we can add fill and stroke property directly on a rect element;


  1. <svg>  
  2. <rect width="200" height="200" fill="slategrey" stroke="black" stroke-width="3"/>  
  3. </svg>  

The rectangle will turn out to be like the screenshot below;



Inline Style Sheet


We can also add style with the style attribute. In the following example we will also add fill and stroke to the rect, but this time we add it within the style and rotate it with CSS3 transform property, like so;.


  1. <svg>  
  2. <rect x="203" width="200" height="200" style="fill:slategrey; stroke:black; stroke-width:3; -webkit-transform: rotate(45deg);"/>  
  3. </svg>  

The rectangle will turn in the same result, only that it is now also rotated;



Internal Style Sheet


Embed the SVG style within the style element is also possible and is no different to how we did it on regular HTML. This example below shows how we add internal styles to affect SVG elements in .html document.


  1. <style type="text/css" media="screen">  
  2.     #internal rect {  
  3.         fill: slategrey;  
  4.         stroke: black;  
  5.         stroke-width: 3;  
  6.         -webkit-transition: all 350ms;  
  7.     }  
  8.     #internal rect:hover {  
  9.         fill: green;  
  10.     }  
  11. </style>  

However, SVG is an XML-based language, so when we are about to add inline style sheet in a .svg document, we need to put the styles declaration inside cdata, as follows;


  1. <style type="text/css" media="screen">  
  2.     <![CDATA[  
  3.     #internal rect {  
  4.         fill: slategrey;  
  5.         stroke: black;  
  6.         stroke-width: 3;  
  7.         -webkit-transition: all 350ms;  
  8.     }  
  9.     #internal rect:hover {  
  10.         fill: green;  
  11.     }  
  12.     ]]>  
  13. </style>  

The cdata here is required, since CSS can have > character that will conflict with XML parsers. Using cdata is highly recommended for embedding style in SVG, even if the conflicting character is not given in the style sheet.



External Style Sheet


The external style sheet also work the same way for SVG elements in .html document.


<link rel="stylesheet" type="text/css" href="style.css">

Again in .svg document, we need to refer the external style sheet as an xml-stylesheet, like so.


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

Grouping Elements


SVG elements can be grouped with the <g> element. Grouping elements will allow us to share common styles to all the elements in the group, here is an example;


<g style="fill:slategrey; stroke:black; stroke-width:3; fill-opacity: 0.5;">	<rect x="203" width="200" height="200"/>	<circle cx="120" cy="106" r="100"/></g>

Both the rectangle and the circle will have the same result.



Final Thought


We have walked through all the essential matters on styling SVG with CSS and this is just one of the advantages of serving graphic with SVG. In the next post we will take a look into another one further, so stay tuned.





           
      
   
    
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
C# Article: Create, Open, Load, Save, Import,...
《SVG精髓》笔记(二)
一篇文章带你了解SVG javascript脚本
Create an SVG Animation using CSS and JavaScript
Icon System with SVG Sprites | CSS
编程前端学习1
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服