Aligning elements left, center and right in flexbox

前端 未结 3 1063
逝去的感伤
逝去的感伤 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;
    }

    jsFiddle

提交回复
热议问题