我想用一个非常基本的例子。使用入门页面和网格系统,我希望如下:

< div class = "行" > < div class = " span12”> <h1>引导启动器模板</h1> < / p > < p >示例文本。 < / div > < / div >

...会产生居中的文本。

然而,它仍然出现在最左边。我做错了什么?


当前回答

如果你使用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使用.text-center类来居中文本已经有很长一段时间了。

.text-center { text-align: center !important; }

或者您可以创建自己的实用程序。这些年来我看到的一些变化:

.u-text-center { text-align: center !important; }
.ta-center { text-align: center !important; }
.ta\:c { text-align: center !important; }

在我这边,我定义了新的CSS样式,易于使用和记忆:

.center { text-align: center;}
.left { text-align: left;}
.right { text-align: right;}
.justify { text-align: justify;}
.fleft { float: left;} /*-> similar to .pull-left already existing in bootstrap*/
.fright { float: right;}  /*-> similar to .pull-right already existing in bootstrap*/
.vab { vertical-align: bottom;}
.bold { font-weight: bold;}
.italic { font-style: italic;}

这是个好主意吗? 有什么好的做法吗?

Bootstrap 2.3有一个文本中心类。

<p class="text-left">Left aligned text.</p>
<p class="text-center">Center aligned text.</p>
<p class="text-right">Right aligned text.</p>

添加答案。希望它能帮助到别人

<div class="row">
  <div class="col-sm-12 col-md-12 col-lg-12">
    <div class='d-flex flex-row justify-content-center'>
      <img     
        className='img-fluid'       
        src="<url>"
          />
    </div>
    </div>
</div>

中心块也是一个选项没有提到以上的答案

    <div class="center-block" style="width:200px;background-color:#ccc;">...</div>

类center-block所做的就是告诉元素的边距为0 auto, auto是左/右边距。但是,除非类text-center或css text-align:center;在父元素上设置时,元素不知道从哪个点进行自动计算,因此它不会像预期的那样居中。

所以文本中心是一个更好的选择。