是的,你可以在SVG使用百分比。
对于基本形状它很简单。你可以写你的主矩形作为
<rect x="0" y="5" width="100%" height="10"/>
你的第二个矩形是更简单,因为它坐落在0,0所有的时间
<rect x="0" y="0" width="10" height="20"/>
但与箭头出现在pathes和多边形的问题你不能使用百分比。要解决此问题,有两步解决方案。
先放路径的符号元素:
<symbol id="arrow" viewBox="0 0 20 20" width="20" height="20">
<path d="M0,0L20 10L0 20z" />
</symbol>
现在你可以放置这个符号就像你放置一个矩形...容易...
<use xlink:href="#arrow" x="100%" y="0" width="20" height="20"/>
,但现在你的箭头开始100%,完全在你的视口之外。你可以设置溢出:在你的svg上可见并且可以完成,但这不是我们想要的......我们希望箭头以100%结束。但是,这也很容易,我们知道箭头宽度为20px。所以,仅仅翻译使用回20像素:使用这种方法
<use xlink:href="#arrow" x="100%" y="0" width="20" height="20" transform="translate(-20 0)"/>
,你可以在百分比上的任何位置基本位置任意形状......
翘曲这一切,你的完整的SVG现在看起来是这样:
<svg id="svg" height="20px" xmlns:xlink="http://www.w3.org/1999/xlink">
<symbol id="arrow" viewBox="0 0 20 20" width="20" height="20">
<path d="M0,0L20 10L0 20z" />
</symbol>
<g id="Group" fill="#5FDCC7">
<rect x="0" y="5" width="100%" height="10" transform="translate(-20 0)" />
<rect x="0" y="0" width="10" height="20" />
<use xlink:href="#arrow" width="20" height="20" x="100%" y="0" transform="translate(-20 0)" />
</g>
</svg>
,这里是使用此SVG用3米不同宽度的片段:
svg:nth-of-type(1) {
width: 100px
}
svg:nth-of-type(2) {
width: 200px
}
svg:nth-of-type(3) {
width: 300px
}
<svg height="20px" xmlns:xlink="http://www.w3.org/1999/xlink">
<symbol id="arrow" viewBox="0 0 20 20" width="20" height="20">
<path d="M0,0L20 10L0 20z" />
</symbol>
<g id="Group" fill="#5FDCC7">
<rect x="0" y="5" width="100%" height="10" transform="translate(-20 0)" />
<rect x="0" y="0" width="10" height="20" />
<use xlink:href="#arrow" width="20" height="20" x="100%" y="0" transform="translate(-20 0)" />
</g>
</svg>
<br/>
<svg height="20px" xmlns:xlink="http://www.w3.org/1999/xlink">
<symbol id="arrow" viewBox="0 0 20 20" width="20" height="20">
<path d="M0,0L20 10L0 20z" />
</symbol>
<g id="Group" fill="#5FDCC7">
<rect x="0" y="5" width="100%" height="10" transform="translate(-20 0)" />
<rect x="0" y="0" width="10" height="20" />
<use xlink:href="#arrow" width="20" height="20" x="100%" y="0" transform="translate(-20 0)" />
</g>
</svg>
<br/>
<svg height="20px" xmlns:xlink="http://www.w3.org/1999/xlink">
<symbol id="arrow" viewBox="0 0 20 20" width="20" height="20">
<path d="M0,0L20 10L0 20z" />
</symbol>
<g id="Group" fill="#5FDCC7">
<rect x="0" y="5" width="100%" height="10" transform="translate(-20 0)" />
<rect x="0" y="0" width="10" height="20" />
<use xlink:href="#arrow" width="20" height="20" x="100%" y="0" transform="translate(-20 0)" />
</g>
</svg>
所以你只想让中间线伸展过来的空间? – Stewartside
是的,正好...(但动态回应,让我们说一个div的宽度) – to7be
好吧给我5分钟 – Stewartside