Using SVG currentColor in an HTML img Tag
When an SVG file is embedded via an HTML <img>
tag, the currentColor keyword does not inherit CSS values
from the parent document and usually defaults to black
(#000000). This occurs because the <img>
element renders the vector file inside an isolated browsing context that
blocks external CSS cascade and inheritance. This guide explains why
this isolation occurs, how currentColor resolves within
that boundary, and the best workarounds for dynamically coloring
SVGs.
The Document Boundary and Style Isolation
The HTML <img> tag treats SVG files as static
external resources, similar to PNG or JPEG images. For security and
architectural reasons, modern browsers sandbox images in an isolated
document tree.
Because of this boundary: - Styles applied to the host HTML document
(including color, fill, and class
declarations) cannot pierce the <img> boundary. - The
SVG document cannot read the computed color property of the
<img> tag or its parent container. - External
stylesheets linked in the main HTML document are completely ignored by
the SVG.
How currentColor Resolves in an img Tag
The currentColor keyword represents the value of the
color property on the element itself or inherited from its
nearest ancestor.
Inside an <img> tag: 1. Without Internal
Styles: If the SVG does not define a color
property within its own <style> tag or root
<svg> element, the browser assigns the default canvas
text color, which is solid black (#000000). Consequently,
any fill="currentColor" or
stroke="currentColor" rule resolves directly to black. 2.
With Internal Styles: If the SVG file includes an
internal stylesheet (such as
<svg style="color: blue;">),
currentColor will resolve to that internally declared value
(blue). However, this value remains static and cannot be
modified by the parent HTML page.
Alternatives for Dynamic SVG Coloring
To dynamically change the color of an SVG using CSS or
currentColor, alternative embedding techniques must be used
instead of the standard <img> tag.
1. Inlining the SVG Element
Embedding the raw <svg> code directly into the
HTML document allows full access to the CSS cascade.
<div style="color: #e63946;">
<svg viewBox="0 0 24 24" width="24" height="24">
<circle cx="12" cy="12" r="10" fill="currentColor" />
</svg>
</div>In this scenario, currentColor successfully inherits the
#e63946 value from the parent <div>.
2. CSS Masking
(mask-image)
If you prefer keeping the SVG in a separate file, you can apply the
SVG as a CSS mask over an element whose background color is set to
currentColor.
<span class="icon-mask"></span>.icon-mask {
display: inline-block;
width: 24px;
height: 24px;
background-color: currentColor;
-webkit-mask-image: url('icon.svg');
mask-image: url('icon.svg');
-webkit-mask-repeat: no-repeat;
mask-repeat: no-repeat;
-webkit-mask-size: contain;
mask-size: contain;
}This method retains the separation of files while allowing the icon to inherit the surrounding text color dynamically.
3. SVG Use Element
(<use>)
You can define an SVG sprite sheet in the document or load an
external symbol via the <use> tag:
<svg class="icon" fill="currentColor">
<use href="#my-icon-id"></use>
</svg>This approach retains external asset caching benefits while enabling direct CSS inheritance.