这就是我现在拥有的:
* {
box-sizing: border-box;
}
.container {
width: 300px;
border: 1px solid #000;
}
.mainrow, .subrows div {
width: 100%;
padding: 10px
}
.mainrow {
background: #CCC;
}
.subrows div {
background: #DDD;
}
.subrows {
overflow: auto;
}
<div class="container">
<div class="mainrow">First row</div>
<div class="subrows">
<div>Sub row 1</div>
<div>Sub row 2</div>
<div>Sub row 3</div>
<div>Sub row 4</div>
<div>Sub row 5</div>
<div>Sub row 6</div>
<div>Sub row 7</div>
<div>Sub row 8</div>
<div>Sub row 9</div>
<div>Sub row 10</div>
</div>
</div>
我要防止.ops
扩展.container
。
我尝试了this post中描述的修复程序:
* {
box-sizing: border-box;
}
.container {
width: 300px;
padding-top: 100px;
border: 1px solid #000;
}
.mainrow, .subrows div {
width: 100%;
padding: 10px
}
.mainrow {
background: #CCC;
}
.subrows {
position: absolute;
width: 100%;
max-height: 100px;
top: 0;
left: 0;
overflow: auto;
}
.subrows div {
background: #DDD;
}
<div class="container">
<div class="mainrow">First row</div>
<div class="subrows">
<div>Sub row 1</div>
<div>Sub row 2</div>
<div>Sub row 3</div>
<div>Sub row 4</div>
<div>Sub row 5</div>
<div>Sub row 6</div>
<div>Sub row 7</div>
<div>Sub row 8</div>
<div>Sub row 9</div>
<div>Sub row 10</div>
</div>
</div>
但是显然这是行不通的。 .subrows
出现在.mainrow
上方,并且布局变形。应该怎么办?
答案 0 :(得分:3)
给出容器的相对位置并将top:100%添加到子行
* {
box-sizing: border-box;
}
.container {
width: 300px;
border: 1px solid #000;
position:relative;
}
.mainrow, .subrows div {
width: 100%;
padding: 10px
}
.mainrow {
background: #CCC;
}
.subrows {
position: absolute;
width: 100%;
max-height: 100px;
top: 100%;
margin-top: 1px; /* for container border to show */
left: 0;
overflow: auto;
}
.subrows div {
background: #DDD;
}
<div class="container">
<div class="mainrow">First row</div>
<div class="subrows">
<div>Sub row 1</div>
<div>Sub row 2</div>
<div>Sub row 3</div>
<div>Sub row 4</div>
<div>Sub row 5</div>
<div>Sub row 6</div>
<div>Sub row 7</div>
<div>Sub row 8</div>
<div>Sub row 9</div>
<div>Sub row 10</div>
</div>
</div>
从容器的边界可以看到,它尚未扩展为适合子行(为什么要这么做,我不知道,因为主行下方的任何内容现在都将被子行覆盖)
答案 1 :(得分:0)
* {
box-sizing: border-box;
}
.container {
width: 300px;
border: 1px solid #000;
}
.mainrow, .subrows div {
width: 100%;
padding: 10px
}
.mainrow {
background: #CCC;
}
.subrows {
position: relative;
width: 100%;
height: 100px;
top: 0;
left: 0;
overflow: auto;
}
.subrows div {
background: #DDD;
}
<div class="container">
<div class="mainrow">First row</div>
<div class="subrows">
<div>Sub row 1</div>
<div>Sub row 2</div>
<div>Sub row 3</div>
<div>Sub row 4</div>
<div>Sub row 5</div>
<div>Sub row 6</div>
<div>Sub row 7</div>
<div>Sub row 8</div>
<div>Sub row 9</div>
<div>Sub row 10</div>
</div>
</div>