0

i'm kind of confused about the position of my elements.

Here's the code:

<div class="header">

   





    * {
     margin: 0;
     padding: 0;
     box-sizing: border-box;
     font-family: "Montserrat", sans-serif;
    }
    
    .header {
     width: 100%;
     height: 55px;
     border: 1px solid #e7e7e7;
     background: #f1f0f4;
    }
    
    .nav {
     text-align: right;
    }
    
    navbar ul li {
     display: inline-block;
     padding: 10px;
     font-size: 15px;
     margin: 5px;
     font-weight: bold;
    }
    
    navbar ul li a {
     color: black;
     text-decoration: none;
    }
    
    navbar ul li:hover {
     background: #d3d2d6;
    }
    
    #logo {
     float: left;
     text-decoration: none;
     margin-left: 100px;
    }
    
    #header-img {
     width: 100%;
     height: 700px;
     background: url('./programming.jpeg') no-repeat center /cover;
     opacity: 0.6;
    }
    
    #text {
     position: relative;
     top: 10%;
     text-align: center;
     font-size: 40px;
     color: rgb(0, 0, 0, 1);
     font-weight: bold;
    }
    .body {
    }
    <div class="header">
            <navbar class="nav">
                <ul>
                    <li id="logo"><i class="fas fa-robot fa-lg"> TSV</i><li>
                    <li><a href="#">Home</a></li>
                    <li><a href="#">Blog</a></li>
                    <li><a href="#">About</a></li>
                    <li><a href="#">Contact</a></li>
                </ul>
            </navbar>
            <div id="header-img">
                    <p id="text">Lorem ipsum dolor sit, amet consectetur adipisicing elit. Deserunt, mollitia?</p>
            </div>
        </div>
        <div class="body">
            <h1>TEST</h1>
        </div>

The main problem is that the .body div overlaps with the header-img div and i don't really understand why is that happening.The header-img div is positioned relative and i thought that it won't disrupt the flow of the elements. Thanks!

Mahatmasamatman
  • 1,537
  • 2
  • 6
  • 20
Truica Sorin
  • 499
  • 2
  • 11
  • 24

2 Answers2

1

It is happening because you defined height: 55px to the .header element. Remove it and it should work fine.

.header {
  width: 100%;
  height: 55px;
  border: 1px solid #e7e7e7;
  background: #f1f0f4;
}

And you want, you can set this removed height to the nav explicitly.

.nav {
  text-align: right;
  height: 55px;
  width: 100%;
}
Azametzin
  • 5,223
  • 12
  • 28
  • 46
1

It's because your <header> element encapsulates both your <navbar> AND <div id="header-img">, but your <header> element is set to height: 55px;, so your <div id="header-image"> which is height: 700px, is overflowing outside of the <header>, causing this behavior.

Remove the height: 55px; from .header {.

.header {
    width: 100%;
    height: 55px;  //REMOVE THIS
    border: 1px solid #e7e7e7;
    background: #f1f0f4;
}
Bryan Elliott
  • 4,055
  • 2
  • 21
  • 22