2017-09-18 19 views
3

フレックスボックスを使ってこのナビゲーションバーを作ったが、右側のボタンを極端にしたい。 は、ここに私のコードです: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; 
    } 
} 

。私はそれらの間にスペースを入れてボタンを極端にしたい。どうすればいいのですか?あなたはflexboxesない浮動コンテナを扱っているよう

答えて

2

現在地clearfixを必要としない - あなたは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; 
 
} 
 

 
.b-a-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 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>

0

あなたはにクラスを追加することができますnav:afterを削除した後のデモ下記参照 -

問題はnav:afterすぎることを検討しますフレックス項目justify-content: space-aroundであるということです別の容器に入れて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>

に役立つことを願います