To begin, ensure that your main div has a CSS property of position: relative;
.
Next, set the child div to have a CSS property of position: absolute;
. This will position the child div absolutely in relation to the nearest ancestor element with a specified position (in this case, its direct parent). It's worth noting that position: relative;
positions an element relative to where it would normally render. If you're interested in learning more about CSS positioning, check out this informative article on CSS Tricks.
After setting the child div, you can use the top, right, bottom, and left properties to precisely position the div as desired.
To adjust for any padding, you'll need to directly set the top and left values to counteract the default position, which takes padding into consideration.
Below is an example for you to experiment with:
https://jsfiddle.net/abc123xyz/9/
Here is the crucial CSS code:
.parent {
padding: 20px;
position: relative; /* enables proper child element positioning */
}
.child {
position: absolute; /* offers easy control over child position */
top: 0; /* eliminates impact of parent padding on positioning */
left: 0; /* same as above */
}
If you prefer the top value to remain calculated based on content, you can remove top: 0;
like in this example where the child retains its default rendering position except where it's overridden by the left value:
https://jsfiddle.net/abc123xyz/10/