Apr 13, 2020
Prevent SVG from Overflowing Div Container
Steps to prevent an SVG from overflowing its div container:
- On the
<svg>
element, remove the inlineheight
andwidth
attributes, if present. - On the
<svg>
element, make sure aviewBox
is specified. Example:viewBox="0 0 72 72"
. - On the container div, specify a
width
.
Here’s a CodePen showing the end result: https://codepen.io/kellenmace/pen/vYNOMKW
If you’re using an SVG as an img
element’s src
, or have something more complex going on, check out this Stack Overflow thread.