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

How to Install Packet Tracer 6.0.1 for Linux

This article is intended to help me remember the installation process for Cisco Packet Tracer 6 details of which seems to be hard to come by on the web. 1. Log in in to your Cisco Netspace account. Yes, you need to be logged in. 2. The " Download PT 6.0.1 " link on is on the NetSpace home page. 3. Click this " Cisco Packet Tracer " link shown below the folder icon as shown in the figure below: 4. Select  Cisco Packet Tracer 6.0.1 5. Select download location. 6. After completion of download, use your file manager to open that folder and rename the file to something such as "CPT6". It is a shell script so no file extension is required. 7. Change the file permissions to executable using command: sudo chmod 777 CPT6 from a terminal window opened in the download directory. This assumes your downloaded file is renamed CPT6 8. Launch the file with command: ./CPT6 from the terminal window. You will get the message: Self extracting archive... We...

How to Install Visual Basic 2017 Offline

This page offers a way to avoid the long and error-prone online installation of Visual Studio which is a massive package. Our interest is limited to Visual Basic and ASP.Net. One can, later, add other tools such as C# and C++. In this page, "VS" refers to Visual Studio. Installing Visual Basic 2017 Offline is not a simple process because Visual Basic 2017 is a part of Visual Studio 2017 that comes as one huge package (up to 35GB) in three versions: Community Edition (Free) Professional Edition (intended for a few people) Enterprise (intended for larger software teams)  Visual Studio by default uses a web-based install process that starts by downloading one of three the bootstrapper package from the VS download page . When you run the bootstraper, it will initiate download of the Visual Studio components that may be up to 35GB in size and takes a very long time to complete. The purpose of this page is to explain how to avoid the headache of this long and tedious ...