Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why don’t SVG images scale using the CSS “width” property?

Tags:

html

css

sass

svg

HTML

<div id="hero">    <div id="social">       <img src="facebook.svg">       <img src="linkedin.svg">       <img src="instagram.svg">     </div> </div> 

CSS using SASS

#hero {     display: flex;     flex-direction: column;     justify-content: center;     align-items: center;     height: 300px;          #social {         width: 50%;         display: flex;         justify-content: space-between;         flex-wrap: wrap;          img {             width: 2em;         }     } } 

I’m not able to resize SVGs using the CSS width property. Here is what I obtain with different approaches (note how icons collapse toward the middle of the hero div):

img { width: 2em; }

enter image description here

img { width: 3em; }

enter image description here

img { width: 4em; }

enter image description here

However, if I use the CSS height property:

img { height: 2em; }

enter image description here

img { height: 3em; }

enter image description here

img { height: 4em; }

enter image description here

I get the behaviour I need, but I’m not sure this is the right way. Why does this happen? Do you know better ways of resizeing SVG images (especially using the Flexible Box Module)?

like image 308
Pine Code Avatar asked Aug 20 '16 16:08

Pine Code


People also ask

Can SVG graphics be scaled with CSS?

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.

Can SVG images be scaled?

SVG images can thus be scaled in size without loss of quality, and SVG files can be searched, indexed, scripted, and compressed. The XML text files can be created and edited with text editors or vector graphics editors, and are rendered by the most-used web browsers.

Does SVG have width and height?

Although the SVG element does have height and width attributes, these might not be specified as pixels, but any of a number of units, so they aren't necessarily a lot of help.


1 Answers

SVGs are different than bitmap images such as PNG etc. If an SVG has a viewBox - as yours appear to - then it will be scaled to fit it's defined viewport. It won't directly scale like a PNG would.

So increasing the width of the img won't make the icons any taller if the height is restricted. You'll just end up with the img horizontally centred in a wider box.

I believe your problem is that your SVGs have a fixed height defined in them. Open up the SVG files and make sure they either:

  1. have no width and height defined, or
  2. have width and height both set to "100%".

That should solve your problem. If it doesn't, post one of your SVGs into your question so we can see how it is defined.

like image 197
Paul LeBeau Avatar answered Oct 19 '22 05:10

Paul LeBeau