In this section, you'll learn about the single most basic usage of an SVG image, using it the same way you would use a JPG, PNG, or GIF, as the src
of an img
element. If you've done any work with HTML at all then you will know how to do this since it's just an image element, but you should start to think about all the different ways you can use SVG, and this is a big one.
Looking at the following code sample, there's nothing special at all about the img
element. There's an src
pointing to the SVG image, height
and width
to define the image's dimensions, and an alt
attribute to provide a textual representation of the image for screen readers and other cases where the image may not display:
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>Mastering SVG - Inserting an SVG Image into an HTML Document</title> </head> <body> <img src="1-2-circles.svg" width="250" height="250" alt="an image showing four circles lined up diagonally across the screen"> </body> </html>
Running the preceding code in a browser renders the following:
Note
One thing that might be a slight problem is that not all web servers, by default, set the correct MIME type for SVG. If the MIME type is set incorrectly, some browsers will not display the SVG image correctly. As one common example, Microsoft's IIS may need a specific configuration setting changed (https://docs.microsoft.com/en-us/iis/manage/managing-your-configuration-settings/adding-ie-9-mime-types-to-iis) to properly serve SVG images. The correct MIME type is image/svg+xml.