How do I scale a stubborn SVG embedded with the <object> tag?

前端 未结 9 1309
眼角桃花
眼角桃花 2020-11-28 02:03

I have some SVG files that specifies width and height as well as viewbox like this:



        
9条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-28 02:12

    You can reach into the embedded svg using JavaScript:

    var svg = document.getElementsByTagName('object')[0].\
      contentDocument.getElementsByTagName('svg')[0];
    svg.removeAttribute('width');
    svg.removeAttribute('height');
    

    Since your svg already has a viewBox, Firefox should scale the 576 pixel width in the viewBox to the 400 pixel width in your document. Other svgs might benefit from a new viewBox derived from the advertised width and height (these are often the same numbers). Other browsers might benefit from different svg tweaks.

提交回复
热议问题