2016-05-14 59 views
1

我正在嘗試使用bootstrap創建一個網站,而我之前使用過bootstrap。這次我正在製作一個帶有固定導航的網站,標誌將位於導航鏈接的中間。我也希望徽標位於導航鏈接的頂部。如何將徽標圖像置於引導固定導航的中間?

此圖片會顯示什麼,我試圖做的:

enter image description here

我現在的代碼是這樣的:

html, body { 
 
    margin: 0; 
 
    padding: 0; 
 
    background: #fff; 
 
    color: #000; 
 
    font-size: 14px; 
 
    font-family: 'Helvetica', serif; 
 
} 
 

 
.navbar { 
 
    background: none; 
 
    border: none; 
 
    margin-top: 20px; 
 

 
} 
 

 
.container-fluid { 
 
    padding-left: 0px; 
 
    padding-right: 0px; 
 
    margin-left: -40px; 
 
} 
 

 
.navbar ul li { 
 
    display: block; 
 
    text-align: center; 
 
    float: left; 
 
    width: 20%; 
 

 
} 
 

 
.fourth-color { 
 
    background-color: #ffecec; 
 
    padding-top: 30px; 
 
    padding-bottom: 30px; 
 
} 
 

 
.third-color { 
 
    background-color: #122212; 
 
    padding-top: 30px; 
 
    padding-bottom: 30px; 
 
} 
 

 
.second-color { 
 
    background-color: #aaa; 
 
    padding-top: 30px; 
 
    padding-bottom: 30px; 
 
} 
 

 
.first-color { 
 
    background-color: #f25727; 
 
    padding-top: 30px; 
 
    padding-bottom: 30px; 
 
}
<!-- Start of Nav Bar --> 
 
<nav class="navbar navbar-default navbar-fixed-top"> 
 
    <div class="container-fluid"> 
 
    <ul> 
 
     <li class="first-color"><a href="#">First</a></li> 
 
     <li class="second-color"><a href="#">Second</a></li> 
 
     <li><a href="#">Logo</a></li> 
 
     <li class="third-color"><a href="#">Third</a></li> 
 
     <li class="fourth-color"><a href="#">Fourth</a></li> 
 
    </ul> 
 
    </div> 
 
</nav>

當我試圖鏈接一個圖像,它不適合,並與整個導航欄螺絲。任何幫助表示讚賞!

回答

2

在這裏你的問題是你只有在有類的li上填充。 (這就是爲什麼說標誌的錨不居中是因爲它沒有填充類)

ex。

.navbar ul li { 
    display: block; 
    text-align: center; 
    float: left; 
    width: 20%; 
} 

.fourth-color, .third-color, .second-color, .first-color { 
    padding-top: 30px; 
    padding-bottom: 30px; 
} 

將填充應用於所有列表,並且問題已修復。

.navbar ul li { 
    padding-top: 30px; 
    padding-bottom: 30px; 
} 

至於中間的標誌。您必須使用position relative,然後將其他樣式添加到該類中,或者在我的例子:nth-child中添加樣式,如下所示。

@import url("http://fonts.googleapis.com/css?family=Raleway&subset=latin,latin-ext"); 
 

 
html, body { 
 
    margin: 0; 
 
    padding: 0; 
 
    background: #fff; 
 
    color: #000; 
 
    font-size: 14px; 
 
    font-family: 'Helvetica', serif; 
 
} 
 

 
.navbar { 
 
    margin-top: 1em; 
 
    text-align: center; 
 
} 
 

 
.navbar ul { 
 
    padding: 0; 
 
    text-decoration: none; 
 
    list-style: none; 
 
} 
 

 
.navbar li { 
 
    display: block; 
 
    float: left; 
 
    width: calc(20% - 1px - 0.906752000000002em); 
 
    padding: 1.16em 1em; 
 
    font: 100 21px "Raleway", sans-serif; 
 
    background: #000; 
 
    color: #fff; 
 
    opacity: 1; 
 
    transition: all 0.1s linear; 
 
} 
 

 
.navbar li:nth-child(1) { 
 
    background: #ff410e; 
 
} 
 

 
.navbar li:nth-child(2) { 
 
    background: #f4ca00; 
 
} 
 

 
.navbar li:nth-child(3) { 
 
    background: #fff; 
 
    color: #000; 
 
    border: 1px solid #000; 
 
    border-radius: 2.84em; 
 
    padding: 1.76em 0; 
 
    width: 20%; 
 
    position: relative; 
 
    margin: -0.64em -2.16em; 
 
    z-index: 1; 
 
} 
 
.navbar li:nth-child(3) a { 
 
    color: #000; 
 
} 
 

 
.navbar li:nth-child(4) { 
 
    background: #99d81b; 
 
} 
 

 
.navbar li:nth-child(5) { 
 
    background: #00abf3; 
 
} 
 

 
.navbar li:hover:not(:nth-child(3)) { 
 
    opacity: 0.7; 
 
} 
 

 
.navbar a { 
 
    color: #fff; 
 
    text-decoration: none; 
 
} 
 
.navbar .active { 
 
    text-decoration: underline; 
 
}
<!-- Start of Nav Bar --> 
 
<nav class="navbar navbar-default navbar-fixed-top"> 
 
    <div class="container-fluid"> 
 
    <ul> 
 
     <li><a class="active" href="#">First</a></li> 
 
     <li><a href="#">Second</a></li> 
 
     <li><a href="#">Logo</a></li> 
 
     <li><a href="#">Third</a></li> 
 
     <li><a href="#">Fourth</a></li> 
 
    </ul> 
 
    </div> 
 
</nav>

除非你打算創建一個下拉列表,我不建議使用列表此菜單欄。而不是you can just style the anchors to fit your needs。 (請記住保留您的代碼DRY):

+0

好的,所以我已經完成了這項工作並將其置於中心位置。但是,當我添加圖像時,我會希望自己的徽標是,它會低於導航欄。另外,真正的圖像方式會更寬一些,看起來像我提供的imgur鏈接中的設計。 – Billy

+0

我已經使這[Codepen](http://codepen.io/anon/pen/qZwVpL)顯示我在說什麼。是的,我不認爲我會使用下拉列表,所以這個想法看起來更好。我可以使用該方法添加圖片徽標嗎? – Billy

+0

我現在明白了。請參閱[如何問](http://stackoverflow.com/questions/how-to-ask)和[完美的問題](http://codeblog.jonskeet.uk/2010/08/29/writing-the - 完美-問題/)。我已經更新了我的答案,並希望它能夠滿足您的需求。 BTW我在我的平板電腦上,所以我使用[kodeWeave](http://kodeweave.sourceforge.net/editor/#0873d7f82d37b9824e7426d3ce6e88ff)(Jade/Stylus預處理器)以更快地回答問題。 –