close icon
daily.dev platform

Discover more from daily.dev

Personalized news feed, dev communities and search, much better than what’s out there. Maybe ;)

Start reading - Free forever
Start reading - Free forever
Continue reading >

SVG Basics for Web Developers

SVG Basics for Web Developers
Author
Nimrod Kramer
Related tags on daily.dev
toc
Table of contents
arrow-down

🎯

Learn the basics of SVG (Scalable Vector Graphics) for web development, including creating, styling, animating, and interacting with SVG images. Discover the advantages and differences between SVG and raster graphics.

SVG, or Scalable Vector Graphics, is a versatile format perfect for web development, offering clear, resizable images that look sharp at any size. Here are the basics you need to know:

  • Vector-based: SVGs use lines and shapes, allowing them to stay crisp no matter how much you zoom in.
  • Code-based: Created with XML, making them easy to edit with standard web tools.
  • Interactive and animated: Can be manipulated with CSS and JavaScript for dynamic web experiences.
  • Accessible: Support for screen readers and keyboard navigation makes SVGs inclusive.

This guide covers creating SVGs, adding them to web pages, the differences between SVG and raster graphics, and tips for styling and animating your SVGs. Whether you're making simple shapes or complex illustrations, SVGs enhance your site's visuals without sacrificing performance.

What is SVG?

SVG stands for Scalable Vector Graphics. It's a type of image for the web that's made with XML, a code similar to what websites are built with. Unlike regular pictures that can get blurry when you zoom in, SVG images stay sharp at any size because they use lines and shapes drawn with math.

SVG images work well on the web because you can edit them with just a text editor, and they can do cool things like move or change when you interact with them. Plus, most web browsers can show SVG images without any problem.

Brief History of SVG

  • 1999 - The W3C (a group that makes web standards) started working on SVG.
  • 2003 - SVG became an official web standard.
  • 2011 - They updated SVG to make it even better.
  • 2016 - They're still working on making SVGs do more cool stuff.

SVG has been around for a while and has gotten more popular because it makes websites look good and work fast.

Why Use SVG?

SVG images have lots of perks:

  • Scalability - They can get really big or really small without losing quality.
  • Editability - You can change them with a text editor since they're made with code.
  • Responsiveness - They can adjust to look good on any screen.
  • Performance - They usually make websites load faster because they're not as heavy as other images.
  • Accessibility - You can add extra info to them so search engines and screen readers understand them better.
  • Animation - You can make parts of an SVG image move or change.

SVG is great for making websites because it's flexible, keeps images looking sharp, and helps pages load quickly.

SVG vs Raster Graphics

Key Differences

When we talk about SVG (Scalable Vector Graphics) and raster graphics like JPG and PNG, we're looking at two different ways to make images. Here's a simple breakdown:

  • File Format: SVGs are drawn using lines and shapes defined by XML code, kind of like a digital blueprint. JPG and PNG images are made up of tiny dots called pixels.
  • Scalability: SVGs can be stretched or shrunk to any size without getting blurry because they're based on these blueprints, not dots. Raster images start to look fuzzy when you make them bigger.
  • Editability: You can easily tweak SVGs by changing their code. With raster images, making changes can be harder once the image is made.

Comparison Table

Comparison Criteria SVG Raster Graphics
File Format Vector
(XML-based)
Bitmap
Scalability Looks good at
any size
Gets blurry when big
Editability Easy to change Harder to tweak
File Size Can be big for complex designs Usually smaller
Browser Support Mostly good Really good

SVGs use lines and shapes to make images that stay sharp no matter how much you zoom in or stretch them out. Raster images, on the other hand, can lose their clarity when you change their size. SVGs are great for web design and making graphics that need to look good on screens of any size.

Getting Started with SVG

Creating an SVG Image

Making a simple SVG image is straightforward. You can do it with any text editor. Here's a quick example of how to draw a basic red square:

<svg width="100" height="100">
  <rect width="100" height="100" fill="red"/> 
</svg>

Just open a new text file, type in the SVG code like the example above, and save it with a .svg ending. Here's what each part does:

  • The <svg> tag starts and ends your SVG image.
  • width and height decide how big the image area is.
  • You use SVG shapes, such as <rect>, to make what you want in the image.
  • Attributes like width, height, and fill change how the shapes look.

This is the basic recipe for creating an SVG file that browsers can show. You can add more elements like circles, lines, or text to make it more interesting.

Viewing SVG Files

To see your SVG file, you have a few options:

  • Just open the .svg file in a web browser.
  • Use web development tools to look at the XML code.
  • Edit the SVG in a program like Inkscape.
  • Put it into an HTML file to see it on a web page.

Opening the SVG file in a browser is the fastest way to check your work. Web development tools are great for a closer look at the code. If you want to make changes, an SVG editor like Inkscape is the way to go.

Adding SVG to Web Pages

You can put SVG images on web pages in two ways: by embedding them right into the HTML or by linking to an external SVG file.

Here's how to embed SVG directly into a webpage:

<body>
  <svg width="100" height="100">
    <circle cx="50" cy="50" r="40" stroke="green" stroke-width="4" fill="yellow" />
  </svg>  
</body>

And this is how you link to an SVG file:

<img src="circle.svg">

Embedding SVG directly is good for simple images, but for more complex ones, linking to an external file helps keep your HTML neat. Both ways let you use CSS to style the SVG.

Deep Dive into SVG Syntax

Basic SVG Elements

SVG images use simple shapes like rectangles, circles, lines, and more. These shapes are made with special tags in XML, a code that tells the computer how to display the image. Each shape has its own set of rules, like size, position, and color.

Here are a few basic shapes you might start with:

  • <rect>: This tag makes a rectangle. You can set where it goes and how big it is with attributes like x, y, width, and height. Use fill to color it in.
  • <circle>: This tag draws a circle. Use cx and cy to place the center and r for the radius.
  • <line>: This tag draws a straight line from one point to another. The attributes x1, y1, x2, and y2 tell it where to start and end.
  • <polygon>: This tag lets you make shapes with any number of sides. The points attribute holds the list of points in the shape.
  • <text>: Use this to add text. Place it with x and y, and change how it looks with style attributes like font-size and fill.

Mixing and matching these shapes lets you build more complex images.

The <path> Element

The <path> element is a powerful tool for drawing complicated shapes. It uses a series of commands to move around the canvas and draw lines or curves:

  • M: Moves to a new start point
  • L: Draws a line to a new point
  • H: Draws a horizontal line
  • V: Draws a vertical line
  • C: Makes a curvy line
  • S: Makes a smooth curvy line
  • Q: Draws a different kind of curve
  • T: Smooths out a curve
  • Z: Closes off the shape

For instance, to draw a triangle, you might write something like this:

<path d="M100 100 L300 100 L200 300 z"/>

This tells the computer to start at one point, draw lines to two more points, and then close the shape.

SVG Coordinate System

SVGs have their own way of figuring out where to put things. Here's what you need to know:

  • The starting point is usually the top-left corner.
  • The x direction goes right, and y goes down.
  • Use width and height to say how big the whole image should be.
  • The viewBox lets you zoom in or out and move around inside your image.

You can place things exactly where you want with x and y, or use special tricks to move them around more freely. Getting the hang of this system is key to making precise drawings.

sbb-itb-bfaad5b

Styling, Animating and Interacting with SVG

Styling SVG with CSS

You can make your SVGs look different using CSS, just like you would with normal web page elements. This means you can change how they look without touching the SVG code itself.

For example, if you want to change the color inside a circle and its border color in an SVG, you could use CSS like this:

svg circle {
  fill: blue;
  stroke: red;  
}

This code makes the inside of all <circle> shapes blue and their border red.

Some things you can change with CSS include:

  • fill - The color inside a shape
  • stroke - The color of the shape's border
  • stroke-width - How thick the border is
  • opacity - How see-through the shape is
  • filter - Adds effects like blur or color change

CSS lets you do cool things like adding smooth color changes (gradients), making things move smoothly (transitions), or turning and resizing shapes (transforms).

SVG Animation with CSS & JavaScript

You can make SVGs move or change over time in a few ways:

CSS Animations

You can use CSS to make animations by changing how something looks bit by bit.

For example, to make an SVG element spin around:

@keyframes spin {
  to {
    transform: rotate(360deg); 
  }
}

svg rect {
  animation: spin 5s linear infinite;
} 

CSS Transitions

CSS transitions make changes look smooth. Like moving a shape from one place to another gently:

svg rect {
  transition: transform .5s;  
}

rect:hover {
  transform: translateX(100px); 
}

JavaScript & SVG Animate

For more complicated movements, you can use JavaScript to change SVGs in many ways.

The <animate> tag in SVG also lets you set up animation sequences right in the SVG.

Interactive SVG

SVGs can do things when you interact with them:

  • JavaScript Events - They can react to mouse clicks, hovering over them, or dragging.
  • Hyperlinks - You can make parts of an SVG work like clickable links using <a>.
  • Tooltips - You can show extra info when someone hovers over parts of an SVG, using either CSS or JavaScript.
  • Filters & Effects - You can change how an SVG looks based on what the user does.

This means you can create SVGs that invite people to click, hover, and explore more.

Conclusion

SVG is a super handy tool for making pictures and designs on websites that look sharp and work well on any device. Let's go over why SVG is so great:

Scalability - SVG images can get bigger or smaller but still look super clear, unlike other kinds of images that can get blurry. This is perfect for making websites that work on both big screens and tiny phones.

Editability - Because SVG is made with XML (a type of code), it's easy to change things up directly with a simple text editor. This means you can quickly adjust your designs.

Animation - You can make parts of an SVG move or change with a bit of CSS or JavaScript, adding some fun interactions.

Compatibility - All the newer web browsers can show SVGs without any trouble. Some older ones might need a little help with extra code.

Performance - Simple SVG files are usually smaller than other image files, helping websites load faster. Even complex SVGs can be made to not slow things down too much.

Accessibility - SVGs are good for everyone because they support features that help people using screen readers or who can't use a mouse.

Versatility - SVG works well with CSS and JavaScript, and fits right into web pages, giving you lots of options for your web design.

Getting the hang of SVG might take a bit of time because it has its own special code and way of placing things. But once you get it, you can make all sorts of cool graphics and interactive designs. SVG is a skill worth learning for anyone making websites.

If you want to learn more, check out the SVG guides on MDN Web Docs or read books like Practical SVG by Chris Coyier. But the best way to get good at SVG is by trying to make things with it. The world of web graphics is more exciting with SVG, so go ahead and start creating!

What is the basic SVG format?

SVG stands for Scalable Vector Graphics. It's a type of image file that's great for the web because it uses math to define shapes and lines, which means it can be resized without getting blurry. An SVG file is made with XML, a code that looks a bit like HTML. Here's a simple example that draws a circle:

<svg width="100" height="100">
  <circle cx="50" cy="50" r="40" stroke="green" stroke-width="4" fill="yellow" /> 
</svg>

This code creates a yellow circle with a green outline.

Is SVG suitable for web?

Absolutely, SVG is perfect for websites because:

  • It doesn't lose quality no matter how much you zoom in or stretch it.
  • It usually has a smaller file size, which helps websites load faster.
  • Search engines can read its text, making it good for SEO.
  • You can animate parts of it with CSS or JavaScript.
  • You can change how it looks with CSS.
  • It’s accessible with the right setup, meaning it works well for people using screen readers.
  • All modern web browsers support SVG well.

SVGs are great for logos, icons, and any graphics that need to look sharp at various sizes.

How do you use SVG in web?

You can add SVG to your web pages in two main ways:

  • Directly in your HTML with the <svg> tag. This is straightforward but can make your HTML file big if the design is complex.
  • As an external file linked to your HTML with an <img>, <object>, or <iframe> tag. This keeps your HTML cleaner but means managing another file.

Here's how you might do it:

<!-- Directly in HTML -->
<svg width="100" height="100">
  <rect width="100" height="100" fill="blue"/>
</svg>

<!-- As an external file -->  
<img src="square.svg">

Both ways let you style the SVG with CSS and make it interactive with JavaScript.

How to create SVG for website?

To make an SVG for your site, follow these steps:

  • Use a vector graphics program like Illustrator or Inkscape to design your image.
  • Save your design as an SVG file.
  • Clean up the SVG code by removing unnecessary details.
  • Put the SVG code directly into your HTML or link to the SVG file.
  • Use CSS to style the SVG so it fits your site's look.
  • Add animations or interactivity with CSS or JavaScript if you like.
  • Make sure it’s accessible by adding descriptions for screen readers.

By following these steps, you'll have a sharp, web-ready SVG for your project.

Related posts

Why not level up your reading with

Stay up-to-date with the latest developer news every time you open a new tab.

Read more