Vector graphics are very useful in many circumstances — they have small file sizes and are highly scalable, so they don't pixelate when zoomed in or blown up to a large size. In this article we'll show you how to include one in your webpage.
| Prerequisites: | You should know the basics of HTML and how to insert an image into your document . |
|---|---|
| Objective: | Learn how to embed an SVG (vector) image into a webpage. |
注意: This article doesn't intend to teach you SVG; just what it is, and how to add it to web pages.
On the web, you'll work with two types of images — raster images ,和 vector images :
.bmp
), PNG (
.png
), JPEG (
.jpg
), and GIF (
.gif
)。
To give you an idea of the difference between the two, let's look at an example. You can find this example live on our Github repo as vector-versus-raster.html — it shows two seemingly identical images side by side, of a red star with a black drop shadow. The difference is that the left one is a PNG, and the right one is an SVG image.
The difference becomes apparent when you zoom in the page — the PNG image becomes pixelated as you zoom in because it contains information on where each pixel should be (and what color). When it is zoomed, each pixel is increased in size to fill multiple pixels on screen, so the image starts to look blocky. The vector image however continues to look nice and crisp, because no matter what size it is, the algorithms are used to work out the shapes in the image, with the values being scaled as it gets bigger.
注意: The images above are actually all PNGs — with the left-hand star in each case representing a raster image, and the right-hand star representing a vector image. Again, go to the vector-versus-raster.html demo for a real example!
Moreover, vector image files are much lighter than their raster equivalents, because they only need to hold a handful of algorithms, rather than information on every pixel in the image individually.
SVG
是
XML
-based language for describing vector images. It's basically markup, like HTML, except that you've got many different elements for defining the shapes you want to appear in your image, and the effects you want to apply to those shapes. SVG is for marking up graphics, not content. At the simplest end of the spectrum, you've got elements for creating simple shapes, like
<circle>
and
<rect>
. More advanced SVG features include
<feColorMatrix>
(transform colors using a transformation matrix,)
<animate>
(animate parts of your vector graphic,) and
<mask>
(apply a mask over the top of your image.)
As a simple example, the following code creates a circle and a rectangle:
<svg version="1.1"
baseProfile="full"
width="300" height="200"
xmlns="http://www.w3.org/2000/svg">
<rect width="100%" height="100%" fill="black" />
<circle cx="150" cy="100" r="90" fill="blue" />
</svg>
This creates the following output:
From the example above, you may get the impression that SVG is easy to handcode. Yes, you can handcode simple SVG in a text editor, but for a complex image this quickly starts to get very difficult. For creating SVG images, most people use a vector graphics editor like Inkscape or Illustrator . These packages allow you to create a variety of illustrations using various graphics tools, and create approximations of photos (for example Inkscape's Trace Bitmap feature.)
SVG has some additional advantages besides those described so far:
So why would anyone want to use raster graphics over SVG? Well, SVG does have some disadvantages:
Raster graphics are arguably better for complex precision images such as photos, for the reasons described above.
注意: In Inkscape, save your files as Plain SVG to save space. Also, please refer to this article describing how to prepare SVGs for the Web .
In this section we'll go through the different ways in which you can add SVG vector graphics to your web pages.
img
element
To embed an SVG via an
<img>
element, you just need to reference it in the src attribute as you'd expect. You will need a
height
或
width
attribute (or both if your SVG has no inherent aspect ratio). If you have not already done so, please read
Images in HTML
.
<img
src="equilateral.svg"
alt="triangle with all three sides equal"
height="87"
width="100" />
alt
属性。
<img>
inside an
<a>
元素。
:focus
).
For browsers that don't support SVG (IE 8 and below, Android 2.3 and below), you could reference a PNG or JPG from your
src
attribute and use a
srcset
attribute (which only recent browsers recognize) to reference the SVG. This being the case, only supporting browsers will load the SVG — older browsers will load the PNG instead:
<img src="equilateral.png" alt="triangle with equal sides" srcset="equilateral.svg">
You can also use SVGs as CSS background images, as shown below. In the below code, older browsers will stick with the PNG that they understand, while newer browsers will load the SVG:
background: url("fallback.png") no-repeat center;
background-image: url("image.svg");
background-size: contain;
像
<img>
method described above, inserting SVGs using CSS background images means that the SVG can't be manipulated with JavaScript, and is also subject to the same CSS limitations.
If your SVGs aren't showing up at all, it might be because your server isn't set up properly. If that's the problem, this article will point you in the right direction .
You can also open up the SVG file in a text editor, copy the SVG code, and paste it into your HTML document — this is sometimes called putting your
SVG inline
,或
inlining SVG
. Make sure your SVG code snippet begins with an
<svg>
start tag and ends with an
</svg>
end tag. Here's a very simple example of what you might paste into your document:
<svg width="300" height="200">
<rect width="100%" height="100%" fill="green" />
</svg>
class
es and
id
s to SVG elements and style them with CSS, either within the SVG or wherever you put the CSS style rules for your HTML document. In fact, you can use any
SVG presentation attribute
as a CSS property.
:focus
) and CSS animations on your SVG image (even in your regular stylesheet.)
<a>
元素。
<foreignObject>
element, but browsers that support SVG still download any fallback images. You need to weigh whether the extra overhead is really worthwhile, just to support obsolescent browsers.
iframe
You can open SVG images in your browser just like webpages. So embedding an SVG document with an
<iframe>
is done just like we studied in
From <object> to <iframe> — other embedding technologies
.
Here's a quick review:
<iframe src="triangle.svg" width="500" height="500" sandbox>
<img src="triangle.png" alt="Triangle with three unequal sides" />
</iframe>
This is definitely not the best method to choose:
iframe
s do have a fallback mechanism, as you can see, but browsers only display the fallback if they lack support for
iframe
s altogether.
In this active learning section we'd like you to have a go at playing with some SVG for fun. In the 输入 section below you'll see that we've already provided you with some samples to get you started. You can also go to the SVG Element Reference , find out more details about other toys you can use in SVG, and try those out too. This section is all about practising your research skills, and having some fun.
If you get stuck and can't get your code working, you can always reset it using the 重置 按钮。
This article has provided you with a quick tour of what vector graphics and SVG are, why they are useful to know about, and how to include SVG inside your webpages. It was never intended to be a full guide to learning SVG, just a pointer so you know what SVG is if you meet it in your travels around the Web. So don't worry if you don't feel like you are an SVG expert yet. We've included some links below that might help you if you wish to go and find out more about how it works.
In the last article of this module we will explore responsive images in detail, looking at the tools HTML has to allow you to make your images work better across different devices.
最后修改: , 由 MDN 贡献者