center a row using Bootstrap 3

后端 未结 13 1398
长发绾君心
长发绾君心 2020-12-29 18:08

How to center a row (12 column) in Bootstrap 3 ? I do not want to use the offset

I am using this way but not worked.

相关标签:
13条回答
  • 2020-12-29 18:22

    Instead of trying to center div's, just add this to your local css.

    .col-md-offset-15 {
        margin-left: 12.4999999%;
    }
    
    which is roughly  offset-1 and half of offset-1. (8.333% + 4.166%) = 12.4999%
    

    This worked for me.

    0 讨论(0)
  • 2020-12-29 18:24

    I know this question was specifically targeted at Bootstrap 3, but in case Bootstrap 4 users stumble upon this question, here is how i centered rows in v4:

    <div class="container">
      <div class="row justify-content-center">
      ...
    

    More related to this topic can be found on bootstrap site.

    0 讨论(0)
  • 2020-12-29 18:31

    this peace of code can help you

    <div class="row" style="display: flex; justify-content: center;"></div>
    
    0 讨论(0)
  • 2020-12-29 18:35

    Instead of

    <div class="col-md-4"></div>
    <div class="col-md-4"></div>
    <div class="col-md-4"></div>
    

    You could just use

    <div class="col-md-4 col-md-offset-4"></div>
    

    As long as you don't want anything in columns 1 & 3 this is a more elegant solution. The offset "adds" 4 columns in front, leaving you with 4 "spare" after.

    PS I realise that the initial question specifies no offsets but at least one previous answer uses a CSS hack that is unnecessary if you use offsets. So for completeness' sake I think this is valid.

    0 讨论(0)
  • 2020-12-29 18:35

    I use text-align-center in a row like this

    <div class="row tac">
        <h1>Centered content</h1>
    </div>
    
    <style>
     .tac { text-align: center}
    </style>
    
    0 讨论(0)
  • 2020-12-29 18:36

    What you are doing is not working, because you apply the margin: auto to the full-width column.

    Wrap it in a div and center that one. E.g:

    <div class="i-am-centered">
      <div class="row">...</div>
    </div>
    

    .

    .i-am-centered { margin: auto; max-width: 300px;}
    

    http://www.bootply.com/93751

    Its a cleaner solution anyway, as it is more expressive and as you usually don't want to mess with the grid.

    0 讨论(0)
提交回复
热议问题