从我的导航栏中删除空格

Remove whitespace from my navigation bar

这是我的代码(我是 html 中的菜鸟)这段代码让我在导航的顶部和左侧留有空白...你们知道我做错了什么吗?

    div.nav {
  background-color: black;
  color: white;
  font-size: 20px;
  font-weight: bold;
  position: fixed;
  width: 100%;
  text-align: center;
}
ul {

}
li {
  display: inline-block;
  list-style: none;
}
li a {
  padding: 15px;
  color: white;
}


<div class="nav">
  <ul>
    <li><a href="index.html">Home</a></li>
    <li><a href="watisdb.html">Wat is D&B</a></li>
    <li><a href="dbnederland.html">D&B in Nederland</a></li>
  </ul>
</div>

已添加html

这是CSS。添加此

body{
   padding: 0;
   margin: 0;
 }

body{
   padding: 0;
   margin: 0;
   
   background-color: #e8e4e5;
 }
 div.nav {
  background-color: black;
  color: white;
  font-size: 20px;
  font-weight: bold;
  position: fixed;
  width: 100%;
  text-align: center;
}
ul {

}
li {
  display: inline-block;
  list-style: none;
}
li a {
  padding: 15px;
  color: white;
}
<div class="nav">
  <ul>
    <li><a href="index.html">Home</a></li>
    <li><a href="watisdb.html">Wat is D&B</a></li>
    <li><a href="dbnederland.html">D&B in Nederland</a></li>
  </ul>
</div>

但您可以将导航 div 位置固定:当您在内容中滚动时它会有所帮助,但导航 div 始终位于顶部。

 body{
   padding: 0;
   margin: 0;
 }
 .content{
   margin-top: 60px;
   height: 150vh;
   background-color: #d2d29d;
 }
 div.nav {
  position: fixed;
  height: 60px;
  background-color: black;
  color: white;
  font-size: 20px;
  font-weight: bold;
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  text-align: center;
}
ul {

}
li {
  display: inline-block;
  list-style: none;
}
li a {
  padding: 15px;
  color: white;
}
<div class="nav">
  <ul>
    <li><a href="index.html">Home</a></li>
    <li><a href="watisdb.html">Wat is D&B</a></li>
    <li><a href="dbnederland.html">D&B in Nederland</a></li>
  </ul>
</div>
<div class="content">
  Some text
</div>

由于您使用的是 position:fixed;,您可以将 css 更改为以下内容,这将强制导航栏位于页面的左上角

div.nav {
    background-color: black;
    color: white;
    font-size: 20px;
    font-weight: bold;
    position: fixed;
    top:0; //<================= Add this
    left:0; //<================= And add this
    width: 100%;
    text-align: center;
}

当然,方法还有很多。以上 CSS 将导致您的空格被删除

    div.nav {
  background-color: black;
  color: white;
  font-size: 20px;
  font-weight: bold;
  position: fixed;
  top:0; //<================= Add this
  left:0; //<================= And add this
  width: 100%;
  text-align: center;
}
ul {

}
li {
  display: inline-block;
  list-style: none;
}
li a {
  padding: 15px;
  color: white;
}
<div class="nav">
  <ul>
    <li><a href="index.html">Home</a></li>
    <li><a href="watisdb.html">Wat is D&B</a></li>
    <li><a href="dbnederland.html">D&B in Nederland</a></li>
  </ul>
</div>

希望对您有所帮助!