[英]Mysterious space after navigation
我使用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;
}
}
它在移动屏幕上看起来很好,但在更高分辨率时会出现问题。 我希望按钮位于极端,两者之间有空间。 我该怎么做?
你不需要一个clearfix ,因为你正在处理flexboxes而不是浮动容器 - 所以你可以删除nav:after
。
问题是nav:after
是一个弹性项目和justify-content: space-around
也会考虑这个 - 在删除nav:after
看下面的演示nav:after
:
* { 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; } .ba-host { white-space: nowrap; } .nav-btn.hoverback:hover { cursor: pointer; background-color: #f8f8f8; } @media (max-width:500px) { nav { flex-direction: column; } }
<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 ba-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>
您可以将一个类添加到另一个容器并使其为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; } .ba-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 ba-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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.