如果要计算一个box的位置以及宽高,那么必须知道它的containing box,然后才能在containing box的基础上计算出box的位置、宽高。选取containing box的方法是固定的,具体如下:
1)文档根节点的containing box也被称为initial containing box。Initial containing box是以文档左上角作为原点,view-port的宽高作为自己宽高的矩形框。
2)其他in-flow box的containing box是距离自己最近的、作为容器的block box的content边界。
3)fixed box的containing box就是窗口的view-port,没有固定位置。因此,fixed box永远可见,也会随着滚动条滚动。
4)absolute box的containing box与距离自己最近的absolute、fixed、relative祖先的box有关。如果这个祖先是inline结点,那么containing box是第一个child box 的padding边界与最后一个child box 的padding边界做并运算得到的。如果这个祖先是block节点,那么containing box就是祖先的block box的padding边界。如果absolute box不存在absolute、fixed、relative祖先,那么absolute box的containing box就是initial containing box。
如下的html代码。
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<HTML>
<HEAD>
<TITLE>Illustration of containing blocks</TITLE>
</HEAD>
<BODY id="body">
<DIV id="div1">
<P id="p1">This is text in the first paragraph...</P>
<P id="p2">This is text <EM id="em1"> in the
<STRONG id="strong1">second</STRONG> paragraph.</EM></P>
</DIV>
</BODY>
</HTML>
Box对应的文档结点 | Containing box对应的文档结点 |
HTML | initial containing box |
body | html |
div1 | body |
p1 | div1 |
p2 | div1 |
em | p2 |
strong | p2 |
如果这个网页增加一条css规则
#div1 { position: absolute; left: 50px; top: 50px }
那么box, containing box的对应规则变为,
Box对应的文档结点 | Containing box对应的文档结点 |
HTML | initial containing box |
body | html |
div1 | initial containing box |
p1 | div1 |
p2 | div1 |
em | p2 |
strong | p2 |
如果再增加一条css规则,
#em1 { position: absolute; left: 100px; top: 100px }
Box对应的文档结点 | Containing box对应的文档结点 |
HTML | initial containing box |
body | html |
div1 | initial containing box |
p1 | div1 |
p2 | div1 |
em | div1 |
strong | em |
原值 | 新值 |
inline-table | table |
Inline, table-row-group, table-column, table-column-group, table-header-group,table-footer-group, table-row, table-cell, table-caption, inline-block | Block |
其他 | 不变 |