SVG <style> definitions removed when loaded as component
See original GitHub issueHi,
When I include style definitions in a <style>
tag, the tag including the styles are being removed when loaded as inline svg using the svg loader. Is this an intended behaviour? I would like to include style definitions and deliver them with the svg within the <svg>
tag. Is there a way to preserve the style definitions within the svg?
Sample markup:
logo.svg
file:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 234.86 58.25">
<style>
.dash-grey { fill: #706f6f; }
.dash-rose { fill: #ea719a; }
.dash-teal { fill: #1c9fb2; }
.dash-teal-light { fill: #7ecbd8; }
.dash-yellow { fill: #f7bc03; }
</style>
<g class="icon">
[...]
</g>
</svg>
What is rendered:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 234.86 58.25" class="logo" height="20" data-v-81440b78=""><g class="icon">[...]</g></svg>
Desired rendering:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 234.86 58.25" class="logo" height="20" data-v-81440b78=""><style>
.dash-grey { fill: #706f6f; }
.dash-rose { fill: #ea719a; }
.dash-teal { fill: #1c9fb2; }
.dash-teal-light { fill: #7ecbd8; }
.dash-yellow { fill: #f7bc03; }
</style><g class="icon">[...]</g></svg>
The loaded SVG should include the style defintions from the SVG file.
Thanks for your help.
Issue Analytics
- State:
- Created a year ago
- Comments:11 (2 by maintainers)
Top Results From Across the Web
Hidding SVG affects other SVG styles in the same page
I would put the patterns in a different svg element: <svg class="defs"> . This svg element may have position:absolute and a very small...
Read more >5 Gotchas You're Gonna Face Getting Inline SVG Into ...
My recommendation is to just hide/show via CSS classes (Technique #1 described in Swapping Out SVG Icons article), and be sure to target...
Read more >SVG Style Inheritance and the 'Flash Of Unstyled SVG'
Issue: Oversized SVG icons when the page's CSS fails to load ... style sheets and are overridden by any other style definitions: external ......
Read more ><defs> - SVG: Scalable Vector Graphics - MDN Web Docs
The element is used to store graphical objects that will be used at a later time. Objects created inside a element are not...
Read more >Appendix K: Changes from SVG 1.1
Remove basic data types already defined by CSS Values and Units. Remove list of color keywords. The list is part of CSS Colors...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
By default, SVGO does not strip classes which are used more than once, as it increases the output size. But it’s needed to be able to apply the styles, because Vue strips the style tag away. Thats why I needed to set the
onlyMatchedOnce
option tofalse
.Maybe it would be worth to include this snippet in the project README.md?
Also had an issue similar to this where the viewbox attribute gets stripped off from some svgs. Fixed it with the properties below: