[CSS] containing block

(1)Content Edge and Padding Edge
The content edge of a box is defined by the outer limits of the content area—it doesn’t include any padding that may exist outside the content.

The padding edge of a box is defined by the outer limits of the padding area—it doesn’t include any borders that may exist outside the padding. If a box has no padding, the padding edge is equivalent to the content edge.

(2)initial containing block
The containing block for the root element is called the initial containing block, and has the same dimensions as the viewport for continuous media (such as the screen) and the page area for paged media (such as print).

(3)rules
The containing block for any other element box is determined by the value of the position property for that element.

a)
If the value of the position property is static (the default) or relative, the containing block is formed by the <u>edge of the content box</u> of the nearest ancestor element whose display property value is one of:

block
inline-block
list-item
run-in (only in a block formatting context; see Formatting Concepts)
table
table-cell

b)
If the value of the position property is absolute, the containing block is the nearest positioned ancestor—in other words, the nearest ancestor whose position property has one of the values absolute, fixed, or relative. The containing block is formed by the <u>padding edge</u> of that ancestor.

c)
If the value of the position property is fixed, the containing block is the <u>viewport</u> (for continuous media) or the page box (for paged media).

例:

html,body,.box1,.box2{
    margin:10px;
    padding:10px;
    border:10px solid;
}
.box2{
    position:absolute;
}



可以看到:
position:absolute;的元素,虽然脱离了文档流,但是还是在原来该出现的位置(left,top)
margin的外边界,仍然位于本来该出现的位置处。

我们可以设置元素的top和left样式,来指定元素相对于它的containing block的位移。

例:
(1)未指定包含块,以html的<u>margin外边界</u>为边界。

.box2{
    left:0;
}

(2)包含块是html,以html的border内边界为边界。

.box2{
    left:0;
}
html{
    position:relative;
}

(3)包含块是body,以body的border内边界为边界。

.box2{
    left:0;
}
body{
    position:relative;
}

(4)包含块为.box1,以.box1的border内边界为边界。

.box2{
    left:0;
}
.box1{
    position:relative;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 本周的周作业还真是奇特!肖爷太有才了!说说那些年大人们说的不能做的事和不能吃的食物。 我小时候喜欢吃鱼籽,妈妈说不...
    林子Amy阅读 1,571评论 0 1
  • 转眼又是一年初冬,在风里,在雨里,在回忆里,我依然平静的过着。有些人,有些事,留在记忆里,最好不过。寒风拂起书页,...
    鸣菁姐姐阅读 3,226评论 1 2
  • 养把头发真累,三天两头要伺候它,它一不顺心吧,就糙给你瞧油给你看,你说咋办?不鸟它吧,你是舒坦了,但你的门面就瓦特...
    淡粉和深蓝阅读 1,531评论 0 0
  • 11.14开始 16:17 比起睡一下午,你已经有进步了,坚持! 4.4 20.13 开始写作业,写了高数作业,...
    玩去你阅读 1,517评论 0 0

友情链接更多精彩内容