I have two divs inside another div, and I want to position one child div to the top right of the parent div, and the other child div to the bottom of the parent div using css. Ie, I want to use absolute positioning with the two child divs, but position them relative to the parent div rather than the page. How can I do this?
Sample html:
<div id="parent">
<div id="child1"></div>
<div id="child2"></div>
</div>
This works because
position: absolutemeans something like "usetop,right,bottom,leftto position yourself in relation to the nearest ancestor who hasposition: absoluteorposition: relative."This is the same working principle for height or width, when you use a relative unit. The value is applied in relation to the containing block.
So we make
#parenthaveposition: relative, and the children haveposition: absolute, then usetopandbottomto position the children.