Html 强制将内容居中对齐

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17785752/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 11:24:59  来源:igfitidea点击:

Force align a content in center

csshtmlcenter

提问by VarunAgw

I have some content which will contain some divisions, labels, forms in it. I want to force them in the center of page. How can I do this?

我有一些内容,其中将包含一些部门、标签、表格。我想强制它们在页面的中心。我怎样才能做到这一点?

Note: I tried using <div align="center">, <center>but nothing works?

注意:我尝试使用<div align="center"><center>但没有任何效果?

回答by Danield

If your content doesn't have a fixed width do this:

如果您的内容没有固定宽度,请执行以下操作:

<div class="content">
 <div class="inner">some content</div>
</div>


.content
{
 text-align: center;
}
.inner
{
 display:inline-block;
}

If it does, you could do this:

如果是这样,你可以这样做:

.content
{
 width: xpx;
 margin: 0 auto;
}

回答by Done

You must use margin (auto) to center the div but it must have a width.

您必须使用边距(自动)将 div 居中,但它必须具有宽度。

W3School - Center Aligning Using the margin Property

W3School - 使用边距属性居中对齐

回答by Iren Patel

<div style="text-align: center;">testing text align</div> 

回答by Mohammad Mahdi Naderi

Try this:

尝试这个:

div {
    margin:0 auto;
}