2016-01-21 88 views
0

我正在嘗試爲我的網站創建標題。我想讓我的標誌位於窗口的左上角,導航欄位於窗口的右上角。我的問題是,導航欄不與我的標誌中心齊平。這裏的商品:如何在使用Bootstrap時將文本與圖像中心垂直對齊。

#logoHeader { 
 
\t float: left; 
 
\t vertical-align: middle; 
 
} 
 

 
ul { 
 
\t list-style: none; 
 
\t display: inline block; 
 
\t vertical-align: middle; 
 
    
 
} 
 

 
li { 
 
\t float: right; 
 
\t padding: 10px; 
 
\t font-size: 22pt; 
 
\t display: inline-block; 
 
} 
 

 
.header .navContainer { 
 
\t height: 131px; 
 
\t vertical-align: middle; 
 
}
<!DOCTYPE html> 
 
<html> 
 

 
<head> 
 
\t <meta charset = "utf-8"> 
 
\t <meta name="viewport" content="width=device-width, initial-scale=1"> 
 
\t <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous"> 
 
\t <link type= "text/css" rel="stylesheet" href="all.css"/> 
 
\t <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script> 
 
\t <script type = "text/javascript" src = "index.js"></script> 
 
\t 
 
\t <title>JM</title> 
 
</head> 
 

 
<body> 
 

 
<div class ="container"> 
 

 
\t <div class="row"> 
 
    \t <div class="col-md-4 header"> 
 
    \t <img id = "logoHeader" src="file:///Users/Jon/Desktop/JM.COM/images/bigJM.png"/> 
 
    \t </div> 
 
    \t <div class="cold-md-8 navContainer"> 
 
     <ul class = "navBar"> 
 
     \t <li>Home</li> 
 
     \t <li>Blog</li> 
 
     \t <li>Publications</li> 
 
     \t <li>Contact</li> 
 
     </ul> 
 
    \t </div> 
 
    
 
\t </div> 
 

 
</div> 
 

 
</body> 
 
</html>

+0

這個線程可以幫助你http://stackoverflow.com/questions/34912881/how-to-vertically-align-a-div-using-css/ – Mystika

回答

0
關注白手起家規則

http://getbootstrap.com/components/#navbar

<!DOCTYPE html> 
 
<html> 
 
    <head> 
 
    <link rel="stylesheet"  href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous"> 
 
    </head> 
 
    <body> 
 
    <nav class="navbar navbar-default"> 
 
     <div class="container-fluid"> 
 
     <!-- Brand and toggle get grouped for better mobile display --> 
 
     <div class="navbar-header"> 
 
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false"> 
 
      <span class="sr-only">Toggle navigation</span> 
 
      <span class="icon-bar"></span> 
 
      <span class="icon-bar"></span> 
 
      <span class="icon-bar"></span> 
 
      </button> 
 
      <a class="navbar-brand" href="#">Brand</a> 
 
     </div> 
 

 
     <!-- Collect the nav links, forms, and other content for toggling --> 
 
     <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1"> 
 
      <ul class="nav navbar-nav navbar-right"> 
 
      <li class="active"><a href="#">Link <span class="sr-only">(current)</span></a></li> 
 
      <li><a href="#">Link</a></li> 
 
      <li class="dropdown"> 
 
       <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Dropdown <span class="caret"></span></a> 
 
       <ul class="dropdown-menu"> 
 
       <li><a href="#">Action</a></li> 
 
       <li><a href="#">Another action</a></li> 
 
       <li><a href="#">Something else here</a></li> 
 
       <li role="separator" class="divider"></li> 
 
       <li><a href="#">Separated link</a></li> 
 
       <li role="separator" class="divider"></li> 
 
       <li><a href="#">One more separated link</a></li> 
 
       </ul> 
 
      </li> 
 
      </ul> 
 
     </div><!-- /.navbar-collapse --> 
 
     </div><!-- /.container-fluid --> 
 
    </nav> 
 
    <body> 
 

 
</html>

</div><!-- /.navbar-collapse --> 
    </div><!-- /.container-fluid --> 
</nav> 
0

在你bootstrap.min.css變化margin-topmargin-bottom爲0px爲olul選擇

ol, ul { 
    margin-top: 0px; 
    margin-bottom: 0px; 
} 

而且在你的CSS代碼在你的問題的填充也是問題之一。設置10px的填充將導致頂部,底部,左側和右側的額外空間。

將其更改爲

li { 
    float: right; 
    /*padding: 10px;*/ 
    padding-left: 10px; 
    padding-right: 10px; 
    font-size: 22pt; 
    display: inline-block; 
} 
+0

這是有幫助。我通過將每個div高度更改爲與我的徽標相同的高度來解決問題,然後將我的ul的線高度設置爲相同的高度。它自然地對齊內容。看起來有幾種方法可以做到這一點。感謝您的迴應! – WriterState