如何设置svg宽度和svg高度百分比?

adi*_*b16 23 css svg

我用svg创建了一行.但是当我调整浏览器大小时,用svg创建的行没有调整大小.

我尝试将svg的宽度设置为百分比,但在此之后不显示该行.我如何设置svg的宽度百分比?

<svg height="210" width="500">
  <line x1="380" y1="20" x2="230" y2="200" style="stroke: rgb(234, 243, 234); stroke-width: 5"></line>
</svg>
Run Code Online (Sandbox Code Playgroud)

adi*_*b16 34

我解决了我的问题.我将我的代码更改为此,这是有效的:

<svg style="width:100%;height:100%;">
  <line x1="100%" y1="0%" x2="0%" y2="100%" style="stroke: rgb(234, 243, 234);stroke-width: 5;"></line>
</svg>
Run Code Online (Sandbox Code Playgroud)


Lar*_*eln 5

强制更新

<svg id="mySVG" height="210" width="500">...</svg>
Run Code Online (Sandbox Code Playgroud)

JS:

var mySVG = document.getElementById("mySVG");
mySVG.setAttribute("width",  window.innerWidth);
mySVG.setAttribute("height", window.innerHeight);
Run Code Online (Sandbox Code Playgroud)

js + jQuery:

$(window).resize(function() {
    $("#mySVG").attr("width",  window.innerWidth);
    $("#mySVG").attr("height", window.innerHeight);
});
Run Code Online (Sandbox Code Playgroud)