Skip to main content

How to Use CSS Within SVG Embeded in HTML

There are six ways of embedding SVG in HTML. However, not all are equal neither are they well-behaved. In this article the issue at hand is how to style embedded SVG with an external style sheet.
The three components of this set up include:
1. The SVG file
2. The HTML file
3. The CSS file

 Using <object> to embed SVG

 This is quite easy. The <object> code within the HTML file will look like this:

<!DOCTYPE html>
<html>
<head>
</head>
<body>

<object type="image/svg+xml" data="test1.svg"/>

</body>
</html>

 SVG

A simple SVG [test1.svg] is shown below:

<?xml version="1.0" standalone="no"?>
<?xml-stylesheet type="text/css" href="style.css"?>

<svg width="400" height="300" xmlns="http://www.w3.org/2000/svg" version="1.1">
  <rect height="100" width="100" id="nrb1" />
 
  <circle  cx="200" cy="200" r="100" id="naks" />
</svg>


Note:
i) The XML statement that points to an external CSS file looks like this:
 <?xml-stylesheet type="text/css" href="style.css"?>

 ii) The id attributes which are used in CSS to specify the shape to style

The CSS sample [style.css] is as follows:

 #nrb1 {
  fill: red;
  stroke: blue;
}

#naks {
  fill: yellow;
  stroke: green;
  stroke-width: 5px;
}

 
 Save all these in one folder and open the HTML file in a browser. 

To test if the CSS works, change the colours and reload the HTML. You can also try adding new attributes such as stroke-width.

 Download examples files

Comments

Popular posts from this blog

How to Install RIOT Plugin for GIMP

How to Install RIOT Plugin for GIMP::  Since this article was first written a stand-alone version of RIOT has been made available Download the plugin from   http://luci.criosweb.ro/riot/download/   Select the download link titled " RIOT as plug-in for other applications "    Launch the  Riot-plugin.exe  file by double clicking. The installer installs all the files including Riot.exe, Riot.dll, and FreeImage.dll in the location where you installed GIMP  If GIMP is open, close it and re-launch it. To optimise an image, open it then save with menu  File-&gt;Save for web with RIOT    You can set the target file size by selecting " Compress to size " button  on bottom right corner of optimised image pane and providing the size in kilobytes. Alternatively you can use a slider provided to allow you to change the quality while showing you a preview of the resulting image. Enjoy .       ...

XUBUNTU PANEL AND WINDOW BUTTONS HAVE DISAPEARED

 XUBUNTU PANELS Xubuntu can be quite troublesome when you shutdown and restart. Desktop panels may disappear as well as window minimize/maximise/close buttons.  The panel is equivalent to the Windows 10 Start menu that provides access to applications. TO MAKE THE PANEL REAPPEAR Open a terminal by right-clicking on the desktop then enter the following: xfwm4 --replace TO RECOVER THE PANEL Open a terminal by right-clicking on the desktop then enter the following: xfCE-panel --restart  That is all . As we say in Kiswahili: Mambo kwisha!

Installing NGINX with PHP and MySQL

My OS: Windows 7 Home Premuim 64bit Software Installed: NGINX 0.9.5 for Windows PHP: php-5.3.5-Win32-VC6-x86.zip MySQL: mysql-5.5.9-winx64.msi NGINX Installation Download from  http://nginx.org/en/download.html Unzip into C:\nginx Change the nginx.conf in C:\nginx\conf as per instructions at http://blog.siteroller.net/set-up-nginx-mysql-and-php-wemp-on-windows See my nginx.conf file  here . Note  that in this example, it is assumed that PHP is installed in C:\nginx\php (unzipped into this folder) and MySQl is installed in C:\Program Files\MySQL PHP Installation After downloading the zip version of PHP (V6, threadsafe, zip), unzip into C:\nginx\php\ Rename  php.ini-production  to  php.ini Make changes to this file as given in at  http://blog.siteroller.net/set-up-nginx-mysql-and-php-wemp-on-windows  but do not expect the line numbers given to match exactly. Activate the extensions  extension=php_...