I need to draw an SVG inside a div
and fill it. If the SVG has stroke
set, it's drawn outside the div
.
I see that svg
with width=100%
doesn't take into account the stroke width.
.shape {
border: 1px solid blue;
width: 200px;
height: 200px;
}
svg {
overflow: hidden; // IE11 has 'visible' as default
}
<div class="shape">
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="100%" height="100%" preserveAspectRatio="none" stroke="red" stroke-width="20" viewBox="0,0,100,50">
<polygon points="0,50 100,50 50,0" />
</svg>
</div>
How to make the SVG fill the parent taking into account stroke width?
It should be something like this:
This has to work for any SVG, the triangle is just an example.
With the browser dev tool open in Chrome for example, if I hover over the polygon
, it shows its bounding box taking into account the stroke. I wonder if that's available only to the web-browser or it can be achieved in code too. Or maybe it's not the right direction.
Another thing I tried is use an <img>
and load the SVG into it. Same result.