Skip to content Skip to sidebar Skip to footer

SVG Without Width/height Renders With A Natural Size

I have this SVG which doesn't have a width or height attribute I have the following HTML

Solution 1:

If you want auto width on the <svg> elements, you should simply use display: block on the parent <div>, but use max-width: 100% on the SVG elements themselves. Chrome somehow fails to enforce proper width calculations when SVGs are contained within an inline-block element:

.block {
  display: block;
}

img {
  max-width: 100%;
}

Proof-of-concept example:

section {
  border: 2px solid green;
  margin: 20px;
  background-color: yellow;
  width: 500px;
  height: 500px;
}

.block {
  display: block;
}

img {
  max-width: 100%;
}

.svg {
   border: 1px solid red; 
}
<section>
   <div class="block svg">
       <img src="https://s3-eu-west-1.amazonaws.com/uploads-eu.hipchat.com/46194/456229/JCHA4rtvAmfIxVv/kiwi.svg" alt="">
   </div>

   <div class="block">
      <img src="http://placeholder.pics/svg/100x200" alt="">
   </div>
</section>

Post a Comment for "SVG Without Width/height Renders With A Natural Size"