导航后的神秘空间

时间:2017-09-18 06:30:28

标签: html css css3 flexbox alignment

我使用flexbox制作了这个导航栏,但我希望右侧按钮位于最右侧。 这是我的代码:https://codepen.io/mayankkamboj/pen/XKbdOY

HTML:

<link href='https://fonts.googleapis.com/css?family=Roboto:300,400' rel='stylesheet' type='text/css'>
<nav>
  <div class='container'>
    <div class='nav-btn hoverback'>
      <img src='https://upload.wikimedia.org/wikipedia/commons/6/69/Airbnb_Logo_B%C3%A9lo.svg' />
    </div>
    <div class='nav-btn no-border'>
      <i class="fa fa-search fa-2x" aria-hidden="true"></i>
      <input type='text' placeholder='Where to ?' class='search'/>
    </div>
  </div>
  <div class='container'>
    <div class='nav-btn hoverback'>
      <div class='btn b-a-host'>Become a Host</div>
    </div>
    <div class='nav-btn hoverback'>
      Help
    </div>
    <div class='nav-btn hoverback'>
      Sign Up
    </div>
    <div class='nav-btn hoverback'>
      Log In
    </div>
  </div>
</nav>

CSS:

*{
  box-sizing:border-box;
}
body{
  padding:0;
  margin:0;
}
nav{
  border-bottom:1px solid #ddd;
  font-family:'Roboto';
  display:flex;
  justify-content:space-between;
  font-weight:300;
}
nav:after{
 content:" ";
  display:block;
  clear:both;
  height:0.01px;
}
.container{
  display:flex;
}

.nav-btn{
  height:4em;
  padding:0em 1em;
  border-right:1px solid grey;
  display:flex;
  min-width:100px;
  justify-content:center;
  align-items:center;
}
.no-border{
  border-right:0px solid white;
}
img{
  height:60%;
}
.fa{
  color:#ccc;
}
nav .search{
  height:4.5em;
  width:20em;
  font-family:'Roboto';
  font-weight:400;
  border-style:none;
  text-indent:1em;
}
nav .search:focus{
  outline-style:none;
}
nav .btn{
  font-weight:400;
  color:grey;
  border:2px solid #aaa;
  padding:0.5em 0.7em;
  letter-spacing:0.01em;
}
.b-a-host{white-space:nowrap;}
.nav-btn.hoverback:hover{
  cursor:pointer;
  background-color:#f8f8f8;
}
@media (max-width:500px){
  nav{
    flex-direction:column;
  }
}

它在移动屏幕上看起来很好,但在较高分辨率时会出现问题。我希望按钮位于极端,两者之间有空间。我该怎么做?

2 个答案:

答案 0 :(得分:3)

您在处理 flexboxes 而不是浮动容器时,不需要 clearfix - 因此您可以删除{{1 }}

问题是--prod flag flex项目,而nav:after也会考虑这一点 - 请在删除nav:after后参阅下面的演示:

justify-content: space-around
nav:after

答案 1 :(得分:0)

您可以将一个类添加到另一个容器并使其justify-content: flex-end以及更多,请将flex:1添加到容器中,以便两个容器可以具有相同的宽度并删除clearfix。我希望它对你有所帮助

*{
  box-sizing:border-box;
}
body{
  padding:0;
  margin:0;
}
nav{
  border-bottom:1px solid #ddd;
  font-family:'Roboto';
  display:flex;
  justify-content:space-between;
  font-weight:300;
}

.container{
  display:flex;
  flex: 1;
}

.container.right {
  justify-content: flex-end;
}

.nav-btn{
  height:4em;
  padding:0em 1em;
  border-right:1px solid grey;
  display:flex;
  min-width:100px;
  justify-content:center;
  align-items:center;
}
.no-border{
  border-right:0px solid white;
}
img{
  height:60%;
}
.fa{
  color:#ccc;
}
nav .search{
  height:4.5em;
  width:20em;
  font-family:'Roboto';
  font-weight:400;
  border-style:none;
  text-indent:1em;
}
nav .search:focus{
  outline-style:none;
}
nav .btn{
  font-weight:400;
  color:grey;
  border:2px solid #aaa;
  padding:0.5em 0.7em;
  letter-spacing:0.01em;
}
.b-a-host{white-space:nowrap;}
.nav-btn.hoverback:hover{
  cursor:pointer;
  background-color:#f8f8f8;
}
@media (max-width:500px){
  nav{
    flex-direction:column;
  }
}
<nav>
  <div class='container'>
    <div class='nav-btn hoverback'>
      <img src='https://upload.wikimedia.org/wikipedia/commons/6/69/Airbnb_Logo_B%C3%A9lo.svg' />
    </div>
    <div class='nav-btn no-border'>
      <i class="fa fa-search fa-2x" aria-hidden="true"></i>
      <input type='text' placeholder='Where to ?' class='search'/>
    </div>
  </div>
  <div class='container  right'>
    <div class='nav-btn hoverback'>
      <div class='btn b-a-host'>Become a Host</div>
    </div>
    <div class='nav-btn hoverback'>
      Help
    </div>
    <div class='nav-btn hoverback'>
      Sign Up
    </div>
    <div class='nav-btn hoverback'>
      Log In
    </div>
  </div>
</nav>