为什么我的 SVG 不在 div 容器中居中显示?

5
我有以下代码。

.services-container {
    width: 100%;
    height: 300px;
    display: flex;
    justify-content: center;
    align-items: center;
    flex-direction: column;
}

.services-container svg {
    width: 70px;
  /*margin: 0 auto;
    display: block; */

}

.services-branding {
    width: 300px;
}

.services-branding figcaption {
    text-align: center;
}

.services-branding p {
    text-align: center;

}
  <div class="services-container">
        <figure class="services-branding">
          <svg
            xmlns="http://www.w3.org/2000/svg"
            width="120"
            height="124"
            viewBox="0 0 120 124"
          >
            <g>
              <g>
                <g><path fill="#d84848" d="M120 14H56v66h64L99.844 47z" /></g>
                <g><path fill="#aa1f1f" d="M56 66.75V80l17-11z" /></g>
                <g><path fill="#f57b71" d="M73 69H25L6.656 5H73z" /></g>
                <g>
                  <path
                    fill="#daeaf2"
                    d="M4.436.18a5 5 0 0 1 6.123 3.536l30.54 113.98a5 5 0 0 1-9.658 2.587L.9 6.304A5 5 0 0 1 4.435.18z"
                  />
                </g>
              </g>
            </g>
          </svg>
          <figcaption>branding</figcaption>
          <p>
            Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam
            nonummy nibh.
          </p>
        </figure>
      </div>

我想问的是为什么SVG元素不像其他元素一样居中显示呢?只有在添加display:block和margin: 0 auto属性时才能实现居中,但我不确定这是否是正确的居中方式。你们可以解释一下吗?非常感谢!

2个回答

6

这是因为应用 text-align: center 无法将 <svg> 居中。你需要将 flexbox 属性应用于 <svg> 的直接父元素,即 .services-branding,如下所示:

.services-container {
  width: 100%;
  height: 300px;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
}

.services-container svg {
  width: 70px;
}

.services-branding {
  width: 300px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
}

.services-branding figcaption {
  text-align: center;
}

.services-branding p {
  text-align: center;
}
<div class="services-container">
  <figure class="services-branding">
    <svg xmlns="http://www.w3.org/2000/svg" width="120" height="124" viewBox="0 0 120 124">
      <g>
        <g>
          <g><path fill="#d84848" d="M120 14H56v66h64L99.844 47z" /></g>
          <g><path fill="#aa1f1f" d="M56 66.75V80l17-11z" /></g>
          <g><path fill="#f57b71" d="M73 69H25L6.656 5H73z" /></g>
          <g>
            <path
              fill="#daeaf2"
              d="M4.436.18a5 5 0 0 1 6.123 3.536l30.54 113.98a5 5 0 0 1-9.658 2.587L.9 6.304A5 5 0 0 1 4.435.18z"
            />
          </g>
        </g>
      </g>
    </svg>
    <figcaption>branding</figcaption>
    <p>
      Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh.
    </p>
  </figure>
</div>


4

这是SVG元素的一个属性,它们的节点点(视口和视图框)按照标准从左上角开始而不是SVG元素的中心。因此,每当您尝试将矢量居中时,它就无法像图像一样完美适配。

您可以尝试添加:

display: block;
margin: auto;

或者,将text-align: center;添加到容器<div>中。

此外,您可以尝试将preserveAspectRatio="xMaxYMax meet"添加到<svg>标记。

我建议您将矢量图像制作成不同尺寸的图像,并根据需要在构建页面的代码中使用它们,因为在CSS上操作图像比矢量图形更容易。

有关矢量图形的更多信息,请访问:https://www.w3schools.com/graphics/svg_intro.asp

关于preserveAspectRatio的更多信息,请访问:https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/preserveAspectRatio


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接