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

前端 未结 6 809
感情败类
感情败类 2020-12-08 13:10

I\'m building a portfolio website.

HTML Code

相关标签:
6条回答
  • 2020-12-08 13:31

    You have to modify the viewBox property to change the height and the width correctly with a svg. It is in the <svg> tag of the svg.

    https://developer.mozilla.org/en/docs/Web/SVG/Attribute/viewBox

    0 讨论(0)
  • 2020-12-08 13:35

    I had to figure it out myself but some svgs your need to match the viewBox & width+height in.

    E.g. if it already has width="x" height="y" then =>

    add <svg ... viewBox="0 0 [width] [height]">

    and the opposite.

    After that it will scale with <svg ... style="width: xxx; height: yyy;">

    0 讨论(0)
  • 2020-12-08 13:36
    1. If the svg file has a height and width already defined width="100" height="100" in the svg file then add this x="0px" y="0px" width="100" height="100" viewBox="0 0 100 100" while keeping the already defined width="100" height="100".
    2. Then you can scale the svg in your css file by using a selector in your case img so you could then do this: img{height: 20px; width: 20px;} and the image will scale.
    0 讨论(0)
  • 2020-12-08 13:41

    The transform CSS property lets you rotate, scale, skew, or translate an element.

    So you can easily use the transform: scale(2.5); option to scale 2.5 times for example.

    0 讨论(0)
  • 2020-12-08 13:43

    You can also use the transform: scale("") option.

    0 讨论(0)
  • 2020-12-08 13:48

    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.

    0 讨论(0)
提交回复
热议问题