Scale SVG to container without mask/crop

后端 未结 3 1621
天命终不由人
天命终不由人 2020-12-04 09:46

I have tried many variants of the svg parameters, but have had no joy in scaling this particular SVG.

I am trying to contain this SVG into a container e

3条回答
  •  猫巷女王i
    2020-12-04 10:29

    Set the SVG width and height to be the size of its container, and set preserveAspectRatio = none.

    and

    $("svg").each(function(){
      this.width = this.parentNode.width;
      this.height = this.parentNode.height;
    }
    

    That's it. Setting CSS is not needed.

    I personally set viewBox to be the size of the contents of the SVG. So in my example, the original image I am loading into my SVG is 300x200. It will shrink to fit a 50x100 div. But viewBox manipulation is a separate issue.

提交回复
热议问题