繁体   English   中英

根据容器的高度缩放线 svg

[英]Scale an line svg as per height of the container

我正在制作一个 svg(一个线元素)并且它是垂直的。 我希望该 svg 行根据容器的高度进行缩放。

<div class="parent">
    <svg viewBox="0 0 100 100">
       <line x1="0" y1="0" x2="0" y2="20" style="stroke: black;stroke-width:1"/>
    </svg>
</div>

现在如果增加父宽度垂直线比例的高度,但要求是,随着我们增加高度,长度应该成比例地增加。 我把viewBox放错了吗?

我认为您正在寻找vector-effect属性。 如果将其设置non-scaling-stroke - 这将使您能够将线条的笔触保持在 1 像素,而与 svg 的大小无关。 看看下面的这个例子,我已经设置了 svg 来填充父级:

 .parent { background: #efefef; width: 200px; height: 200px; } svg { width: 100%; height: 100%; }
 <div class="parent"> <svg viewBox="0 0 100 100"> <line x1="0" y1="0" x2="0" y2="100" style="stroke: black;stroke-width:1" vector-effect="non-scaling-stroke"/> </svg> </div>

尝试更改周围父级的大小,您会注意到笔画宽度始终保持一致。

按比例缩放 SVG:

 .parent { background: yellow; } .parent svg { height: 100%; }
 100px height <div class="parent" style="height: 100px;"> <svg viewBox="0 0 2 100" preserveAspectRatio="none"> <line x1="0" y1="0" x2="0" y2="100" style="stroke: black; stroke-width:2"/> </svg> </div> 300px height <div class="parent" style="height: 300px;"> <svg viewBox="0 0 2 100" preserveAspectRatio="none"> <line x1="0" y1="0" x2="0" y2="100" style="stroke: black; stroke-width:2"/> </svg> </div>

缩放 SVG 但保持笔画

 .parent { background: yellow; } .parent svg { height: 100%; }
 100px height <div class="parent" style="height: 100px;"> <svg viewBox="0 0 2 100" preserveAspectRatio="none"> <line x1="0" y1="0" x2="0" y2="100" style="stroke: black; stroke-width:2" vector-effect="non-scaling-stroke"/> </svg> </div> 300px height <div class="parent" style="height: 300px;"> <svg viewBox="0 0 2 100" preserveAspectRatio="none"> <line x1="0" y1="0" x2="0" y2="100" style="stroke: black; stroke-width:2" vector-effect="non-scaling-stroke"/> </svg> </div>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM