Top & Left Vs Margin-top & Margin-left
Solution 1:
Well, the main difference is that absolutely positioned elements are pulled out of the content flow.
But also with relatively positioned elements, the surrounding content may become obfuscated.
Margins on the other hand are added to the element's size while the surrounding content flows accordingly.
See this sample fiddle with some differences.
Surely there are situations where both deliver the same results, but under normal conditions, these two aproaches aren't simply exchangeable nor comparable.
Solution 2:
A There's a semantic difference. Box offsets like top
specify how far a box's margin edge (dotted edges in the image below) is offset from a reference edge (For absolutely positioned elements, that's the top edge of the box's containing block). Margin properties like margin-top
specify the width of the margin area of a box (The distance TM
between the dotted edge and and solid edge in the image below).
Btop
and left
apply only to positioned elements (elements with position
set to anything other than static
) while margin-top
and margin-left
apply to all elements except elements with table display types other than table-caption, table and inline-table.
Solution 3:
Margin describes the space between your box and adjacent boxes. Boxes that are positioned relatively (i.e. that are part of the normal flow) will keep the sufficient space between them that each one's "margin" requirements are met (called "margin collapsing").
top
and left
on the other hand are positional attributes that specify where your box is located; for absolutely positioned boxes the values are taken relative to the nearest containing box which is itself absolutely positioned. The top/left/bottom/right attributes specify the location of the respective edge of your box including its margin.
In short, the two are entirely different concepts. For normally flowed boxes you should use margin
to control the spacing between neighbouring boxes.
Post a Comment for "Top & Left Vs Margin-top & Margin-left"