Skip to content Skip to sidebar Skip to footer

Html And Css Margin Issues

applying to my div #logo margin value forces all my other elements to follow this margin. It's in the 'logo' div margin value. while setting it to 0 it's all comes up. i just want

Solution 1:

#logo { position: relative; top: 200px; }

Relative positioning won't affect the position of the other elements.

Solution 2:

As said by @Uchit Kumar it's due to margin collapsing as you can read here:

Two margins are adjoining if and only if:

... ...

both belong to vertically-adjacent box edges, i.e. form one of the following pairs:

top margin of a box and top margin of its first in-flow child

And since all the child element are floated, the first in-flow child is logo so its margin will collapse with the one of the container (the body).

So the easiest fix would be to make the third element to float also:

#logo {
  width: 100px;
  height: 100px;
  margin: auto;
  margin-top: 200px;
  float:left
}

#label {
  height: 50px;
  float: right;
}

nav {
  float: left;
  margin-left: 30px;
}

ulli {
  display: inline;
  text-decoration: none;
  font-size: 40px;
  margin: 10px;
  border-bottom: 20px red;
}
<body><divid="label"><imgsrc="https://lorempixel.com/100/100/"></div><nav><ul><li><ahref="#">About</a></li><li><ahref="#">Portfolio</a></li><li><ahref="#">Contacts</a></li></ul></nav><divid="logo"><imgsrc="https://lorempixel.com/100/100/"></div></body>

But this solution is not the suitable one as it will change the behavior of the logo and maybe floating is not needed here.

Another fix to avoid margin collapsing is to add a small padding-top to container (the body):

body {
 margin:0;
 padding-top:1px;/*added this */ 
}
#logo {
  width: 100px;
  height: 100px;
  margin: auto;
  margin-top: 300px;
  clear:both;
}

#label {
  height: 50px;
  float: right;
}

nav {
  float: left;
  margin-left: 30px;
}

ulli {
  display: inline;
  text-decoration: none;
  font-size: 40px;
  margin: 10px;
  border-bottom: 20px red;
}
<body><divid="label"><imgsrc="https://lorempixel.com/100/100/"></div><nav><ul><li><ahref="#">About</a></li><li><ahref="#">Portfolio</a></li><li><ahref="#">Contacts</a></li></ul></nav><divid="logo"><imgsrc="https://lorempixel.com/100/100/"></div></body>

Or simply use margin-bottom in the previous element instead:

body {
  margin: 0;
}

#logo {
  width: 100px;
  height: 100px;
  margin: auto;
  clear: both;
}

#label {
  height: 50px;
  float: right;
}

nav {
  float: left;
  margin-left: 30px;
  margin-bottom: 300px;
}

ulli {
  display: inline;
  text-decoration: none;
  font-size: 40px;
  margin: 10px;
  border-bottom: 20px red;
}
<body><divid="label"><imgsrc="https://lorempixel.com/100/100/"></div><nav><ul><li><ahref="#">About</a></li><li><ahref="#">Portfolio</a></li><li><ahref="#">Contacts</a></li></ul></nav><divid="logo"><imgsrc="https://lorempixel.com/100/100/"></div></body>

Here is a very usefull link were you can find a more complete explanation about collapsing margin and floating:

margin-top not working with clear: both

Solution 3:

quick fix is ..

#logo  {
    width: 400px;
    height: 400px;
    margin: auto;
    margin-top: 200px;
    float: left
}

its may be due to margin collapsing... (not sure) read this.. https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Box_Model/Mastering_margin_collapsing

Post a Comment for "Html And Css Margin Issues"