css - When should i use Absolute and Relative positioning? -
this question has answer here:
i have read absolute , relative positioning still haven't understand properly. what's practical use of absolute , relative positioning , when should use them exactly?
absolute: element positioned relative first positioned (not static) ancestor element
relative: element positioned relative normal position
this tells browser whatever going positioned should removed normal flow of document , placed in exact location on page. taken out of normal flow of document - won't affect how elements before or after in html positioned on web page.
if want position element 10ems top of document window, use "top" offset position there absolute positioning:
position: absolute; top: 10em;
this element display 10em top of page - no matter else displays there in normal flow.
relative positioning uses same 4 positioning properties absolute positioning. instead of basing position of element upon browser view port, starts element if still in normal flow.
for example, if have 3 paragraphs on web page, , third has position: relative style placed on it, it's position offset based on it's current location - not original sides of view port.
<p>paragraph 1.</p> <p>paragraph 2.</p> <p style="position: relative;left: 2em;">paragraph 3.</p>
in above example, third paragraph positioned 3em left side of container element, still below first 2 paragraphs. remain in normal flow of document, , offset slightly. if changed position: absolute; following display on top of it, because no longer in normal flow of document.
Comments
Post a Comment