Why is a div outside the parent div when I use height: 100%

I have problems placing a div in another div that has a specific height, say 400px.

When I set the inner div to 100%, then it overlaps the outer div and jumps on top of the outer. Why is the height 100% independent of the size of the inner div to the outer height of the divs?

body {
  min-width:300px;
}

.header {
  background-color:pink;
  width:100%;
  height:400px;
}

.menu {
  background-color: red;
}

.header-container {
  color:white;
  background-color:gray;
  height:100%;
  max-width:400px;
  margin:auto;
}

.headline {
  padding-right:36px;
  padding-left:36px;
  padding-top:54px;
  padding-bottom:54px;
}

.clearfix {
  clear:both;
}
<div class="header">
  <div class="menu">
  menu
  </div>
  <div class="header-container clearfix">
    <div class="headline">
      <span>das ist mein blog</span>
      <span>this is the underline</span>
    </div>
  </div>
</div>
<div class="blog">
y
</div>
<div class="footer">
x
</div>
Run codeHide result

https://jsfiddle.net/g9ec4nw8/

+4
source share
1 answer

Since filling on .header-containercauses overflow.

Adding box-sizing: border-box;to yours .header-containerwill fix the window size problem.

But not only that, you did not take your 18px heights into account .menu.

In full, changing your .header-containerto the following code:

.header-container {
        color:white;
        background-color:gray;
        height:calc(100% - 18px);
        max-width:400px;
        box-sizing: border-box;
        margin:auto;
        top:0;
        bottom:0;
        padding-right:36px;
        padding-left:36px;
        padding-top:54px;
        padding-bottom:54px;
 }

To solve a problem.

Fiddle .

+7

Source: https://habr.com/ru/post/1685586/


All Articles