簡體   English   中英

帶標題的標題中的導航欄居中

[英]Centering nav bar in header with logo

在尋求有關項目的幫助時,我試圖獲得一個導航欄,該導航欄位於標頭內,左側帶有徽標。

的HTML

<header>
  <img src="http://s32.postimg.org/5bebu6mbl/Image_5_8_16_at_12_10_PM.jpgHome" />
  <div id="nav">
    <span><a href="#">Home</a></span>
    <span><a href="#"><button>Televeisions</button></a></span>
    <span><a href="#">Electronics</a></span>
    <span><a href="#">Services</a></span>
  </div>
</header>

的CSS

header {
  height: 5.5em;
  background: gray;
  color: Black;
  text-align: justify;
}

#nav {
  width: 100%;
  text-align: center;
}

#nav span a {
  color: black;
  text-decoration: none;
  padding: 10px;
}

JSFiddle

導航寬度100%會將導航強制在圖像下方。 我添加了溢出:隱藏在標題中,將img浮動到左側,並在#nav上放置一個空白。

將CSS調整為如下所示:

header {
  height: 5.5em;
  background: gray;
  color: Black;
  text-align: justify;
  overflow: hidden;
}
img {
  float: left;
}
#nav {
  width: 600px;
  text-align: center;
  margin-top: 2.5em;
}

#nav span a {
  color: black;
  text-decoration: none;
  padding: 10px;
}

您可以調整邊距等,以從頭開始設計風格。

Codepen: http ://codepen.io/Tambe257/pen/XdGdLZ

在將要嵌套在其中的父元素和子元素上使用定位。 這是一個簡單的例子

header {
  height: 5.5em;
  background: gray;
  color: Black;
  text-align: justify;
  position:relative;
}

#nav {
  width: 100%;
  text-align: center;
 top:2em;
 position:absolute;
}

#nav span a {
  color: black;
  text-decoration: none;
  padding: 10px;

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM