justify-content:space-between无法按预期对齐元素

时间:2016-03-01 09:31:45

标签: html css twitter-bootstrap css3 flexbox

我需要使用flexbox来导航我的导航,因此我想出了以下内容:



.navbar-brand > img {
  width: 100px;
}
.navbar-default {
  background-color: #fff;
  border-color: #fff;
  -webkit-box-shadow: 0 0 3px rgba(0, 0, 0, .3);
  box-shadow: 0 0 3px rgba(0, 0, 0, .3);
}
.navbar-default .navbar-nav > li > a {
  color: #464646;
  text-transform: uppercase;
}
.navbar-default .navbar-nav > li > a:hover,
.navbar-default .navbar-nav > li > a:focus,
.navbar-default .navbar-nav > li > a:active {
  color: #727272;
}
.navbar-default .navbar-nav > li:not(.active) > a:before {
  content: '';
  position: absolute;
  bottom: 0;
  left: 30%;
  right: 30%;
  height: 1px;
  background: #ed1c24;
  opacity: 0;
  -webkit-transform: translateY(10px);
  -ms-transform: translateY(10px);
  -o-transform: translateY(10px);
  transform: translateY(10px);
  -webkit-transition: all .3s;
  -o-transition: all .3s;
  transition: all .3s;
}
.navbar-default .navbar-nav > li > a:hover:before {
  opacity: 1;
  -webkit-transform: none;
  -ms-transform: none;
  -o-transform: none;
  transform: none;
}
.navbar-default .navbar-nav > li:first-child > a {
  font-weight: 700;
}
.navbar-default .navbar-nav > li.active > a {
  background: #ed1c24;
  color: #fff;
  padding-top: 25px;
  padding-bottom: 25px;
  position: relative;
  -webkit-transition: all .3s;
  -o-transition: all .3s;
  transition: all .3s;
}
.navbar-default .navbar-nav > li.active > a:hover,
.navbar-default .navbar-nav > li.active > a:focus,
.navbar-default .navbar-nav > li.active > a:active {
  background: #e0222a;
  color: #fff;
}
.navbar-default .navbar-nav > li.active:hover > a:after {
  bottom: 0;
}
.navbar-default .navbar-nav > li.active > a:after {
  font-family: FontAwesome;
  content: '\f078';
  position: absolute;
  bottom: 5px;
  font-size: 0.6em;
  /*opacity: 0.8;*/
  left: 50%;
  -webkit-transform: translateX(-50%);
  -ms-transform: translateX(-50%);
  -o-transform: translateX(-50%);
  transform: translateX(-50%);
  -webkit-transition: all .3s;
  -o-transition: all .3s;
  transition: all .3s;
}
/* use flexbox to center align nav elements above 992px */

@media (max-width: 992px) {
  .navbar-default .navbar-nav > li > a {
    text-align: center;
  }
  .navbar-collapse {
    max-height: 350px;
    overflow-y: hidden;
  }
}
@media (min-width: 992px) {
  .navbar-default {
    display: flex;
    align-items: center;
    justify-content: space-between;
  }
  .navbar-default {
    min-height: 100px;
  }
  .navbar-default .navbar-right {
    display: flex;
    align-items: center;
  }
  .navbar-default > .container {
    display: flex;
    align-items: center;
    justify-content: space-between;
  }
}

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<nav role="navigation" class="navbar navbar-default navbar-fixed-top">
  <div class="container">
    <!-- Brand and toggle get grouped for better mobile display -->
    <div class="navbar-header">
      <button type="button" data-target="#navbarCollapse" data-toggle="collapse" class="navbar-toggle">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <a href="index.html" class="navbar-brand">
        <img src="https://upload.wikimedia.org/wikipedia/commons/4/48/EBay_logo.png" alt="Logo">
      </a>
    </div>
    <!-- Collection of nav links and other content for toggling -->
    <div id="navbarCollapse" class="collapse navbar-collapse">
      <ul class="nav navbar-nav navbar-right">
        <li class="active"><a href="index.html">Home</a>
        </li>
        <li><a href="consulting.html">CONSULTING</a>
        </li>
        <li><a href="devices.html">Medical Devices</a>
        </li>
        <li><a href="">Servises</a>
        </li>
        <li><a href="">News</a>
        </li>
        <li><a href="">Contact Us</a>
        </li>
      </ul>
    </div>
  </div>
</nav>
&#13;
&#13;
&#13;

FIDDLE HERE

从HTML中可以看出,.container有两个子元素。

我将以下CSS应用于.container元素:

.navbar-default > .container{
        display: flex;
        align-items:center;
        justify-content:space-between;
    }

问题是space-between没有使容器的两个子元素位于容器的左右边缘。

我想要的行为是两个子元素应该在左边和右边,这可以使用浮点数来实现,I.E。,我将一个子项向左浮动,一个向右浮动。

此外,如果您应用flex-startflex-end,则元素将被拉到边缘,但是,flex-startflex-end,两个元素都会被拉到一边。因此,我需要使用space-between

有人可以告诉我为什么space-between无效吗?这个错误导致我整个网站出现巨大的对齐问题,请有人告诉我我做错了什么。

3 个答案:

答案 0 :(得分:30)

问题是与Bootstrap样式表冲突,后者在伪容器中放置伪元素。这导致space-between计算多个弹性项目而不是两个。

这是您的灵活容器:

徽标和导航菜单与justify-content: space-between对齐,但未定位在相对的边缘。对齐看起来更像space-around

enter image description here

这是Bootstrap的::before::after伪元素(或伪flex项目):

Firefox documentation中所述:

  

In-flow ::after::before伪元素是弹性项目。

enter image description here

我们在假设中加入一些内容:

就像在汽车旅馆房间里发出黑光一样,你会发现很多东西都不在那里。

enter image description here

删除(或覆盖)伪元素,问题就消失了:

enter image description here

有关flex容器和伪元素的更多详细信息:

答案 1 :(得分:0)

因为flex定位项只是直接响应子元素。使用div将.container类下的所有内容换行,并为该div提供position: flexjustify-content: space-between属性。

示例:

<nav>
 <div class="container">
    <div class="wrapper">
      ...
    <div>
 </div>
</nav>

<style>
.wrapper {
  position: flex;
  justify-content: space-between;
}
</style>

答案 2 :(得分:-2)

<style> #logo{
 position:absolute;
 float:left;
 left:10px;
}
</syle>
<img id="logo" src="https://upload.wikimedia.org/wikipedia/commons/4/48/EBay_logo.png" alt="Logo">

enter link description here