gpt4 book ai didi

html - 为什么我的 svg 没有在 div 容器中居中?

转载 作者:太空宇宙 更新时间:2023-11-03 22:17:10 24 4
gpt4 key购买 nike

我有以下代码

.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 不像其他元素那样居中?它仅在我添加到 svg display:block 和 margin: 0 auto 时有效,但我不确定这是否是居中的正确方法。你们能给我解释一下吗?提前致谢!

最佳答案

因为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>

关于html - 为什么我的 svg 没有在 div 容器中居中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56101115/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com