You can include SVG files into your HTML file via embed
object
and svg
tags.
- Using an
embed
orobject
tag requires linking the image with a URL. (This is what I prefer, because I don't like all that SVG code in my HTML code, so I’d like to keep it that way.) - Using the
svg
tag (AFAIK) requires having the SVG code inline in your HTML code.
My question is:
How do I include SVG icons, images, and other files into my HTML file, without having to put the entire SVG code in, and still be able to apply styles to them? Applying them through JS is fine too.
Note:
When I include them via object
or embed
, I can't seem to access them through jQuery, even with $("#my-svg-div").find("svg")
(which, by the way, almost every answer on SO says I should). I just get undefined
.
Thank you!
This is the most comprehensive walkthrough of (1) the different ways of using SVGs in your HTML and (2) what styling the individual pieces (ie: paths) of the SVG via CSS/JS you can do.
https://css-tricks.com/using-svg/
In an image tag (ie:
<img src="picture.svg" />
or as a background image in CSS = no stylingInline - style away, but it'll clutter your HTML. PHP helps here, or you could use a gulp build task or something to keep the SVGs from cluttering your working code, while still being inline in the end.
As an object you can now add CSS within the .svg file:
or
Data URIs - can be great for background images. No styling.
Short Answer
You can programmatically inline an SVG image. Such an image can be treated essentially identically to an actual inline
<svg>
element, including being able to have styles applied to it.If your SVG image is referenced in an
<object>
or<iframe>
element (e
), you can inline it as follows:e.parentElement.replaceChild(e.contentDocument.documentElement.cloneNode(true), e);
If your SVG image is referenced in an
<embed>
element, replace.contentDocument
in the above code with.getSVGDocument()
.If your SVG image is referenced in an
<img>
element, a completely different strategy, involving AJAX and described below, can be used to inline the image.General Strategy
If your external SVG image files are same-origin (e.g. the images are loaded from the same place as the HTML code), then one approach that allows styling of these images is to programmatically inline them as follows:
Benefits
This inlining strategy gives you the best of two worlds:
You get the benefits of separate image files, including:
Yet you can still do anything to the eventually-inlined SVG images that you could do to
<svg>
elements that were truly originally inline, including:Implementation
For
<object>
or<iframe>
elements:You can inline externally referenced SVG code as follows:
...where
e
or$e
are the vanilla or jQuery variables (respectively) in which you have selected an external-SVG-referencing<object>
or<iframe>
element.To include
<embed>
elements:If, instead, you are using an external-SVG-referencing
<embed>
element, then you can inline the SVG content by replacing.contentDocument
in the above code with.getSVGDocument()
(note the additional parentheses). Note that.contentDocument
does not work with<embed>
elements while.getSVGDocument()
actually works with all three element types. However.getSVGDocument()
is deprecated and so should only be used if you really need<embed>
elements.To include
<img>
elements:Neither of the above strategies works for
<img>
elements. To inline these, you can retrieve thesrc
attribute of the<img>
element, make an AJAX request for that file, create a new<svg>
element using the retrieved SVG code and replace the original<img>
element with the new<svg>
element. If you want this strategy to work for all four element types, then just be aware that the URL of the referenced SVG image is held in thesrc
attribute of<iframe>
,<embed>
and<img>
elements but in thedata
attribute of an<object>
element. This strategy can be implemented as follows:Example
The following example demonstrates where the above strategy does and does not allow external CSS stylings to be applied to originally-external SVG images. (I didn't create a code snippet or jsfiddle because of the need to reference local external files.)
The following two screenshots show the CSS styling (red triangles) or lack thereof (black triangles) before and after inlining. It shows the results for SVG images originally embedded in the HTML (
<svg>
) or referenced in the indicated elements (<object>
,<iframe>
,<embed>
, and<img>
). The three rows show the results of inlining using the three indicated strategies.Before clicking the button, no inlining has yet been attempted, and the screen looks like this. Only embedded SVG elements (the 1st column) are styled:
After clicking the button, inlining is attempted, and the screen now looks like this. CSS styling has been successfully applied to some of the elements:
The code required for this example follows:
image.svg
(externally referenced file, i.e. not embedded in the HTML):index.html
(obviously, remove the jQuery script line if not using jQuery):styles.css
(only thepolygon
line is important for demonstrating the inlining):main.js
(jQuery version):main.js
(vanilla JavaScript version):Note the following:
id
andclass
(or any other) attributes, event listeners,iframe
functionality, etc.object
,iframe
,embed
, andimg
elements. It does not address external SVG images referenced in CSSbackground-image
properties or in thedrawImage
function of a<canvas>
context. I suspect those won't work this way, but I haven't checked.index.html
, Chrome and Opera have security settings that will make this impossible. To get those browsers to work in such a local configuration, you instead need to run a local server as instructed in this other SO question. There should be no such problem if you are using the inlining strategy in a normal server-hosted web site.