Real World Use of CSS with SVG

SVG is a lightweight vector image format that’s used to display a variety of graphics on the Web and other environments with support for interactivity and animation. In this article, we’ll explore the various ways to use CSS with SVG, and ways to include SVGs in a web page and manipulate them.

The Scalable Vector Graphic (SVG) format has been an open standard since 1999, but browser usage became practical in 2011 following the release of Internet Explorer 9. Today, SVG is well supported across all browsers, although more advanced features can vary.

SVG Benefits

Bitmap images such as PNGs, JPGs and GIFs define the color of individual pixels. An 100×100 PNG image requires 10,000 pixels. Each pixel requires four bytes for red, green, blue and transparency so the resulting file is 40,000 bytes (plus a little more for meta data). Compression is applied to reduce the file size; PNG and GIF use ZIP-like lossless compression while JPG is lossy and removes less noticeable details.

Bitmaps are ideal for photographs and more complex images, but definition is lost as the images are enlarged.

SVGs are vector images defined in XML. Points, lines, curves, paths, ellipses, rectangles, text, etc. are drawn on an SVG canvas. For example:

<svg xmlns="" viewBox="0 0 800 600">
  <circle cx="400" cy="300" r="250" stroke-width="20" stroke="#f00" fill="#ff0" />

The viewBox defines a co-ordinate space. In this example, an 800×600 area starting at position 0,0 has a yellow circle with a red border drawn in the center:

SVG circle

The benefits of vectors over bitmaps:

  • the SVG above uses less than 150 bytes, which is considerably smaller than an equivalent PNG or JPG
  • SVG backgrounds are transparent by default
  • the image can scale to any size without losing quality
  • SVG code/elements can be easily generated and manipulated on the server or browser
  • in terms of accessibility and SEO, text and drawing elements are machine and human-readable.

SVG is ideal for logos, charts, icons, and simpler diagrams. Only photographs are generally impractical, although SVGs have been used for lazy-loading placeholders.

SVG Tools

It’s useful to understand the basics of SVG drawing, but you’ll soon want to create more complex shapes with an editor that can generate the code. Options include:

Each has different strengths, and you’ll get differing results for seemingly identical images. In general, more complex images require more complex software.

The resulting code can usually be simplified and minimized further using SVGO (plugins are available for most build tools) or Jake Archibold’s SVGOMG interactive tool.

SVGs as Static Images

When used within an HTML <img> tag or CSS background-url, SVGs act identically to bitmaps:

<!-- HTML image -->
<img src="" alt="a vector graphic" />
/* CSS background */
.myelement {
  background-image: url('mybackground.svg');

The browser will disable any scripts, links, and other interactive features embedded into the SVG file. You can manipulate an SVG using CSS in an identical way to other images using transform, filters, etc. The results of using CSS with SVG are often superior to bitmaps, because SVGs can be infinitely scaled.

CSS Inlined SVG Backgrounds

An SVG can be inlined directly in CSS code as a background image. This can be ideal for smaller, reusable icons and avoids additional HTTP requests. For example:

.mysvgbackground {
  background: url('data:image/svg+xml;utf8,<svg xmlns="" viewBox="0 0 800 600"><circle cx="400" cy="300" r="50" stroke-width="5" stroke="#f00" fill="#ff0" /></svg>') center center no-repeat;

Standard UTF-8 encoding (rather than base64) can be used, so it’s easy to edit the SVG image if necessary.

CSS with SVG: Responsive SVG Images

When creating a responsive website, it’s normally practical to omit <img> width and height attributes and allow an image to size to its maximum width via CSS:

img {
  display: block;
  max-width: 100%;

However, an SVG used as an image has no implicit dimensions. You might discover the max-width is calculated as zero and the image disappears entirely. To fix the problem, ensure a default width and height is defined in the <svg> tag:

<svg xmlns="" width="400" height="300">

Note: the dimensions should not be added to inlined SVGs, as we’ll discover in the next section …

The post Real World Use of CSS with SVG appeared first on SitePoint.

Source link

Leave a Reply

Your email address will not be published. Required fields are marked *