我正试图将列的内容居中。看起来对我没用啊。这是我的HTML:

<div class="row">
   <div class="col-xs-1 center-block">
       <span>aaaaaaaaaaaaaaaaaaaaaaaaaaa</span>
   </div>
 </div>

JSFiddle演示


当前回答

使用文本中心代替中心块。

或者在span元素上使用center-block(我把列做宽了,这样你可以更好地看到对齐):

<div class="row">
   <div class="col-xs-10" style="background-color:#123;">
      <span class="center-block" style="width:100px; background-color:#ccc;">abc</span>
   </div>
</div>

其他回答

col- gl -4 col-md-6

 1. col-lg-4 ≥ 1200px (popular 1366, 1600, 1920+)
 2. col-md-6 ≥ 970px (popular 1024, 1200)
 3. col-sm-8 ≥ 768px (popular 800, 768)
 4. col-11 set default smaller devices for gutter (popular 600,480,414,375,360,312)
 5. mx-auto = always block center


没必要把事情复杂化。使用Bootstrap 4,您可以简单地使用边缘自动类my-auto在列内水平对齐项

         <div class="col-md-6 my-auto">
           <h3>Lorem ipsum.</h3>
         </div>

Bootstrap 4/Bootstrap 5,水平和垂直对齐内容使用伸缩盒

<div class="page-hero d-flex align-items-center justify-content-center">
 <h1>Some text </h1>
</div>

使用引导类align-items-center和justify-content-center

.page-hero { 身高:200 px; 背景颜色:灰色; } <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="匿名"> <div class="page-hero - d-flex align-items-center - align- content-center"> <h1>一些文本</h1> < / div >

//add this to your css
    .myClass{
          margin 0 auto;
          }

// add the class to the span tag( could add it to the div and not using a span  
// at all
    <div class="row">
   <div class="col-xs-1 center-block">
       <span class="myClass">aaaaaaaaaaaaaaaaaaaaaaaaaaa</span>
   </div>
 </div>

Use:

<!-- unsupported by HTML5 -->
<div class="col-xs-1" align="center">

而不是

<div class="col-xs-1 center-block">

你也可以使用bootstrap 3 css:

<!-- recommended method -->
<div class="col-xs-1 text-center">

Bootstrap 4现在有将内容居中的伸缩类:

<div class="d-flex justify-content-center">
   <div>some content</div>
</div>

注意,默认情况下,它将是x轴,除非flex方向是列