svg sizing when in img tag

Hey, so if i have an svg in a file, e.g.
// example.svg
<svg width="10px" height="10px" viewbox="...">
<path></path>
</svg>
// example.svg
<svg width="10px" height="10px" viewbox="...">
<path></path>
</svg>
and i import it into my html with an img tag, if my img tag has display: block on it because of the img reset, meaning it should take up 100% of the container width, why is the size of the img tag when i take a look in dev tools equivalent to the width and height attribute values of the svg it's importing (in this case 10px)? Is this because those attributes inherit over to the img tag and become it's width and height attributes? I always thought the img tag would be 100% of the container due to display: block; and the svg inside it would just be it's own size. I'd appreciate if someone could help clear this up, thanks in advance.
17 Replies
ἔρως
ἔρως2mo ago
if you use <img>, you're not importing anything it's just another image, like jpeg or the nasty webp
snxxwyy
snxxwyyOP2mo ago
Oh okay I see, so it’s turning the svg into an image which means the svgs height and width attributes are the images?
ἔρως
ἔρως2mo ago
no it's the natural width and height basically, if you don't touch it, don't set any size in css or html that's the natural size of the image (some will say "intrinsic size" but that now-a-days has a different meaning)
snxxwyy
snxxwyyOP2mo ago
I don’t really understand why the img isn’t 100% of the container and somehow uses the svgs width and height attributes
ἔρως
ἔρως2mo ago
that has nothing to do with the svg remember that images have a natural size, and setting to block doesn't do what you expect did you set the image to 100%?
snxxwyy
snxxwyyOP2mo ago
How is the natural size determined? I didn’t actually, so block doesn’t set it to 100%?
ἔρως
ἔρως2mo ago
by the image's dimensions for images? i don't think it does images are special
snxxwyy
snxxwyyOP2mo ago
And the images dimensions are usually its width and height attributes? So since the img tag doesn’t have any, it’s natural size is the size of the svg? Oh I didn’t know that haha, so it does for everything else but images?
ἔρως
ἔρως2mo ago
no, it's the naturalWidth and naturalHeight i think so, but don't quote me on that
snxxwyy
snxxwyyOP2mo ago
Oh I see, I’ve never seen those on an image before, I thought it’d just be the images dimensions that size it
ἔρως
ἔρως2mo ago
those are properties that you can only see in javascript or in the debug tools
snxxwyy
snxxwyyOP2mo ago
Oh okay, I see
ἔρως
ἔρως2mo ago
that is what determines the size of an image when you don't set anything
snxxwyy
snxxwyyOP2mo ago
And I assume that’d be the images dimensions by default?
ἔρως
ἔρως2mo ago
yup
snxxwyy
snxxwyyOP2mo ago
Ah this all makes sense now. Understanding these little things really does make other things clearer haha I appreciate the help, thanks
ἔρως
ἔρως2mo ago
you're welcome
Want results from more Discord servers?
Add your server