绝对位置和隐藏溢出
我们有两个 DIV,一个嵌入另一个中。如果外部 DIV 不是绝对定位的,则绝对定位的内部 DIV 不遵守外部 DIV 的溢出隐藏。
#first {
width: 200px;
height: 200px;
background-color: green;
overflow: hidden;
}
#second {
width: 50px;
height: 50px;
background-color: red;
position: absolute;
left: 250px;
top: 250px;
}
<div id="first">
<div id="second"></div>
<div id="third"></div>
</div>
是否有机会使内部 DIV 服从外部 DIV 的溢出隐藏,而不将外部 DIV 设置为绝对位置(因为这会弄乱我们的完整布局)? 此外,我们的内部 DIV 的相对位置也不是一个选项,因为我们需要“扩展”表 TD。
#first {
width: 200px;
height: 200px;
background-color: green;
}
#second {
width: 50px;
height: 400px;
background-color: red;
position: relative;
left: 0px;
top: 0px;
}
<table id="first">
<tr>
<td>
<div id="second"></div>
</td>
</tr>
</table>
还有其他选择吗?
We have two DIVs, one embedded in the other. If the outer DIV is not positioned absolute then the inner DIV, which is positioned absolute, does not obey the overflow hidden of the outer DIV.
#first {
width: 200px;
height: 200px;
background-color: green;
overflow: hidden;
}
#second {
width: 50px;
height: 50px;
background-color: red;
position: absolute;
left: 250px;
top: 250px;
}
<div id="first">
<div id="second"></div>
<div id="third"></div>
</div>
Is there any chance to make the inner DIV obey the overflow hidden of the outer DIV without setting the outer DIV to position absolute (cause that will muck up our complete layout)?
Also position relative for our inner DIV isn't an option as we need to "grow out" of a table TD.
#first {
width: 200px;
height: 200px;
background-color: green;
}
#second {
width: 50px;
height: 400px;
background-color: red;
position: relative;
left: 0px;
top: 0px;
}
<table id="first">
<tr>
<td>
<div id="second"></div>
</td>
</tr>
</table>
Are there any other options?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
你只需像这样制作
div
:我希望这段代码能帮助你:)
You just make
div
s like this:I hope this code will help you :)
将外部
设置为
position:relative
,将内部设置为
position:absolute
。它应该适合你。Make outer
<div>
toposition: relative
and inner<div>
toposition: absolute
. It should work for you.外部 div 的
position:relative
怎么样?在隐藏内部的示例中。它也不会在布局中移动它,因为您没有指定顶部或左侧。What about
position: relative
for the outer div? In the example that hides the inner one. It also won't move it in its layout since you don't specify a top or left.绝对定位的元素实际上是根据相对父元素或最近找到的相对父元素来定位的。因此带有
overflow:hidden
的元素应该位于relative
和absolute
定位元素之间:An absolutely positioned element is actually positioned regarding a
relative
parent, or the nearest found relative parent. So the element withoverflow: hidden
should be betweenrelative
andabsolute
positioned elements:确保。
}
Make sure.
}
径向背景渐变遇到了这个问题。如果上述解决方案剪切了元素的部分内容,只需应用相对于 html 正文而不是直接父级的位置。
Ran into this issue with a radial background gradient. If the above solutions clip parts of your element, just apply the position relative to the html body instead of a direct parent.