Bootstrap 4 - Center List

前端 未结 4 1106
误落风尘
误落风尘 2021-02-19 11:51

I am building an app with the Bootstrap 4 beta. This app is really basic at the moment and has a single inline list. I\'m trying to center this list horizontally in the middle o

相关标签:
4条回答
  • 2021-02-19 11:59

    If you want to use bootstrap classes then you can use row

    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" rel="stylesheet"/>
    
    <div class="container">
      <ul class="list-inline d-flex justify-content-center">
        <li class="list-inline-item">Item 1</li>
        <li class="list-inline-item">Item 2</li>
        <li class="list-inline-item">Item 3</li>
        <li class="list-inline-item">Item 4</li>
        <li class="list-inline-item">Item 5</li>    
      </ul>
    </div>

    Otherwise, Simply text-align:center is enough.

    ul.list-inline {
      text-align: center
    }
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" rel="stylesheet" />
    
    <div>
      <ul class="list-inline">
        <li class="list-inline-item">Item 1</li>
        <li class="list-inline-item">Item 2</li>
        <li class="list-inline-item">Item 3</li>
        <li class="list-inline-item">Item 4</li>
        <li class="list-inline-item">Item 5</li>
      </ul>
    </div>

    0 讨论(0)
  • 2021-02-19 12:05

    You need to use the class text-center which actually centers the block in the screen using the text-align property within bootstrap css.

    <div>
       <ul class="list-inline text-center">
          <li class="list-inline-item">Item 1</li>
          <li class="list-inline-item">Item 2</li>
          <li class="list-inline-item">Item 3</li>
          <li class="list-inline-item">Item 4</li>
          <li class="list-inline-item">Item 5</li>    
       </ul>
    </div>
    

    Hope this helps.

    0 讨论(0)
  • 2021-02-19 12:17

    Use text-center OR,

    Use mx-auto inside a flexbox container (d-flex)..

    https://www.bootply.com/6COUMfNrEU

    <div class="d-flex">
      <ul class="list-inline mx-auto justify-content-center">
        <li class="list-inline-item">Item 1</li>
        <li class="list-inline-item">Item 2</li>
        <li class="list-inline-item">Item 3</li>
        <li class="list-inline-item">Item 4</li>
        <li class="list-inline-item">Item 5</li>    
      </ul>
    </div>
    

    How to centering works in Bootstrap 4...

    text-center is used for display:inline elements

    mx-auto (auto x-axis margins) will center display:block or display:flex elements that have a defined width, (%, vw, px, etc..). Flexbox is used by default on grid columns, so there are also various centering methods...

    Center text or inline elements: text-center

    <div class="container">
        <h1 class="text-center">i'm centered</h1>
        <div class="row">
            <div class="col text-center">i'm centered!</div>
        </div>
    </div>
    

    Center display:block or display:flex: mx-auto

    <div class="row">
        <div class="col-12">
            <img class="mx-auto d-block" src="//placehold.it/200x150?text=mx-auto">
        </div>
    </div>
    

    Columns can also be centered with: mx-auto, because row is display:flex

    <div class="row">
        <div class="col-4 mx-auto">
            <h6>I'm .col-4 centered</h6>
        </div>
    </div>
    

    Demo Bootstrap 4 Horizontal Centering

    0 讨论(0)
  • 2021-02-19 12:22

    Use display:flex for ul because justify-content-center works with flexbox

    ul {
      display:flex;
    }
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" integrity="sha384-/Y6pD6FV/Vv2HJnA6t+vslU6fwYXjCFtcEpHbNJ0lyAFsXTsjBbfaDjzALeQsN6M" crossorigin="anonymous">
    <div>
      <ul class="list-inline justify-content-center">
        <li class="list-inline-item">Item 1</li>
        <li class="list-inline-item">Item 2</li>
        <li class="list-inline-item">Item 3</li>
        <li class="list-inline-item">Item 4</li>
        <li class="list-inline-item">Item 5</li>    
      </ul>
    </div>

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