【发布时间】:2017-02-02 21:02:07
【问题描述】:
你能帮我理解为什么我的路径拒绝水平居中到 svg 容器或 div 中。
好的,如果我尝试水平对齐没有 rect svg 的路径,如果 svg 容器是 100% 宽度,我可以这样做。但是,如果我想添加 rect 并且只使用静态的 div 宽度和高度,我永远找不到路径中心的解决方案。
html:
<div id="svg-container">
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="100%" height="100%" viewBox="0 0 1200 1200">
<rect id="background" width="100%" height="100%" fill="grey"/>
<path id="path" xmlns="http://www.w3.org/2000/svg"
d="M 201.68015,653.12212 C 189.21313,655.67886 177.65942,662.51955 166.63177,673.41648 C 155.59034,684.51344 91 L 310.26302 176.06855,451.96604 z etc................ "
style="fill:#131516;fill-rule:evenodd;stroke:#131516;stroke-width:0.60229105" />
</svg>
</div>
还有CSS:
#svg-container
{
height: 250px;
width: 250px;
}
#path
{
display: block;
position: relative;
left: 30%;
}
JSFiddle : https://jsfiddle.net/170jkfLr/2/#&togetherjs=w9w2vhjhEJ
【问题讨论】:
-
可能是因为它遵循路径数据中的坐标。你可以考虑transforming the coordinates或nesting。
-
另一位领导SVG positioning