这是我的代码:
.main{
border:1px solid black;
width: 100px;
height: 100px; /* in reality this isn't based on the pixel, it is % */
}
.parent{
border:1px solid blue;
position: relative;
overflow:hidden;
height: inherit;
width: inherit;
}
.child{
border:1px solid red;
margin-top: 1px;
padding: 4px 8px 30px 7px;
overflow: scroll;
height: 100%;
}
span{
color: #222;
display: block;
font-size: 13px;
line-height: 30px;
padding-bottom: 1px;
text-align: center;
font-family: BYekan,'BYekan', Tahoma;
background-color: #F7F7F7;
}
<div class="main">
<div class="parent">
<span>title</span>
<div class="child">
one<br> two<br> three<br> four<br> five<br> six<br>
</div>
</div>
</div>
解释
如您所见,div.child
元素不在视野范围内,six
的单词现在不可见。我该如何解决?
实际上那是因为span
的高度。 height
的{{1}}为div.child
,因此其100%
与height
相同。虽然div.parent
顶部有span
,但毫无疑问,div.parent
的{{1}}应小于height
。
注1:我可以使用div.child
100%
解决问题。但我不想用它。因为我网站的大多数用户都使用IE7这样的旧浏览器。
注2:正如我在上面的代码段中所评论的那样,box-sizing: border-box;
的高度基于div.child
。所以我根本不能使用div.main
,因为基于像素没有任何东西。旧浏览器也不支持%
。
注3: calc()
的最终calc()
应为height
,而不是更多。
答案 0 :(得分:2)
.child 视图的高度必须为:
高度= 100% - (30 + 2 + 4 + 4 + 1 + 2)
.child{
border:1px solid red;
margin-top: 1px;
padding: 4px 8px 4px 7px;
overflow: scroll;
height: calc(100% - 43px);
}
.main{
border:1px solid black;
width: 100px;
height: 100px; /* in reality this isn't based on the pixel, it is % */
}
.parent{
border:1px solid blue;
position: relative;
overflow:hidden;
height: inherit;
width: inherit;
}
.child{
border:1px solid red;
margin-top: 1px;
padding: 4px 8px 4px 7px;
overflow: scroll;
height: calc(100% - 42px);
}
span{
color: #222;
display: block;
font-size: 13px;
line-height: 30px;
padding-bottom: 1px;
text-align: center;
font-family: BYekan,'BYekan', Tahoma;
background-color: #F7F7F7;
}
<div class="main">
<div class="parent">
<span>title</span>
<div class="child">
one<br> two<br> three<br> four<br> five<br> six<br>
</div>
</div>
</div>
不使用calc()
.main{
border:1px solid black;
width: 100px;
height: 100px; /* in reality this isn't based on the pixel, it is % */
}
.parent{
border:1px solid blue;
position: relative;
overflow:hidden;
height: inherit;
width: inherit;
}
.child{
border:1px solid red;
margin-top: 1px;
padding: 4px 8px 4px 7px;
overflow: scroll;
position:absolute;
top:0;
bottom: 0;
width:100%;
margin-top:30px;
}
span{
color: #222;
display: block;
font-size: 13px;
line-height: 30px;
padding-bottom: 1px;
text-align: center;
font-family: BYekan,'BYekan', Tahoma;
background-color: #F7F7F7;
}
<div class="main">
<div class="parent">
<span>title</span>
<div class="child">
one<br> two<br> three<br> four<br> five<br> six<br>
</div>
</div>
</div>
突出显示更改。
.main{
border:1px solid black;
width: 100px;
height: 100px; /* in reality this isn't based on the pixel, it is % */
}
.parent{
border:1px solid blue;
position: relative;
overflow:hidden;
height: inherit;
width: inherit;
}
.child{
border:1px solid red;
margin-top: 1px;
/* removed */ padding: 4px 8px 30px 7px;
/* added */ padding: 4px 8px 4px 7px;
overflow: scroll;
/* removed */ height: 100%;
/* added */ position:absolute;
/* added */ top:0;
/* added */ bottom: 0;
/* added */ width:100%;
/* added */ margin-top:30px;
}
span{
color: #222;
display: block;
font-size: 13px;
line-height: 30px;
padding-bottom: 1px;
text-align: center;
font-family: BYekan,'BYekan', Tahoma;
background-color: #F7F7F7;
}
<div class="main">
<div class="parent">
<span>title</span>
<div class="child">
one<br> two<br> three<br> four<br> five<br> six<br>
</div>
</div>
</div>
.main{
border:1px solid black;
width: 100px;
height: 100px; /* in reality this isn't based on the pixel, it is % */
}
.parent{
border:1px solid blue;
position: relative;
overflow:hidden;
height: inherit;
width: inherit;
}
.child{
border:1px solid red;
margin-top: 1px;
/* removed padding: 4px 8px 30px 7px;*/
/* added */ padding: 4px 8px 4px 7px;
overflow: scroll;
/* removed height: 100%;*/
/* added */ position:absolute;
/* added */ top:0;
/* added */ bottom: 0;
/* added */ width:100%;
/* added */ margin-top:30px;
}
span{
color: #222;
display: block;
font-size: 13px;
line-height: 30px;
padding-bottom: 1px;
text-align: center;
font-family: BYekan,'BYekan', Tahoma;
background-color: #F7F7F7;
}
<div class="main">
<div class="parent">
<span>title</span>
<div class="child">
one<br> two<br> three<br> four<br> five<br> six<br>
</div>
</div>
</div>
答案 1 :(得分:1)
让孩子div变小
.main{
border:1px solid black;
width: 100px;
height: 100px; /* in reality this isn't based on the pixel, it is % */
}
.parent{
border:1px solid blue;
position: relative;
overflow:hidden;
height: inherit;
width: inherit;
}
.child{
border:1px solid red;
margin-top: 1px;
padding: 4px 8px 30px 7px;
overflow: scroll;
height: 50%; /*make it smaller*/
}
span{
color: #222;
display: block;
font-size: 13px;
line-height: 30px;
padding-bottom: 1px;
text-align: center;
font-family: BYekan,'BYekan', Tahoma;
background-color: #F7F7F7;
}
&#13;
<div class="main">
<div class="parent">
<span>title</span>
<div class="child">
one<br> two<br> three<br> four<br> five<br> six<br>
</div>
</div>
</div>
&#13;