消除导航和标题之间的小差距

时间:2016-06-03 15:21:08

标签: css html5 header nav

我的导航栏(位于页面顶部)和标题(导航栏下方的,其中包含大图像和文字),他们之间有一个非常小的差距,我想删除。我已经访问了很多关于类似问题的帖子,并尝试了他们的解决方案,但似乎无法为我工作,包括:display: block; margin: 0; e.t.c。

我想这与我在标题内容或导航内容上应用的样式有关。

body {
  margin: 0px;
  padding: 0px;
  background-color: #f2f2f2;
}
html {
  margin: 0px;
  padding: 0px;
}
#logo {
  height: 50px;
  width: auto;
  float: left;
}
nav ul {
  list-style-type: none;
  overflow: hidden;
  margin: 0;
  padding: 0;
  background-color: #1a1a1a;
  text-align: center;
  border: 1px solid #e7e7e7;
  display: inline-block;
  width: 100%;
}
nav li {
  display: inline-block;
}
nav a {
  display: inline-block;
  padding: 16px 15px;
  text-decoration: none;
  font-family: arial;
  font-weight: bold;
  color: white;
}
nav a:hover {
  background-color: orange;
  color: white;
}
nav {
  margin-bottom: 0;
}
header {
  margin-top: 0;
  margin-bottom: 10px;
  width: 100%;
  font-family: arial;
  font-size: 18px;
  color: orange;
}
h1 {
  position: absolute;
  top: 150px;
  width: 100%;
  z-index: 1;
  text-align: center;
}
#bannerImage {
  width: 100%;
  height: auto;
  position: relative;
  z-index: 0;
}
<nav>
  <ul>
	<img id="logo" src="https://67.media.tumblr.com/f607af5bc60d1b2837add83c70a2aa45/tumblr_inline_mrwv19q8fE1qz4rgp.gif"/>
	<li><a href="Game1.html" id="myBtn" onclick="disableLink()" >Game 1</a></li><li><a href="">Game 2</a></li><li><a href="">Game 3</a></li>
  </ul>
</nav>
	
<header>
  <img id="bannerImage" src="http://static2.hypable.com/wp-content/uploads/2014/09/Hogwarts-lake.png"/>
  <h1>Games</h1>
</header>

3 个答案:

答案 0 :(得分:1)

只需定义nav

的高度即可

&#13;
&#13;
body {
  margin: 0px;
  padding: 0px;
  background-color: #f2f2f2;
}
html {
  margin: 0px;
  padding: 0px;
}
#logo {
  height: 50px;
  width: auto;
  float: left;
}
nav ul {
  list-style-type: none;
  overflow: hidden!important;
  margin: 0;
  padding: 0;
  background-color: #1a1a1a;
  text-align: center;
  border: 1px solid #e7e7e7;
  display: inline-block;
  width: 100%;
}
nav li {
  display: inline-block;
}
nav a {
  display: inline-block;
  padding: 16px 15px;
  text-decoration: none;
  font-family: arial;
  font-weight: bold;
  color: white;
}
nav a:hover {
  background-color: orange;
  color: white;
}
nav {
  margin-bottom: 0;
  height: 51px;
}
header {
  margin-top: 0;
  margin-bottom: 10px;
  width: 100%;
  font-family: arial;
  font-size: 18px;
  color: orange;
}
h1 {
  position: absolute;
  top: 150px;
  width: 100%;
  z-index: 1;
  text-align: center;
}
#bannerImage {
  width: 100%;
  height: auto;
  position: relative;
  z-index: 0;
}
&#13;
<nav>
  <ul>
	<img id="logo" src="https://67.media.tumblr.com/f607af5bc60d1b2837add83c70a2aa45/tumblr_inline_mrwv19q8fE1qz4rgp.gif"/>
	<li><a href="Game1.html" id="myBtn" onclick="disableLink()" >Game 1</a></li><li><a href="">Game 2</a></li><li><a href="">Game 3</a></li>
  </ul>
</nav>
	
<header>
  <img id="bannerImage" src="http://static2.hypable.com/wp-content/uploads/2014/09/Hogwarts-lake.png"/>
  <h1>Games</h1>
</header>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

问题是由ul overflow:hidden造成的,因此请将其删除。

添加box-sizing:border-box以避免水平滚动条

<强>更新

我注意到您有一个img作为ul的孩子,这使得该HTML无效。

所以我调整了你的代码以使其有效。

*,
*::before,
*::after {
  box-sizing: border-box
}
body,
html {
  margin: 0;
  padding: 0;
  background-color: #f2f2f2;
}
#logo {
  height: 50px;
  width: auto;
  display: inline-block;
  vertical-align:top
}
nav {
  margin-bottom: 0;
  background-color: #1a1a1a;
  border: 1px solid #e7e7e7;
}
nav ul {
  list-style-type: none;
  width: calc(100% - 60px);
  margin:0;
  padding: 0;
  text-align: center;
  display: inline-block;
  vertical-align: top;
}
nav li {
  display: inline-block;
}
nav a {
  display: inline-block;
  padding: 16px 15px;
  text-decoration: none;
  font-family: arial;
  font-weight: bold;
  color: white;
}
nav a:hover {
  background-color: orange;
  color: white;
}
header {
  margin-top: 0;
  margin-bottom: 10px;
  width: 100%;
  font-family: arial;
  font-size: 18px;
  color: orange;
}
h1 {
  position: absolute;
  top: 150px;
  width: 100%;
  z-index: 1;
  text-align: center;
}
#bannerImage {
  width: 100%;
  height: auto;
  position: relative;
  z-index: 0;
}
<nav>
  <img id="logo" src="https://67.media.tumblr.com/f607af5bc60d1b2837add83c70a2aa45/tumblr_inline_mrwv19q8fE1qz4rgp.gif" />
  <ul>
    <li><a href="Game1.html" id="myBtn" onclick="disableLink()">Game 1</a>
    </li>
    <li><a href="">Game 2</a>
    </li>
    <li><a href="">Game 3</a>
    </li>
  </ul>
</nav>

<header>
  <img id="bannerImage" src="http://static2.hypable.com/wp-content/uploads/2014/09/Hogwarts-lake.png" />
  <h1>Games</h1>
</header>

答案 2 :(得分:0)

尝试设置导航的高度:

nav {
  height: 50px;
}

即使没有将边距设置为0,也可以尝试使用它。