Skip to content
Tutorials Center
My learning

Topics

Lesson 80 of 122 ยท Graphics

SVG Basics

Draw sharp, scalable graphics with inline SVG. Learn the coordinate system and viewBox, the basic shapes, fill and stroke, and when to use img instead.

Icons, logos, charts and diagrams need to look crisp on every screen, from a small phone to a huge high-resolution monitor. Photos are stored as grids of pixels, so they get blurry when you scale them up. SVG (Scalable Vector Graphics) works differently: it describes shapes with math, like "a circle with its center here and this radius", and the browser draws them at whatever size is needed. The result is always sharp, and the files are often tiny.

SVG is a markup language, just like HTML, and you can write it directly inside an HTML page. That means you can style it with CSS, change it with JavaScript and make it accessible. In this lesson you'll learn how the SVG coordinate system works, draw every basic shape, color them with fill and stroke, and decide when to embed SVG inline versus loading it as an image.

Your first inline SVG

An <svg> element creates a drawing area. Shapes go inside it:

A first drawing

A few things to notice:

  • SVG elements look like HTML elements but are part of a different vocabulary. When you write <svg> inside an HTML page, the browser switches to SVG mode until the closing </svg>.
  • Shapes without content can be written with a self-closing slash, like <circle ... />. In SVG that's normal and fine. (The slash is optional in inline SVG, but many people keep it because standalone SVG files are XML, where it's required.)
  • Shapes are painted in source order. Later shapes cover earlier ones, so the background rectangle comes first.

Inline SVG doesn't need the xmlns attribute; the HTML parser knows what <svg> is. A standalone .svg file, however, must declare xmlns="http://www.w3.org/2000/svg" on its root element.

Coordinates and the viewBox

SVG uses a coordinate system with its origin at the top-left corner. The x-axis goes right and the y-axis goes down, which surprises many people who remember math class.

The viewBox attribute defines which part of that coordinate space is visible. It takes four numbers: min-x min-y width height.

HTML
<svg viewBox="0 0 100 50" width="400" height="200">โ€ฆ</svg>

This says: "show the region from (0, 0) to (100, 50)". The width and height attributes say how big the SVG should appear on the page. Since 400 is four times 100, every unit in the drawing becomes four CSS pixels. You can draw in whatever units are convenient and let the viewBox scale everything.

That's what makes SVG truly scalable. With a viewBox, you can remove width and height, or set the size with CSS, and the whole drawing grows and shrinks smoothly:

One drawing, three sizes

The coordinates are identical in all three; only width differs. Try changing the viewBox to "0 0 50 50" in one of them: you'll zoom into the top-left quarter of the drawing.

If the viewBox and the display area have different shapes, SVG keeps the drawing's proportions and centers it by default. The preserveAspectRatio attribute changes that behavior, but the default is right most of the time.

The basic shapes

SVG has six basic shapes. Each is controlled by its own attributes:

Element Key attributes Draws
<rect> x, y, width, height, optional rx/ry A rectangle; rx rounds the corners
<circle> cx, cy (center), r (radius) A circle
<ellipse> cx, cy, rx, ry An oval with separate horizontal and vertical radii
<line> x1, y1, x2, y2 A straight line between two points
<polyline> points A series of connected straight lines (open)
<polygon> points Like polyline, but the last point joins the first (closed)

The points attribute is a list of x,y pairs: points="10,80 50,20 90,80".

All six shapes

The <line> has a stroke but no fill, because a line has no inside to fill. Without stroke it would be invisible. Likewise, the polyline sets fill="none"; otherwise the browser would fill the area between its first and last points with black.

Fill and stroke

Every shape has two painted parts:

  • Fill is the inside. The default is black.
  • Stroke is the outline. The default is none.

Useful painting attributes include:

Attribute Effect
fill Inside color, or none
stroke Outline color, or none
stroke-width Outline thickness, in user units
fill-opacity, stroke-opacity, opacity Transparency from 0 to 1
stroke-linecap Line ends: butt, round or square
stroke-linejoin Corners: miter, round or bevel
stroke-dasharray Dashes, for example "8 4" for 8 units on, 4 off

The stroke is centered on the shape's edge: half of it falls inside, half outside. A shape at the very edge of the viewBox may have half its stroke cut off.

These presentation attributes are also CSS properties. You can set them in a stylesheet, and CSS rules override the attributes:

Styling SVG with CSS

The keyword currentColor uses the element's CSS color, so the icon follows the text color around it. This is the standard trick for icons that should match the surrounding text and react to hover, focus and dark mode. You'll find more about color values in Colors.

Inline SVG or img?

There are two common ways to put an SVG on a page.

As an image, with <img src="logo.svg" alt="โ€ฆ"> (or as a CSS background):

HTML
<img src="/samples/sunset.svg" alt="A cartoon sunset over green hills" width="320" height="200">
  • The file is cached by the browser and can be reused on many pages.
  • Your HTML stays short.
  • But the SVG is sealed off: page CSS can't style its insides, currentColor can't come from the page, and scripts inside it don't run.

Inline, with <svg> directly in the HTML:

  • You can style every shape with CSS, including hover effects and theme colors.
  • JavaScript can change it, for example to animate a chart.
  • It's part of the page's accessibility tree, so you control how it's announced.
  • But it isn't cached separately, and large or repeated graphics make the HTML heavier.

A good rule of thumb: use <img> for illustrations and other artwork that just needs to be shown, and inline SVG for icons and graphics that must match your styles or respond to the user. For more on <img>, see Images.

Accessibility preview. An <img> gets its text alternative from alt. Inline SVG needs a different technique, using role="img" and a <title>, which you'll learn in the next lesson. Purely decorative inline SVG should get aria-hidden="true" so screen readers skip it.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. In SVG coordinates, where is the point (0, 0) and which way does y increase?

    The origin is the top-left corner, x grows to the right and y grows downward.
  2. 2. An SVG has viewBox="0 0 50 50" and width="200". How many CSS pixels does one user unit become?

    50 units are stretched across 200 pixels, so each unit is 4 pixels wide.
  3. 3. You draw a <line> with only x1, y1, x2 and y2. Why can't you see it?

    A line has no inside to fill, and strokes are off by default. Add stroke (and usually stroke-width).
  4. 4. You want an icon to change color when its parent link is hovered. What's the best approach?

    Inline SVG can be styled by page CSS, and currentColor makes it follow the text color. An SVG loaded with <img> can't be styled from outside.

Summary

  • SVG describes graphics as shapes, so they stay sharp at any size.
  • Inline <svg> lives directly in your HTML; the origin is top-left and y points down.
  • viewBox="min-x min-y width height" sets the visible coordinate region, and width/height (or CSS) set the displayed size.
  • The basic shapes are rect, circle, ellipse, line, polyline and polygon.
  • fill paints the inside (default black), stroke the outline (default none); both can be set with CSS, and currentColor ties them to text color.
  • Use <img> for self-contained artwork and inline SVG when you need styling, scripting or fine-grained accessibility.

Next lesson: SVG Paths, Text and Gradients.