How to host an SVG file

A vector image is markup, not a picture file, and that is both why it scales perfectly and why hosts are careful with it.

A vector image is markup. It scales perfectly because it describes shapes rather than pixels, and it is treated carefully by hosts because markup can contain code.

An inlined vector icon changing colour with CSS beside one loaded as an image that does not.
An inlined vector icon changing colour with CSS beside one loaded as an image that does not.

This guide covers what to strip, how to serve it, and when to inline.

It can contain code

An SVG can include scripts, event handlers, external references and embedded content.

Served inline from your domain, any of that executes with the same privileges as your own page. That is why many hosts serve these files with headers that force a download rather than rendering, and why uploading one from an unknown source is a genuine risk rather than a theoretical one.

Before publishing any vector file you did not produce, strip it.

Scripts. Event handler attributes. External references. Embedded objects. There are tools that do this, and for a small file it can be read by eye.

Strip the metadata too

Vector files exported from design software carry a surprising amount of editor state.

Layer names, editor namespaces, comments, and coordinates written to many decimal places. None of it affects the image and it frequently accounts for half the file size.

Optimising removes it. On a typical icon that takes a file from several kilobytes to under one, with no visible change.

Raw export Optimised
Editor metadata Present Removed
Coordinate precision Excessive Reduced
Typical size Several KB Under 1KB
Visible difference None

The content type

Content-Type: image/svg+xml

Served as anything else, it downloads instead of rendering. Most hosts get this right by extension; some do not.

If a vector file downloads when you open its address, this is why.

Inline it for icons

An icon inlined directly in the page has three advantages.

No request. One fewer thing to fetch and fail.

Styles reach it. The shapes are part of the document, so CSS can change their colour, size and state.

Nothing external. The page is self-contained and behaves identically wherever it is opened.

This is the main practical reason to inline rather than link: a vector loaded through an image tag is isolated from your styles, and colour changes simply do not apply. That surprises people repeatedly.

The same icon before and after optimisation, with file sizes shown.
The same icon before and after optimisation, with file sizes shown.

Linking is right for large graphics

An illustration used on several pages is worth loading as a file, cached once and reused.

Inlining a large vector into every page repeats its markup on each one, which is worse than one cached request.

The rule is the same as for images generally: small and structural, inline it. Large and reused, load it.

For the surrounding ground, see SVG in HTML: diagrams that scale and follow your colours and HTML SVG change color.

Put it at an address

Strip scripts and external references, remove editor metadata, serve it as image/svg+xml, inline icons so styles can reach them, and never serve an untrusted vector file from your own domain.

Then the image scales perfectly and is not carrying anything you did not intend.

Questions people ask

Why do some hosts refuse to serve SVG inline?

Because the format can contain scripts and external references. Served inline from your domain, that code runs with your page's privileges, so many hosts force a download instead.

What should I strip from one?

Scripts, event handlers, external references and embedded objects. Also editor metadata, which frequently doubles the file size for no benefit.

Should I inline it in the page instead?

For icons and small graphics, yes. It removes a request, lets styles reach the shapes, and there is nothing external to break.

Why is the file so large?

Editor metadata and excessive coordinate precision. Optimising typically removes half the file with no visible change.

Can I change its colour with CSS?

Only when it is inlined or used as a mask. An SVG loaded through an image tag is isolated from your styles, which is the usual reason colour changes appear not to work.

Keep reading