As @Robert Longson already commented:
No, you can't access external <use>
references in images
This applies to:
- css
background-image
- html
<img>
elements
- svg
<image>
elements
- css pseudo elements using Data URLs within
content
property
From mdn web docs: Data URLs
Note: Data URLs are treated as unique opaque origins by modern
browsers, rather than inheriting the origin of the settings object
responsible for the navigation.
Data URLs can't contain external file references – they will be omitted.
So you can only render self contained elements:
<svg xmlns="http://www.w3.org/2000/svg" width="64" height="16" viewBox="0 0 64 16">
<defs>
<circle id="circle" cx="8" cy="8" r="8" />
</defs>
<use href="#circle" fill="blue" />
<use x="16" href="#circle" fill="red" />
<use x="32" href="#circle" fill="yellow" />
<use x="48" href="#circle" fill="green" />
</svg>
Converted to
.icon {
background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='64' height='16' viewBox='0 0 64 16'%3E%3Cdefs%3E%3Ccircle id='circle' cx='8' cy='8' r='8' /%3E%3C/defs%3E%3Cuse href='%23circle' fill='blue' /%3E%3Cuse x='16' href='%23circle' fill='red' /%3E%3Cuse x='32' href='%23circle' fill='yellow' /%3E%3Cuse x='48' href='%23circle' fill='green' /%3E%3C/svg%3E");
background-repeat: no-repeat;
background-size: auto 100%;
display: block;
width: 4em;
height: 1em;
outline: 1px solid #ccc;
}
would work. However, you won't be able to select or style elements as you can with inlined svgs.
.icon {
background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='64' height='16' viewBox='0 0 64 16'%3E%3Cdefs%3E%3Ccircle id='circle' cx='8' cy='8' r='8' /%3E%3C/defs%3E%3Cuse href='%23circle' fill='blue' /%3E%3Cuse x='16' href='%23circle' fill='red' /%3E%3Cuse x='32' href='%23circle' fill='yellow' /%3E%3Cuse x='48' href='%23circle' fill='green' /%3E%3C/svg%3E");
background-repeat: no-repeat;
background-size: auto 100%;
display: block;
width: 4em;
height: 1em;
}
<div class="icon"></div>
<use>
reference would have to be internal to the image and not to an external file as you have it. – Hathcock