Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Block element not pushing next element onto new line?

First off, take a look at the HTML.

<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>

    <p>test</p>

So, as you can see I just have a navbar in the markup header, featuring links and a logo. Then, I write a paragraph outside of this header. Header is a block element, so my question is, why is this paragraph behind the navbar at the top of the page, so if the navbar has a background you can;t even see it. Why isn’t it going below the navbar?
Take a look at my CSS.

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  font-family: cabin, sans-serif;
}

header {
  display: block;
}

/* NAVBAR */
.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;
}
/*NAVBAR END*/

Thanks in advance.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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 */
}
<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>1</p>
    <p>2</p>
    <p>3</p>
    <p>4</p>
    <p>5</p>
  </div>
</body>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading