绝对定位的div不隐藏
现在
<div id="container">
<div id="div1"></div>
<div>
,让我们假设:
- “容器”的宽度为 300px,
- “容器”的溢出:隐藏;
- “div1”的宽度为 1000px;
- “div1”是绝对定位的,top:0px,left:0px;
问题:
“div1”没有隐藏,它溢出了“容器”,但仍然显示:(。
如果我简单地删除“position:absolute”,它就会起作用。
如何隐藏“div1”的溢出?
I have this
<div id="container">
<div id="div1"></div>
<div>
Now, let's assume that:
- the "container" has a width of 300px
- the "container" has overflow: hidden;
- the "div1" has a width of 1000px;
- the "div1" is absolute positioned, top:0px,left:0px;
The problem:
The "div1" is not hidden, it overflows the "container" but it's still showing :(.
If I simply remove the "position:absolute" it will work.
How can I hide the overflow of "div1" ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
添加位置:相对于容器 div 元素。:
Exa:
Add position: relative to container div element.:
Exa:
添加
将隐藏溢出。
adding
will hide the overflow.
向元素添加绝对位置将从正常流中删除该元素。
它将自身绝对定位到相对定位的最近的父级。
这就是为什么在“容器”中添加“position:relative”就会达到预期的效果。
Adding position absolute to an element will remove that element from the normal flow.
It will position itself absolute to the closest parent that is relatively positioned.
This is why adding "position:relative" to the "container" will achieve the desired effect.