Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between position and margin [duplicate]

Tags:

css

What's the difference between

position : relative;
bottom : 10px;

And :

margin-top: -10px;

In my opinion they are the same isn't ?

like image 554
Hayi Avatar asked Mar 04 '14 11:03

Hayi


4 Answers

No; if you give something position: relative and pull it up 10 pixels, it will move, but continue to occupy (in terms of its box) the 10 pixels below it that it previously occupied.

If you give something margin and pull it up 10 pixels, it moves up and brings everything underneath it with it, too.

Fiddle: http://jsfiddle.net/2cY8F

like image 118
Mitya Avatar answered Sep 24 '22 21:09

Mitya


When you are using position:relative then it positions the element at it's normal location in the document flow, and then subsequently moves it to the offset position. The space it originally occupies is reserved in the layout (left empty).

FIDDLE DEMO for position: relative

Margins however is not positioning properties, infact it is a part of box model used to create block boxes on the page. It is the space that exists between the border edge of an element to that of next adjacent element.

FIDDLE DEMO for margin

Also check Negative margins vs relative positioning

like image 23
Rahul Tripathi Avatar answered Sep 24 '22 21:09

Rahul Tripathi


Margin is that space between the edge of an element's box and the edge of the complete box, such as the margin of a letter.

The position property specifies the type of positioning method used for an element (static, relative, absolute or fixed).

In your first case you have a relatively positioned element. For relatively positioned elements, the bottom property sets the bottom edge of an element to a unit above/below its normal position.

Your second case consists entirely of setting the margin-top which changes the "bounding box" of the element.

like image 36
turnt Avatar answered Sep 24 '22 21:09

turnt


If you use position, the element appears 10px from the top. If you use margin, the element will sort of 'push' itself from the top, adding space around it.

I will add an image to explainenter image description here

The grey-black box shows your div. The first half of the picture shows position, the second shows margin, where the yellow part is the margin.

like image 22
Maartje Avatar answered Sep 28 '22 21:09

Maartje