我想用一个非常基本的例子。使用入门页面和网格系统,我希望如下:
< div class = "行" > < div class = " span12”> <h1>引导启动器模板</h1> < / p > < p >示例文本。 < / div > < / div >
...会产生居中的文本。
然而,它仍然出现在最左边。我做错了什么?
我想用一个非常基本的例子。使用入门页面和网格系统,我希望如下:
< div class = "行" > < div class = " span12”> <h1>引导启动器模板</h1> < / p > < p >示例文本。 < / div > < / div >
...会产生居中的文本。
然而,它仍然出现在最左边。我做错了什么?
当前回答
最好的方法是定义一个css样式:
.centered-text {
text-align:center
}
然后在你需要居中文本的地方,像这样添加它:
<div class="row">
<div class="span12 centered-text">
<h1>Bootstrap starter template</h1>
<p>Use this document as a way to quick start any new project.<br> All you get is this message and a barebones HTML document.</p>
</div>
</div>
或者如果你只想让p标签居中:
<div class="row">
<div class="span12 centered-text">
<h1>Bootstrap starter template</h1>
<p class="centered-text">Use this document as a way to quick start any new project.<br> All you get is this message and a barebones HTML document.</p>
</div>
</div>
你使用的内联css越少越好。
其他回答
如果你使用Bootstrap 3,它也有内置的CSS类命名为.text-center。这就是你想要的。
<div class="text-left">
left
</div>
<div class="text-center">
center
</div>
<div class="text-right">
right
</div>
请参阅jsfiddle中的示例。http://jsfiddle.net/ucheng/Q4Fue/
您可以使用以下类型中的任何一种
使用Bootstrap现有的类文本中心。 添加自定义样式,style = "text-align:center"。 使用列偏移量: 例如:<div class="col-md-offset-6 col-md-12"></div>
2018年更新:
在Bootstrap 4.1.3中,内容可以使用mx-auto类居中。
<div class="mx-auto">
Centered element
</div>
参考:https://getbootstrap.com/docs/4.1/utilities/spacing/ #水平居中
注意:这在引导3中被移除。
在bootstrap 3之前,你可以像这样以分页为中心使用CSS类:
<div class="span12 pagination-centered">
Centered content.
</div>
以类分页为中心已经在bootstrap.css(或bootstrap.min.css)中,并且只有一个规则:
.pagination-centered{text-align:center;}
使用Bootstrap 2.3.0。只需使用类文本中心
只需为你想要的div或标签使用一个类,text-center。我认为它可能会很好。它是一个用于文本对齐中心的引导类。
下面是一些文本对齐引导类:
text-left, text-right, text-justify, etc.
<div class="row">
<div class="col-md-12 text-center">
<h1>Bootstrap starter template</h1>
<p>Example text.</p>
</div>
</div>