My question here is, as by the title: is it possible to make an embedded SVG image or object behaving like an inline one and therefore fully stretching out and/or shrinking within to its container size?
I already saw here and several Q&A here on Stackoverflow but can't seem to find a "real" answer to this...
Here's the sample code I worked out:
/*SOME RESET RULES FIRST*/
*, ::before, ::after {
box-sizing: border-box;
}
body {
line-height: 1.5;
}
img {
max-width: 100%;
display: block;
}
/*CUSTOM RULES NOW*/
div.gcnt{
position: relative;
display: inline-block;
min-height: 50px;
max-height: 200px;
min-width: 100px;
max-width: 300px;
width: 20%; height: 30%;
border: 1px dashed greenyellow;
}
div.gcnt.inline svg, div.gcnt.ii img, div.gcnt.oi object {
position: absolute;
width: 100%; height: 100%;
}
<div class="gcnt inline"><svg xmlns="http://www.w3.org/2000/svg" viewbox="0 0 100 100" preserveAspectRatio="none"><circle cx="50%" cy="50%" r="40" stroke="black" stroke-width="1.2" fill="darkseagreen"/></svg></div>
inline SVG: actually works great!
<hr>
<div class="gcnt ii"><img src="a.svg" class="svgi"></div>
img embedded SVG: doesn't do
<hr>
<div class="gcnt oi"><object id="svg1" class="svgo" data="a.svg" type="image/svg+xml"></object></div>
object embedded SVG: neither does
The inline svg
works good, while using the img
or object
tags to include it don't do instead (despite a.svg contains exactly the same code used for inline svg).
Here's a screenshot to show out what happens:
To be clear, I don't want to use background-image
style rule for this.
Any idea to achieve my purpose here? I'd really appreciate your help, thank you in advance
Oh, btw, I don't care supporting MsIE
EDIT:
Coming to a conclusion, however embarrassing, it ended up I was duped by a paragraph heading title in the article I linked on top of this question.
(This is how it looks atm I'm writing: )
As @RobertLongson pointed out in his comment here below: despite it working well using an inline SVG element, it won't when using external SVGs (included in the web page through <img>
or <object>
tags) and the reason of the problem is because that the attribute name is case sensitive and the right case must be respected to make it work correctly.
In my a.svg
contents I had it wrongly written as viewbox instead of viewBox
(as it should be).
Simply changing that to the correct case makes it all working good as expected, so... this is the definitely the answer in this case.