i know what is absolute & relative position but some points are still not cleared to me. for reference
css:
.rel{ position:relative; background:red; } .abs{ position:absolute; background:blue; }
html:
<div class="rel">rel</div> <div class="abs">abs</div>
now points are :
relative div takes 100% width automatically but absolute div only takes content width. why?
when i give height 100% there is no effect in the relative div but absolute div takes 100% height. why?
when i give margin-top:30px it's shift absolute div also but when i give top:30px then only relative div shift. why?
when i don't give top:0 , left:0
to the absolute div it's takes above div height. why?
Relative - the element is positioned relative to its normal position. Absolute - the element is positioned absolutely to its first positioned parent.
Because absolutely positioned elements do not behave as block level elements and do not flow after each other like normal a <div> does. Show activity on this post. Because the element, which you give absolute position take the width from his parent and didn't behave as a block element.
Summary: 1. Relative is always in proportion to a whole. Absolute is the total of all existence.
Absolute positioning defines the position of a given bounding box from the top and left side margins of the web page. This not only allows objects to be placed in an exact location, it also allows objects to be placed one on top of another.
Setting position:absolute
removes the element in question from the normal flow of the document structure. So unless you explicitly set a width it won't know how wide to be. you can explicitly set width:100%
if that is the effect you're after.
An element with position:relative
on the whole behaves in the same way a normal position:static
element does. Therefore, setting height:100%
will have no effect unless the parent element has a defined height. In contrast absolute positioned elements are removed from the document flow so are free to adjust to whatever height their containing element currently has.
This is probably something to do with the parent elements in your HTML but I can't help further unless you provide the full HTML and CSS of your page.
The default value of the top and left properties is auto. This means the browser will calculate these settings for you and set them to where the element would be rendered if it didn't have position:absolute
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With