Autosize an SVG?

后端 未结 3 2204
慢半拍i
慢半拍i 2021-02-14 03:03

Here is a code demo

I have a SVG with an unknown height. I can figure it out after I load in json and use javascript+math but is there any css I can use to dynamically s

3条回答
  •  醉话见心
    2021-02-14 03:22

    An alternative is to do similar to the above JS but set viewBox (note, not viewbox!) instead of width and height from JS with values from the getBBox() call. Then use preserveAspectRatio="xMidYMid meet" or similar.

    var svg = document.getElementById("x1");
    svg.setAttribute("viewBox", "0 0 32 18.4");
    

    https://jsfiddle.net/t88pLgbb/4/

提交回复
热议问题