I thought a general rule is that, whenever a div foo
has position: relative
, then if none of the parent and ancestor has any non-static position
(so need to have one of relative, absolute, or fixed
), then, the div foo
now will be position relative to the overall document.
But in the following page:
http://jsfiddle.net/4RcEn/6/
<div id="box1"></div>
<div id="box2">
<div id="box3">some text inside some text</div>
</div>
<style>
#box1 { width: 300px; height: 100px; background: #ffd; margin-left: 60px }
#box2 { width: 300px; height: 100px; background: #fa0; margin-left: 60px }
#box3 { width: 100px; height: 80px; background: #af0; position:
absolute; left: 20px; }
</style>
box3
actually behave like this: the left
is 20px
and is relative to the document, but top
is auto
(by default), and is in fact relative to the container div. Only when top
is set to 0
, 0px
, or some other value, then it is relative to the document. What rule is governing this?
P.S. with the rules in the spec, I don't see a rule that says: if the top
or left
is not specified, then the behavior is such and such. So this is a de facto standard that if it is not specified, then if there is no "containing block (which is defined as non-static positioned block), then it won't be relative to the "initial block"?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…