Div需要被集中

这个问题在这里已经有了答案:

  • 使用Twitter Bootstrap 3 28答案居中列
  • 如何使用Twitter Bootstrap获得中心内容? 22个答案

  • 如果您使用的是boostrap 4,那么将class justify-content-md-center到具有row类的div完成这项工作。 但似乎你正在使用引导3,所以你可以使用offset类。 尝试以这种方式更新您的代码。

    <div class="container">
      <div class="row">
        <div class="col-sm-12" id="middleBoxMargin">
          <div class="row" id="middleBox">
            <div class="col-sm-2 col-sm-offset-2">
              <div id="groupInsurance"></div>
            </div>
            <div class="col-sm-2">
              <div id="lifeInsurance"></div>
            </div>
            <div class="col-sm-2">
              <div id="dentalInsurance"></div>
            </div>
            <div class="col-sm-2">
              <div id="replacementInsurance"></div>
            </div>
          </div>
        </div>
      </div>
    </div>
    

    你可以使用flex

    #middleBox {
        display: flex;
        justify-content:center
    }
    
    .col {
      padding: 0 15px;
    }
    
    .box {
        background-color: #EEEEEE;
        text-align: center;
        height: 100px;
        width: 100px;
    }
    <div class="container">
      
                <div id="middleBox">
                       <div class="col">
                        <div id="groupInsurance" class="box"></div>
                       </div>
                       <div class="col">
                        <div id="lifeInsurance" class="box"></div>
                       </div> 
                       <div class="col"> 
                        <div id="dentalInsurance" class="box"></div>
                       </div> 
                       <div class="col"> 
                        <div id="replacementInsurance" class="box"></div>
                       </div>
                </div>
                
     </div>

    我会做这样的事情:

    div {
      border: 1px solid black;
    }
    
    #middleBoxMargin {
      margin-top: 80px;
    }
    
    #middleBox {
      display: flex;
      justify-content: center;
    }
    
    #middleBox > div {
      background-color: #EEEEEE;
      text-align: center;
      height: 145px;
      width: 20%;
      margin: 5px 10px;
    }
    <div class="container">
      <div class="row">
        <div class="col-sm-12" id="middleBoxMargin">
          <div id="middleBox">
            <div class="col-sm-2">
              <div id="groupInsurance"></div>
            </div>
            <div class="col-sm-2">
              <div id="lifeInsurance"></div>
            </div>
            <div class="col-sm-2">
              <div id="dentalInsurance"></div>
            </div>
            <div class="col-sm-2">
              <div id="replacementInsurance"></div>
            </div>
          </div>
        </div>
      </div>
    </div>
    链接地址: http://www.djcxy.com/p/75913.html

    上一篇: Div needs to be centered

    下一篇: Centering text within div using bootstrap