在弹性框中左对齐,居中和向右对齐元素

时间:2017-04-04 15:23:15

标签: html css css3 flexbox centering

我尝试使用flexbox创建此顶部标头。

header

基本上我想将<div class="header-title">(机构机构1)与你看到的其他3个元素对齐。 (Institutioner,Ledere和Log ud)就像你在图像上看到的那样。

&#13;
&#13;
.nav {
    background: #e1e1e1;
}
ol, ul {
    list-style: none;
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: flex-start;
}
.header-title {
  justify-content: center;
    align-self: center;
    display: flex;
}
.nav ul li.logout {
      margin-left: auto;
}
.nav ul li a {
    text-decoration: none;
    padding: 0px 20px;
    font-weight: 600;
}
&#13;
<div class="nav mobilenav">
  <div class="header-title">
    Institution institution 1
  </div>
  <ul>
    <li><a href="/institutions/">Institutioner</a></li>
    <li>
      <a href="/leaders/">Ledere</a>
    </li>
    <li class="logout">
      <a class="button-dark" href="/user/logout">Log ud</a>
    </li>
  </ul>
</div>
&#13;
&#13;
&#13;

Demo - JSFiddle

4 个答案:

答案 0 :(得分:28)

使用嵌套的flex容器和flex-grow: 1

这允许您在导航栏上创建三个等宽部分。

然后每个部分成为(嵌套的)flex容器,允许您使用flex属性垂直和水平对齐链接。

现在左右物品固定在容器边缘,中间项目完全居中(即使左右项目宽度不同)。

.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

答案 1 :(得分:3)

像这样使用justify-content: space-between;

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

答案 2 :(得分:1)

如果您愿意更改html,则需要将标题中的所有项目放在DOM中的同一级别。

这是一个有效的例子

&#13;
&#13;
.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;
}
&#13;
<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>
&#13;
&#13;
&#13;

答案 3 :(得分:0)

这是一个 Flex 解决方案,它在正确居中中间容器的同时对齐左右容器。

.header-box {
    width: 100%;
    display: flex;
    flex-flow: row wrap;
    padding-top: 50px;
    
}
.left-header, .center-header, .right-header {
    flex: 100px; /* adjust width if needed */
}
.header-box div:nth-of-type(1) {
    text-align: left;
}
.header-box div:nth-of-type(2) {
    align-self: center;
    text-align: center;
}
.header-box div:nth-of-type(3) {
    text-align: right;
}
<div class="header-box">
  <div class="left-header">Left<br>header<br>content</div>
  <div class="center-header">Center<br>header<br>content</div>
  <div class="right-header">Right<br>header<br>content</div>
</div>