Is there a syntax that will allow me to reference an inline svg by id in an img tag's src attribute? Or is it possible to echo the svg code directly into the src attribute? I'd prefer to avoid creating a duplicate file and a duplicate HTTP request, if possible.
For a previously loaded svg with id = #my_svg"
, I tried:
src="url(#my_svg)"
.
I also tried printing the svg directly into the src attribute from the server, like this:
<img src="data:image/svg+xml;utf8,
<?php
$svg_str = logo_svg();
$svg_str = str_replace('"', "'", $svg_str);
echo $svg_str;
?>
"
style="position:absolute;width:100%;height:100%">
</img>
Is there a syntax that will allow me to reference an inline svg by id in an img tag's src attribute? Or is it possible to echo the svg code directly into the src attribute? I'd prefer to avoid creating a duplicate file and a duplicate HTTP request, if possible.
For a previously loaded svg with id = #my_svg"
, I tried:
src="url(#my_svg)"
.
I also tried printing the svg directly into the src attribute from the server, like this:
<img src="data:image/svg+xml;utf8,
<?php
$svg_str = logo_svg();
$svg_str = str_replace('"', "'", $svg_str);
echo $svg_str;
?>
"
style="position:absolute;width:100%;height:100%">
</img>
Share
Improve this question
edited Nov 17, 2024 at 7:57
Olivier
18.4k1 gold badge11 silver badges31 bronze badges
asked Nov 16, 2024 at 21:21
fritzfritz
211 silver badge6 bronze badges
5
|
2 Answers
Reset to default 0body {
background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3./2000/svg' width='10' height='10'><linearGradient id='gradient'><stop offset='10%' stop-color='%23F00'/><stop offset='90%' stop-color='%23fcc'/> </linearGradient><rect fill='url(%23gradient)' x='0' y='0' width='100%' height='100%'/></svg>");
}
An example of gradient created in javascript
If you want to avoid this, you can create an assets folder in your GitHub repository and put the svg you want to use, it works well, because some links only work if they are hosted in specific places sometimes, you can create a folder in github with your icons and images, and when you need to use them, you just use the link to your own repository and adjust the size of the icons
</img>
– j08691 Commented Nov 16, 2024 at 22:20