  [](https://badge.fury.io/js/%40iconfu%2Fsvg-inject)
# SVGInject
A tiny, intuitive, robust, caching solution for injecting SVG files inline into the DOM.
Developed and maintained by [INCORS](http://www.incors.com), the creators of [iconfu.com](https://www.iconfu.com).
## What does it do?
SVGInject replaces an `` element with an inline SVG. The SVG is loaded from the URL specified in the `src` attribute of the `` element.

Element before injection:
```html
```
Element after injection (SVG loaded from image.svg):
```html
```
## Why should I use it?
An SVG can only be properly styled with CSS and accessed with Javascript on element level if the SVG is inline in the DOM. With SVGInject you can keep your SVGs as individual files, but still access them with CSS and Javascript.
## How to install SVGInject?
### Manually
Include the SVGInject Javascript file in the `
` element of the HTML document, or anywhere before the first usage of SVGInject
```html
...
...
```
Download plain version (v1.2.3): [svg-inject.js](https://raw.githubusercontent.com/iconfu/svg-inject/v1.2.3/dist/svg-inject.js)
Download minified version (v1.2.3): [svg-inject.min.js](https://raw.githubusercontent.com/iconfu/svg-inject/v1.2.3/dist/svg-inject.min.js)
### npm
If you are using [npm](https://www.npmjs.com)
```console
$ npm install @iconfu/svg-inject
```
### Yarn
If you are using [Yarn](https://yarnpkg.com)
```console
$ yarn add @iconfu/svg-inject
```
## Basic usage
### Option 1 - Call SVGInject from the `onload` attribute
Add `onload="SVGInject(this)"` to any `` element where you want the SVG to be injected.
For most use cases this approach is recommended and provides nice [advantages](#what-are-additional-advantages-when-using-onload).
```html
```
### Option 2 - Call SVGInject from anywhere
```html
```
**Hooray :tada: - The SVGs get injected and are styleable!!!**
## What are the advantages?
* **Wide browser support**: Works on all browsers supporting SVG. Yes, this includes Internet Explorer 9 and higher! ([full list](https://caniuse.com/#feat=svg))
* **Fallback without Javascript**: If Javascript is not available the SVG will still show. It's just not styleable with CSS.
* **Fallback if image source is not available**: Behaves like a normal `` element if file not found or not available.
* **Prevention of ID conflicts**: IDs in the SVG are made unique before injection to prevent ID conflicts in the DOM.
## What are additional advantages when using `onload`?
The recommended way to trigger injection is to call `SVGInject(this)` inside the `onload` attribute:
```html
```
This provides additional advantages:
* **Intuitive usage**: Insert SVG images into HTML code just as PNG images, with only one additional instruction. It's very clear to understand what it does by looking at the pure HTML.
* **Works with dynamic content**: If `` elements are added dynamically, injection still works.
* **Built-in prevention of unstyled image flash**: SVGInject hides `` elements until injection is complete, thus preventing a brief flicker of the unstyled image (called [unstyled image flash](#how-does-svginject-prevent-unstyled-image-flash)).
* **Early injection**: The injection can already start before the DOM content is fully loaded.
* **Standard-conform**: The `onload` event handler on `` elements has long been supported by all browsers and is officially part of the W3C specification since [HTML5.0](https://www.w3.org/TR/html50/webappapis.html#event-handler-attributes).
If you do not want to use the `onload` attribute but prefer to inject SVGs directly from Javascript, you can find more information [here](#how-to-use-svginject-directly-from-javascript).
## What are the limitations?
SVGInject is intended to work in production environments however it has a few limitations you should be aware of:
* The image source must conform to the [same-origin policy](https://en.wikipedia.org/wiki/Same-origin_policy), which basically means the image origin must be where the website is running. This may be bypassed using the [Cross-Origin Resource Sharing (CORS) mechanism](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS).
* Due to the same-origin policy SVGInject does not work when run from the local file system in many browsers (Chrome, Safari), yet in Firefox it works.
## How are attributes handled?
All attributes are copied from the `` element to the injected `