1

When I try to set margin-top to the #nav element, why does that property than affects the whole header div?

heres the source:

<!DOCTYPE html>
<html>
  <head>
    <title>Improving Great Deal</title>
    <style type="text/css">
      * {
        margin: 0px;
        padding: 0px;
      }
      #header {
        background: black;
        height: 60px;
        width: 100%;
        clear: left;
      }
      #nav {
        width: 50%;
        height: 40px;
        background: red;
        margin: 5px auto;


      }
      #nav ul {
        list-style: none;


      }
      #nav ul li {
        float: left;

      }
      #nav ul li a {
        text-decoration: none;
        color: white;
        font-size: 20px;
        padding: 0px 23px;
        line-height: 34px;
      }
    </style>
  </head>
  <body>
    <div id="header">
      <div id="nav">
        <ul>
          <li><a href="#">Pocetna</a></li>
          <li><a href="#">Forum</a></li>
          <li><a href="#">Blog</a></li>
          <li><a href="#">Pitanja</a></li>
          <li><a href="#">Informacije</a></li>
          <li><a href="#">Zarada</a></li>
        </ul>
      </div> 
    </div>


  </body>
</html>
Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
nidza
  • 21
  • 4

1 Answers1

1

The reason being margin gets overlapped and you need overflow: hidden; to show it. See the details below. To include the margin of the children, you need to add overflow: hidden. The possible explanation for this is as below.

Few possible solutions for this issue include:

div { border: 2px solid transparent;
      overflow: hidden;}

div table {display: block;}

The browser always collapses the margins with the nearby margins. When you give an overflow: hidden, it calculates all the contents inside it's box model and makes it confine to the content.

Sorry about my quick dirty handwriting...

This is the same case with floats too. Floated items do not occupy any space. Have a look here:

div {padding: 5px; border: 1px solid #666;}
<div>
  <div style="float: left;"></div>
</div>

But the same thing, if the parent has an overflow: hidden it doesn't happen:

div {padding: 5px; border: 1px solid #666;}
<div style="overflow: hidden;">
  <div style="float: left;"></div>
</div>

A whole big article about this concept here: What You Should Know About Collapsing Margins. The overflow is such a powerful property that it works for everything. But when it comes to position you need to use it carefully!

The position works like float, in the way that both do not take the layout. For eg., see the below snippet:

div {padding: 5px; border: 1px solid #666;}
<div>
  <div style="position: absolute;"></div>
</div>

Where, if you play with it in the wrong way:

div {padding: 5px; border: 1px solid #666;}
<div style="overflow: hidden; position: relative;">
  <div style="position: absolute;"></div>
</div>

The contents above get cut. Hope it is clear.

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252