<our-svg>
<our-svg> reads an SVG and writes it into the page as an inline <svg>, so you can style it with CSS. The SVG can be a static file in wwwroot (src) or a media item picked in Umbraco (media-item).
<our-svg src="/assets/icon.svg" /><our-svg media-item="@Model.Logo" />Options
Section titled “Options”<our-svg src="/assets/icon.svg" class="my-css-class" ensure-viewbox="true" cache="true" cache-minutes="180" ignore-appsettings="true" /><our-svg media-item="@Model.Logo" class="my-css-class" ensure-viewbox="true" cache="true" cache-minutes="180" ignore-appsettings="true" />| Attribute | Type | What it does |
|---|---|---|
src |
string |
The path to an SVG file in wwwroot. |
media-item |
IPublishedContent |
A media item whose file is an SVG. |
class |
string |
A CSS class for the <svg>. |
ensure-viewbox Added in v1.0.0 |
bool |
Replaces the SVG’s width and height with a viewBox, so it scales with its container. |
cache Added in v1.0.0 |
bool |
Keeps the file’s contents in Umbraco’s runtime cache, so it isn’t read on every request. Wins over the global Cache setting, so cache="false" turns caching off for this tag. See When is an SVG cached? |
cache-minutes Added in v1.0.0 |
int |
How long a cached SVG is kept. Without it, the global CacheMinutes is used (180 by default). |
scope-styles Coming in v2.1.0 |
bool |
Scopes the SVG’s <style> blocks to this SVG, so class names such as .st0 from two SVGs on one page don’t restyle each other. See Scoped styles. |
unique-ids Coming in v2.1.0 |
bool |
Gives every id in the SVG a prefix of its own for this tag, and updates the references to them, so SVGs on one page don’t share gradients, clip paths, masks or filters. See Unique ids. |
ignore-appsettings Added in v1.0.0 |
bool |
Ignores the global EnsureViewBox, Cache, ScopeStyles and UniqueIds settings for this tag, so only its own attributes apply. |
| Any other attribute Coming in v2.1.0 | Copied onto the <svg>, replacing the file’s own value. |
Other attributes such as aria-hidden, aria-label, role, id, data-*, width or height reach the inlined <svg>. Use aria-hidden="true" for a decorative icon, or role="img" aria-label="Company logo" for one that means something:
<our-svg src="/assets/icon.svg" aria-hidden="true" /><our-svg media-item="@Model.Logo" role="img" aria-label="Company logo" />Scoped styles
Section titled “Scoped styles”Design tools such as Illustrator export SVGs with generic class names in an inline <style>, such as <style>.st0{fill:#3544b1}</style>. Once inlined, those rules apply to the whole page, so when two SVGs both define .st0, the last rule wins for both and one is recoloured.
With scope-styles="true" (or "ScopeStyles": true in the global settings), the <svg> gets a class made from its file path or media key, such as svg-1a2b3c4d5e6f, and every selector in its <style> blocks is prefixed with it:
<!-- In the file --><svg><style>.st0{fill:#3544b1}</style>...</svg>
<!-- Inlined with scope-styles="true" --><svg class="svg-1a2b3c4d5e6f"><style>.svg-1a2b3c4d5e6f .st0{fill:#3544b1}</style>...</svg>- Rules inside
@media,@supports,@containerand@layerare scoped too;@keyframes,@font-faceand other at-rules are left as they are. - Selectors for the SVG itself (
svg,svg .a,:root) target that<svg>. - Comma-separated selectors are each prefixed, and comments, strings and a
<![CDATA[ ]]>wrapper are kept. - The same file always gets the same class, so the output is the same on every render.
- SVGs without a
<style>are inlined as they are, with no class added.
This is based on the Umbraco Community site’s scoping inline SVG styles.
Unique ids
Section titled “Unique ids”SVGs often give gradients, clip paths, masks and filters an id and refer to them with url(#id) or href="#id". Inlined into a page, those ids are shared with everything else on it: the same SVG twice gives duplicate ids, and two SVGs exported with the same ids can pick up each other’s gradients.
With unique-ids="true" (or "UniqueIds": true in the global settings), each <our-svg> prefixes the SVG’s ids with a short random prefix of its own, such as s1a2b3c4d-gradient, and updates:
url(#id)in any attribute (fill,clip-path,mask,filter,style…) and in<style>,href="#id"andxlink:href="#id",- id references in
aria-labelledby,aria-describedbyand the other ARIA id attributes, #idselectors in<style>.
Only ids the SVG defines are changed, so colours such as #fff and links to ids elsewhere on the page stay as they are. An id you put on the <our-svg> tag itself is kept as written.
scope-styles and unique-ids can be used together: the selectors are scoped, and the ids in them are prefixed.
When is an SVG cached?
Section titled “When is an SVG cached?”cache on the tag always decides when it’s there. When it isn’t, the global Cache setting decides, unless the tag has ignore-appsettings="true", in which case the SVG isn’t cached.
cache on the tag |
ignore-appsettings on the tag |
Global Cache |
Cached? |
|---|---|---|---|
| not set | not set | false |
no |
| not set | not set | true |
yes |
| not set | true |
false |
no |
| not set | true |
true |
no |
true |
not set | false |
yes |
true |
not set | true |
yes |
true |
true |
false |
yes |
true |
true |
true |
yes |
false |
not set | false |
no |
false |
not set | true |
no |
false |
true |
false |
no |
false |
true |
true |
no |
Only the file’s contents are cached, so class, ensure-viewbox, scope-styles and unique-ids still apply to each tag separately, even when several tags share one cached file.
Cached SVGs from the media library (media-item) are cleared when media is saved or changed, on every server of a load-balanced site. SVGs from wwwroot (src) stay cached until cache-minutes runs out or the site restarts.
What’s removed from the SVG
Section titled “What’s removed from the SVG”The SVG is inlined into the page, so anything in it that could run script is removed first:
<script>,<foreignObject>,<iframe>,<embed>and<object>elements,- event handler attributes such as
onloadandonclick, - links that aren’t internal (
#…), web (http,https,mailto, relative) or, for images, image references, such asjavascript:anddata:text/html, <set>and<animate>elements that change a link or an event handler.
Everything else, including styles, gradients, internal references and animations, is kept as it is.
Global settings
Section titled “Global settings”Set defaults for every <our-svg> in appsettings.json. The values shown are the defaults. See Configuration.
{ "Our.Umbraco.TagHelpers": { "OurSVG": { "EnsureViewBox": false, "Cache": false, "CacheMinutes": 180, "ScopeStyles": false, "UniqueIds": false } }}