admin管理员组文章数量:1026630
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
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
-
1
Side note, there's no such thing as
</img>
– j08691 Commented Nov 16, 2024 at 22:20 - 1 images are complete files, you don't refer to them by id. If you want to reference something by id we have a different tag for that i.e. <use> – Robert Longson Commented Nov 17, 2024 at 0:09
- 1 you also need to escape the # characters in a Data-URI – Danny '365CSI' Engelman Commented Nov 17, 2024 at 9:19
- Note: If you load a second IMG with the same src your browser will fetch it from the cache, so there is no duplicate HTTP request – Danny '365CSI' Engelman Commented Nov 17, 2024 at 11:22
- Provided your svg image doesn't contain any ID references (like clip-path, gradients etc) that may collide with other element ID you could just echo the SVG markup directly. So you get a inlined SVG element in your HTML. – herrstrietzel Commented Nov 17, 2024 at 21:33
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://it.en369.cn/questions/1745647845a2161132.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
</img>
– j08691 Commented Nov 16, 2024 at 22:20