I have an SVG line (simplified for this example), that should be 100% of the container width, I tried to use two methods to insert an SVG image into my page, but I faced a problem in Safari and Firefox browsers, that SVG doesn’t work correctly with the img tag, it left some space on the left and right side.
How can I fix that?
Interactive example: https://codepen.io/mxddx/pen/zYVWdGz
.wrapper {
width: 100%;
padding: 2px 0px;
background: #090;
}
.svg {
width: 100%;
}
img {
width: 100%;
}
<div class="wrapper">
<svg class="svg" viewBox="0 0 1380 6" fill="none" xmlns="http://www.w3.org/2000/svg">
<rect width="1380" height="6" rx="4" fill="black"/>
</svg>
</div>
<hr/>
<div class="wrapper">
<img src="https://svgshare.com/i/19We.svg" />
</div>
<hr/>
<div class="wrapper">
<!-- No viewBox -->
<img src="https://svgshare.com/i/19XH.svg" />
</div>
I tried to add viewBox, width, and height in different combinations but it’s not help
2
Answers
I tested it on firefox and safari.
display properties of svg
Explain CSS 'display' property applied to SVG elements?
Full width and height SVG
Full width and height SVG
svg in img tag vs inline difference (Animation)
https://gsap.com/community/forums/topic/28388-svg-in-img-tag-vs-inline-difference/v
I can see it happen in both FireFox and Chrome when zooming in/out the page.
In OPs Code Snippet (full page mode), FireFox adds more padding than Chrome:
Is your browser page at 100% ?? press Ctrl-0
maybe working with percentages helps
The width of an open Console also affects it