Html 如何使用css将内联div居中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3654399/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to center an inline div using css?
提问by Mihai Om
How can I center an inline div (not its content) using css?
如何使用 css 将内联 div(而不是其内容)居中?
VERY IMPORTANT: the width of the div is unknown (I can not specify it)
非常重要:div 的宽度未知(我无法指定)
回答by Moin Zaman
If by inline you mean, its CSS display property is set to inline
then you can center it by giving the immediate parent container a text-align:center
如果你的意思是内联,它的 CSS 显示属性设置为,inline
那么你可以通过给直接父容器一个text-align:center
If you mean a div within another div, you can follow this approach if you can add a wrapper and float both:
如果您的意思是另一个 div 中的一个 div,那么如果您可以添加一个包装器并同时浮动两者,则可以遵循此方法:
How to horizontally center a floating element of a variable width?
回答by katum
I solved this.
You should add new div
with text-align:center
outside your div
like this
我解决了这个问题。您应该添加新的div
与text-align:center
您的外div
像这样
<div style="text-align:center;"> <div>
<div style="text-align:center;"> <div>
which not in center with inline display </div></div>
与内嵌显示不在中心 </div></div>
回答by Guy Sopher
This is the easiest and most straightforward solution
这是最简单和最直接的解决方案
.center {
display: table;
margin: auto;
}
回答by mkoistinen
Wrap it into a div that you can specify the width of and then set the margins of the div in question to: margin: 0, auto;
将其包装到一个 div 中,您可以指定其宽度,然后将相关 div 的边距设置为:margin: 0, auto;
回答by meder omuraliev
you can probably get away with display:inline-block
and text-align:center;
on the parent element if you want it it to be "blocky". also you'll probably need to give it a vertical-align:top
如果您希望它是“块状”,您可能可以在父元素上使用display:inline-block
和text-align:center;
。你也可能需要给它一个vertical-align:top