Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does 10px + 10px = 10px?

I have the following script where the margin for the . portlet class is behaving strangely:

http://jsfiddle.net/mYx5y/19/

it should be adding 10px around each portlet, which should mean a gap of 20px between portlets. For some reason, I am only getting 20px if a portlet has a widget to the right or left of itself, but only 10px if the portlet has another portlet above or below it.

Why am I getting 10px vertically?

like image 853
oshirowanen Avatar asked Mar 14 '11 11:03

oshirowanen


People also ask

What does margin 10px 5px 15px 20px mean?

margin: 10px 5px 15px 20px; top margin is 10px. right margin is 5px. bottom margin is 15px. left margin is 20px.

What does padding 10px mean?

If the padding property has four values: top padding is 10px. right padding is 5px. bottom padding is 15px. left padding is 20px.

Why do we use margin and padding?

A margin is the space around an element, while padding is the space inside of an element. You use a margin to ensure that other elements aren't too close to the element in question. You use padding to make space inside of the element itself.

What is the purpose of margin in HTML?

Margins are used to create space around elements, outside of any defined borders. This element has a margin of 70px.


2 Answers

Because margins don't add up, they collapse. If you have two elements A and B, A margin=10px and B margin=15px, the spacing between A and B will be 15px.

Two solutions:

  1. Use margin-bottom: 20px, margin-top: 0px
  2. Wrap them into a container and apply padding: 10px 0;
like image 169
Marcel Jackwerth Avatar answered Oct 29 '22 04:10

Marcel Jackwerth


Because margins collapse, they are not added, just max()-ed (the biggest margin value of neighbouring objects will be used). Use padding if the space is part of your element.

like image 21
vbence Avatar answered Oct 29 '22 02:10

vbence