admin管理员组文章数量:1435859
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
本文标签: htmlreferencing an inline svg by id inside an img src attributeStack Overflow
版权声明:本文标题:html - referencing an inline svg by id inside an img src attribute - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745647846a2668243.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
</img>
– j08691 Commented Nov 16, 2024 at 22:20