如何强制子元素适应父元素,即在 #content 上显示滚动条?
HTML 代码如下:
CSS(层叠样式表):
HTML 代码如下:
<div id="parent">
<div id="header">
varied-sized header
</div>
<div id="content">
the real content<br>
this can be so long<br>
that it doesn't fit into the parent<br>
so this should get a scroll bar<br>
so that the user can... scroll it down<br>
</div>
</div>
CSS(层叠样式表):
#parent {
background-color: red;
display: inline-block;
width: 200px;
height: 300px;
}
#header {
background-color: yellow;
font-size: 32px;
margin: 20px;
}
#content {
background-color: green;
font-size: 24px;
margin: 20px;
overflow: auto;
}
编辑:
#content不指定高度能行吗?计算该属性的正确值可能很困难和繁琐(如果其他事物发生变化)。