svg中的水平中心svg路径包含在div容器

时间:2016-09-25 13:15:30

标签: html css svg

你能帮助我理解为什么我的路径拒绝水平居中到svg容器或div中。

好的,如果我尝试水平对齐JUST没有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

1 个答案:

答案 0 :(得分:0)

这样做,例子(推特图标到中间):

<center>
    <div style="width: 40px; height: 40px;">
        <svg class="sqs-svg-icon--social" viewBox="0 0 64 64">
            <use class="sqs-use--icon" xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#twitter-icon">
                <svg id="twitter-icon" viewBox="0 0 64 64" width="100%" height="100%">
                    <path
                        d="M48,22.1c-1.2,0.5-2.4,0.9-3.8,1c1.4-0.8,2.4-2.1,2.9-3.6c-1.3,0.8-2.7,1.3-4.2,1.6 C41.7,19.8,40,19,38.2,19c-3.6,0-6.6,2.9-6.6,6.6c0,0.5,0.1,1,0.2,1.5c-5.5-0.3-10.3-2.9-13.5-6.9c-0.6,1-0.9,2.1-0.9,3.3 c0,2.3,1.2,4.3,2.9,5.5c-1.1,0-2.1-0.3-3-0.8c0,0,0,0.1,0,0.1c0,3.2,2.3,5.8,5.3,6.4c-0.6,0.1-1.1,0.2-1.7,0.2c-0.4,0-0.8,0-1.2-0.1 c0.8,2.6,3.3,4.5,6.1,4.6c-2.2,1.8-5.1,2.8-8.2,2.8c-0.5,0-1.1,0-1.6-0.1c2.9,1.9,6.4,2.9,10.1,2.9c12.1,0,18.7-10,18.7-18.7 c0-0.3,0-0.6,0-0.8C46,24.5,47.1,23.4,48,22.1z"
                        />
                </svg>
            </use>
        </svg>
    </div>
</center>