I have this markup:
#widget1 {
height:100px;
width:200px;
}
<div class="widget" id="widget1">
<object data="foo.svg" type="image/svg+xml" />
</div>
I managed to make the <object>
element fill up the outer <div>
, but the inner foo.svg file has its own ideas how big it is. I need foo.svg (which consists of an <svg>
element, of course) to be the same size as <object>
and <div>
.
Set the CSS attribute position:absolute on your <svg> element to cause it to sit inside and fill your div. (If necessary, also apply left:0; top:0; width:100%; height:100% .)
By default, just like with object-fit , the browser will fit the viewBox inside of the SVG viewport (or “box”) by containing it inside of it, such that the entire viewBox — and, thus, all the contents of the SVG — are visible inside the viewport.
Any height or width you set for the SVG with CSS will override the height and width attributes on the <svg> . So a rule like svg {width: 100%; height: auto;} will cancel out the dimensions and aspect ratio you set in the code, and give you the default height for inline SVG.
We use viewBox to make the SVG image scalable. viewBox = “0 0 100 100”: defines a coordinate system having x=0, y=0, width=100 units and height=100 units. Thus, the SVG containing a rectangle having width=50px and height=50px will fill up the height and width of the SVG image, and all the dimensions get scaled equally.
This is answered (with examples) in the svg primer.
tl;dr summary:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With