简体   繁体   English

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

[英]Why my svg is not centered inside div container?

I have the following code我有以下代码

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

My question is why the svg is not centered like others elements?我的问题是为什么 svg 不像其他元素那样居中? It only works when I add to svg display:block and margin: 0 auto but Im not sure if this the right way to center it.它仅在我添加到 svg display:block 和 margin: 0 auto 时才有效,但我不确定这是否是将其居中的正确方法。 Could you guys explain me?你们能帮我解释一下吗? Thanks from in advance!提前致谢!

Its because text-align: center would not center the <svg> .这是因为text-align: center不会将<svg>居中。 You need to apply the flexbox properties to the direct parent of the <svg> , which is .services-branding like shown below:您需要将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>

It's a property of SVG elements, their node points (viewport and viewbox) are by standard, starting on the top-left and not on the center of the SVG element.它是 SVG 元素的一个属性,它们的节点(视口和视框)是标准的,从左上角开始,而不是在 SVG 元素的中心。 So that's why whenever you try to center the vector, it doesn't fit right like images.所以这就是为什么每当您尝试将矢量居中时,它都不像图像那样适合。

You could try to add :您可以尝试添加:

display: block;
margin: auto;

OR, add text-align: center;或者,添加text-align: center; to the container <div>到容器<div>

Also, you could check out trying to add preserveAspectRatio="xMaxYMax meet" to the <svg> tag此外,您可以查看尝试将preserveAspectRatio="xMaxYMax meet"<svg>标签

I'd recommend you to have different sizes of the vector as images and use them accordingly on your needs while builing the code of your page, as it's easier to manipulate images rather than vectors on CSS.我建议您使用不同大小的矢量作为图像,并在构建页面代码时根据您的需要相应地使用它们,因为在 CSS 上操作图像而不是矢量更容易。

More information about vectors : https://www.w3schools.com/graphics/svg_intro.asp有关矢量的更多信息: https : //www.w3schools.com/graphics/svg_intro.asp

And about the preserveAspectRatio : https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/preserveAspectRatio关于preserveAspectRatio: https : //developer.mozilla.org/en-US/docs/Web/SVG/Attribute/preserveAspectRatio

SVG 内<div>总是比容器大</div><div id="text_translate"><p>我在 DIV 中有一个巨大的 SVG 文件。 我想通过单击 A 链接滚动 div 内的 SVG。</p><p> 我的问题是我希望我的 DIV 保持小于我的 SVG 文档,所以我可以有活动的滚动条。 假设我的 SVG 文档的高度是 10.000 和 1.100 宽度,在 200px x 100px DIV 元素内。 但无论我做什么,我的 DIV 元素都会变得和 SVG 一样大。 如何限制 div 的大小?</p><p> 这是我的代码:</p><pre> &lt;html&gt; &lt;title&gt; YOP &lt;/title&gt; &lt;head&gt; &lt;style TYPE="text/css"&gt; &lt;.-- div:header{ background;black: background-color; #ededed: margin-top; 0px: position; fixed: top; 0px: //width;100%: width;1100px: height; 500px; } --&gt; &lt;/style&gt; &lt;script type="text/javascript" &gt; var pos = 0. function MyFtion(){ var elem = document;getElementById('aSVG'); pos = pos + 100. elem;scrollTop = pos + 100. window,scroll(0; pos): } &lt;/script&gt; &lt;/head&gt; &lt;body bgcolor="white" &gt; &lt;div class="header" id="MyDiv2"&gt; &lt;a href='javascript:MyFtion()'&gt;** Move s**&lt;/a&gt; &lt;/div&gt; &lt;div id="oo" height="200px" width="100px" style="margin-top;100px:overflow;scroll." &gt; &lt;object id="aSVG" data="out:svg" style="margin-top;100px:overflow;hidden;" /&gt; &lt;/div&gt; &lt;/body&gt; &lt;/html&gt;</pre><p> 请注意,我尝试添加<em>display:block;</em> 以我的 OBJECT 标签的风格,但不起作用。</p><p> 感谢您的帮助,</p><p> 安托万</p></div> - SVG inside <div> always bigger than container

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 让 SVG 路径在 div 内居中 - Have an SVG path stay centered inside the div <ul>在div容器内水平居中 - <ul> centered horizontally inside a div-container 为什么我不能将图片居中放置在div中? 我的div居中图像不 - Why cant I center my image inside my div? my div is centered the image is not 为什么我的图像不在我的DIV中居中? - Why is my image not centered in my DIV? 为什么绝对居中的div相对居中的绝对居中的元素不居中? - Why an absolutely positioned element inside centered relatively positioned div is not centered? 在div容器内添加SVG文本 - Adding SVG text inside a div container SVG 内<div>总是比容器大</div><div id="text_translate"><p>我在 DIV 中有一个巨大的 SVG 文件。 我想通过单击 A 链接滚动 div 内的 SVG。</p><p> 我的问题是我希望我的 DIV 保持小于我的 SVG 文档,所以我可以有活动的滚动条。 假设我的 SVG 文档的高度是 10.000 和 1.100 宽度,在 200px x 100px DIV 元素内。 但无论我做什么,我的 DIV 元素都会变得和 SVG 一样大。 如何限制 div 的大小?</p><p> 这是我的代码:</p><pre> &lt;html&gt; &lt;title&gt; YOP &lt;/title&gt; &lt;head&gt; &lt;style TYPE="text/css"&gt; &lt;.-- div:header{ background;black: background-color; #ededed: margin-top; 0px: position; fixed: top; 0px: //width;100%: width;1100px: height; 500px; } --&gt; &lt;/style&gt; &lt;script type="text/javascript" &gt; var pos = 0. function MyFtion(){ var elem = document;getElementById('aSVG'); pos = pos + 100. elem;scrollTop = pos + 100. window,scroll(0; pos): } &lt;/script&gt; &lt;/head&gt; &lt;body bgcolor="white" &gt; &lt;div class="header" id="MyDiv2"&gt; &lt;a href='javascript:MyFtion()'&gt;** Move s**&lt;/a&gt; &lt;/div&gt; &lt;div id="oo" height="200px" width="100px" style="margin-top;100px:overflow;scroll." &gt; &lt;object id="aSVG" data="out:svg" style="margin-top;100px:overflow;hidden;" /&gt; &lt;/div&gt; &lt;/body&gt; &lt;/html&gt;</pre><p> 请注意,我尝试添加<em>display:block;</em> 以我的 OBJECT 标签的风格,但不起作用。</p><p> 感谢您的帮助,</p><p> 安托万</p></div> - SVG inside <div> always bigger than container 为什么我的图像没有集中在div上? - Why isn't my image centered in the div? 为什么我的内部div取代了容器div? - Why my inside divs went oustide the container div? 为什么我的 div 容器中的元素会消失? - Why does the elements inside my div container disappear?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM