So with the code below, when I change the margin of bar, the position of foo changes. But this only happens when the body is relatively positioned. If I statically position the body, then the position of foo no longer depends on the margin of bar. Can anyone tell me what's going on?
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
* {
margin:0;
}
body {
position:relative;
}
#foo {
position:absolute;
top:50px;
left:50px;
}
#bar {
margin:100px;
}
</style>
</head>
<body>
<div id="foo">asdf</div>
<div id="bar">asdf</div>
</body>
</html>
Absolutely positioned elements are anchored by their closest absolutely, or relatively positioned parent.
EDIT: For added clarity about the margin please see my comment below.
EDIT2: This also applies to elements with a fixed position.
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