Getting started

A basic example

Let us dive straight in with an example. Take a look at the following code.

xml
<svg width="300" height="200"
     xmlns="http://www.w3.org/2000/svg">

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

  <circle cx="150" cy="100" r="80" fill="green" />

  <text x="150" y="125" font-size="60" text-anchor="middle" fill="white">SVG</text>

</svg>

Copy the code and paste it in a file, demo1.svg. Then open the file in a browser. It will render as shown in the following screenshot. (Or see it live)

Red background composed of a centered green circle. White text centered inside the circle is SVG.

The rendering process involves the following:

  1. We start with the <svg> root element:

  2. The background is set to red by drawing a rectangle <rect> that covers the complete image area.

  3. A green circle <circle> with a radius of 80px is drawn atop the center of the red rectangle (center of circle offset 150px to the right, and 100px downward from the top left corner).

  4. The text "SVG" is drawn. The interior of each letter is filled in with white. The text is positioned by setting an anchor where we want the midpoint to be: in this case, the midpoint should correspond to the center of the green circle. Fine adjustments can be made to the font size and vertical position to ensure the final result is aesthetically pleasing.

Basic properties of SVG files

SVG file types

SVG files come in two flavors. Normal SVG files are text files containing SVG markup. The recommended filename extension for these files is ".svg" (all lowercase).

Due to the potentially massive size SVG files can reach when used for some applications (e.g., geographical applications), the SVG specification also allows SVG files to be gzip-compressed. The recommended filename extension for these files is ".svgz" (all lowercase). A gzip-compressed SVG loads correctly only when the server sends the correct Content-Encoding header (see the next section), so use .svgz only when you know your web server is configured to serve it correctly.

A word on web servers for .svgz files

Now that you have an idea of how to create basic SVG files, the next stage is to upload them to a web server. There are some gotchas at this stage though. For normal SVG files, servers should send the following HTTP headers:

http
Content-Type: image/svg+xml
Vary: Accept-Encoding

For gzip-compressed SVG files, servers should send the following HTTP headers:

http
Content-Type: image/svg+xml
Content-Encoding: gzip
Vary: Accept-Encoding

You can check that your server is sending the correct HTTP headers with your SVG files by opening your browser's developer tools and inspecting the response headers for one of your SVG files — in Firefox, for example, in the Network Monitor. If you find that your server is not sending the headers with the values shown, then you should contact your web host or, if you manage the server yourself, update its configuration to serve SVG with those headers.

Server misconfiguration is a very common reason for SVG failing to load, so make sure you check yours. If your server is not configured to send the correct headers with the SVG files it serves, then Firefox will most likely show the markup of the files as text or encoded garbage, or even ask the viewer to choose an application to open them.