Why is the content rendered under the navigation menu?

Viewed 53

There is a navigation menu that I developed in <header>. However, the <p> element I use after the <header> element and the <header> components overlap. Why does this issue occur and how do I fix this issue?

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}
body {
  font-family: cabin, sans-serif;
}
header {
  display: block;
}
.primary {
  color: blue;
  font-weight: bold;
}
.navbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  height: 70px;
  position: fixed;
  top:0px;
  padding: 0 30px;
  background: transparent;
}
.navbar ul {
  display: flex;
}
.navbar li {
  list-style: none;
}
.navbar a {
  text-decoration: none;
  margin: 0 5px;
  padding: 10px 20px;
  font-weight: bolder;
}
.navbar a:hover{
  border-bottom: 2px blue solid;
}
.navbar a:visited{
  color: blue;
}
<body>
  <!-- Navigation Menu -->
  <header>
    <div class="navbar">
        <h1 class="logo"><span class="primary">benj</span>.codes</h1>

        <ul>
          <li><a href="index.html">Home</a></li>
          <li><a href="#">About</a></li>
          <li><a href="#">Projects</a></li>
          <li><a href="#">Contact</a></li>
        </ul>
    </div>
  </header>
  
  <!-- Content -->
  <p>test</p>
</body>

1 Answers

This issue occurs in the .navbar class style position: fixed; caused by its use. In this case, the navigation menu remains fixed when the scrollbar is opened. So you can enclose other elements after the <header> element in a <div> element; apply a margin-top style to this element and you will see all other content scroll.

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}
body {
  font-family: cabin, sans-serif;
}
header {
  display: block;
}
.primary {
  color: blue;
  font-weight: bold;
}
.navbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  height: 70px;
  /* This style applied causes the <p> element to render above. */
  position: fixed;
  top:0px;
  padding: 0 30px;
  background: yellow;
}
.navbar ul {
  display: flex;
}
.navbar li {
  list-style: none;
}
.navbar a {
  text-decoration: none;
  margin: 0 5px;
  padding: 10px 20px;
  font-weight: bolder;
}
.navbar a:hover{
  border-bottom: 2px blue solid;
}
.navbar a:visited{
  color: blue;
}
/* The following style has been applied to the <div> element that encloses other elements. */
.container {
  margin-top: 70px; /* To avoid shifting caused by the "position: fixed" class style */
  height: 1500px;   /* To make the scrollbar pop up */
}
p {
  height: 200px;
  border: 1px solid red;
  background-color: lightgray;
}
<body>
  <header>
    <div class="navbar">
      <h1 class="logo"><span class="primary">benj</span>.codes</h1>
      <ul>
        <li><a href="index.html">Home</a></li>
        <li><a href="#">About</a></li>
        <li><a href="#">Projects</a></li>
        <li><a href="#">Contact</a></li>
      </ul>
    </div>
  </header>
  
  <div class="container">
    <p>Content</p>
  </div>
</body>

Related