Aligning elements left, center and right in flexbox

前端 未结 3 1051
逝去的感伤
逝去的感伤 2020-12-08 05:07

I\'m trying to create this top header using flexbox.

Basically I would like to center the

(Institution inst
相关标签:
3条回答
  • 2020-12-08 05:14

    Use nested flex containers and flex-grow: 1.

    This allows you to create three equal-width sections on the nav bar.

    Then each section becomes a (nested) flex container which allows you to vertically and horizontally align the links using flex properties.

    Now the left and right items are pinned to the edges of the container and the middle item is perfectly centered (even though the left and right items are different widths).

    .nav {
      display: flex;
      height: 50px;      /* optional; just for demo */
      background: white;
    }
    
    .links {
      flex: 1;          /* shorthand for: flex-grow: 1, flex-shrink: 1, flex-basis: 0 */
      display: flex;
      justify-content: flex-start;
      align-items: center;
      border: 1px dashed red;
    }
    
    .header-title {
      flex: 1;
      display: flex;
      justify-content: center;
      align-items: center;
      border: 1px dashed red;
    }
    
    .logout {
      flex: 1;
      display: flex;
      justify-content: flex-end;
      align-items: center;
      border: 1px dashed red;
    }
    
    .links a {
      margin: 0 5px;
      text-decoration: none;
    }
    <div class="nav mobilenav">
    
      <div class="links">
        <a href="/institutions/">Institutioner</a>
        <a href="/leaders/">Ledere</a>
      </div>
    
      <div class="header-title">Institution institution 1</div>
    
      <div class="logout"><a class="button-dark" href="/user/logout">Log ud</a></div>
    
    </div>

    jsFiddle

    0 讨论(0)
  • 2020-12-08 05:28

    Use justify-content: space-between; like this:

    .container {
      display: flex;
      justify-content: space-between;
    }
    <div class="container">
      <div>A</div>
      <div>B</div>
      <div>C</div>
    </div>

    0 讨论(0)
  • 2020-12-08 05:35

    If you are open to changing your html, you need to put all the items in your header on the same level in the DOM.

    Here's a working example

    .nav {
      background: #e1e1e1;
      list-style: none;
      display: flex;
      align-items: center;
      justify-content: space-between;
      height: 60px;
    }
    
    .nav > div {
      min-width: 0;
      white-space: nowrap;
    }
    
    .header-title {
      flex-basis: 80%;
      text-align: center;
    }
    
    .nav div a {
      text-decoration: none;
      padding: 0px 20px;
      font-weight: 600;
    }
    <div class="nav mobilenav">
    
      <div><a href="/institutions/">Institutioner</a></div>
    
      <div><a href="/leaders/">Ledere</a></div>
    
      <div class="header-title">
        Institution institution 1
      </div>
    
      <div class="logout">
        <a class="button-dark" href="/user/logout">Log ud</a>
      </div>
    
    </div>

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