为什么我的 svg 不在 div 容器内居中?

Pat*_*ski 6 html css svg flexbox

我有以下代码

.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;

}
Run Code Online (Sandbox Code Playgroud)
  <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>
Run Code Online (Sandbox Code Playgroud)

我的问题是为什么 svg 不像其他元素那样居中?它仅在我添加到 svg display:block 和 margin: 0 auto 时才有效,但我不确定这是否是居中的正确方法。你们能给我解释一下吗?提前致谢!

Anu*_*ava 7

这是因为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;
}
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)


小智 5

它是 SVG 元素的一个属性,它们的节点点(视口和视框)按照标准,从左上角开始,而不是从 SVG 元素的中心开始。这就是为什么每当您尝试将矢量居中时,它都不会像图像那样正确放置。

您可以尝试添加:

display: block;
margin: auto;
Run Code Online (Sandbox Code Playgroud)

或者,添加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