我有一个非常具体的要求,即父div
与absolute:position
和孩子div
与absolute position
和父div
我有overflow:hidden
因此,width
的额外div
隐藏了,但这不起作用,我知道父div
必须与position:relative
匹配,但是根据我的代码的当前结构,我无法将父div
position
从absolute
更改为relative
,只是想知道是否有办法处理此问题? / p>
以下是JSfiddle示例,我希望浅红色框隐藏在父div
下,以便子div
位于父级内。
这是我的代码
.wrapper {
position: absolute;
left: 100px;
top: 100px;
width: 400px;
height: 350px;
background: #666666;
padding: 10px;
}
.wrapper-inner {
position: absolute;
width: 450px;
height: 380px;
background: #fecccc;
}

<div class="wrapper">
<div class="wrapper-inner">
Content goes here
</div>
</div>
&#13;
答案 0 :(得分:1)
您似乎错过了在overflow:hidden
课程中添加wrapper
。
.wrapper{position:absolute; left:100px; top:100px; width:400px; height:350px; background:#666666; padding:10px; overflow:hidden;}
.wrapper-inner{position:absolute; width:450px; height:380px; background:#fecccc;}
&#13;
<div class="wrapper">
<div class="wrapper-inner">
Content goes here
</div>
</div>
&#13;
答案 1 :(得分:0)
根据您的要求检查此代码:
.wrapper {
position: absolute;
left: 100px;
top: 100px;
width: 400px;
height: 350px;
background: #666666;
padding: 10px;
overflow: hidden;
}
答案 2 :(得分:0)
请查看我的fiddle
.wrapper{position:absolute; left:100px; top:100px; width:400px; height:350px; overflow:hidden; background:#666666; padding:10px;}
.wrapper-inner{position:relative; width:100%; height:100%; background:#fecccc; overflow-y:scroll;}
<div class="wrapper">
<div class="wrapper-inner">
Content goes here Content goes hereContent goes hereContent goes hereContent goes hereContent goes hereContent goes hereContent goes hereContent goes hereContent goes hereContent goes hereContent goes hereContent goes hereContent goes hereContent goes hereContent goes hereContent goes hereContent goes hereContent goes hereContent goes hereContent goes hereContent goes hereContent goes hereContent goes hereContent goes hereContent goes hereContent goes hereContent goes hereContent goes hereContent goes hereContent goes hereContent goes hereContent goes hereContent goes hereContent goes hereContent goes hereContent goes hereContent goes hereContent goes hereContent goes hereContent goes hereContent goes hereContent goes hereContent goes hereContent goes hereContent goes hereContent goes hereContent goes hereContent goes hereContent goes hereContent goes hereContent goes hereContent goes hereContent goes hereContent goes hereContent goes hereContent goes hereContent goes hereContent goes hereContent goes here
</div>
</div>
答案 3 :(得分:0)
除非我误解了您的要求,否则您不希望看到子div延伸到父div之外。你提到过overflow:hidden
,但实际上并不存在于你的小提琴中。将它添加到.wrapper
类似乎完成了我认为您的要求。
答案 4 :(得分:0)
CSS
.wrapper-inner {
position: absolute; /* positioned wrt it parent */
top:10px; /* account for 10px padding in parent */
bottom:10px;
left:10px;
right:10px;
/* width: 450px;
height: 380px; */
background: #fecccc;
}
答案 5 :(得分:0)
您错过了父包装中的 overflow:hidden
:
.wrapper {
position: absolute;
left: 100px;
top: 100px;
width: 400px;
height: 350px;
background: #666666;
padding: 10px;
overflow: hidden
}
.wrapper-inner {
position: absolute;
width: 450px;
height: 380px;
background: #fecccc;
}
简而言之,您已回答了自己的问题! :)