绝对定位的div不隐藏

发布于 2024-10-09 22:52:10 字数 373 浏览 0 评论 0原文

现在

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

慈悲佛祖 2024-10-16 22:52:10

添加位置:相对于容器 div 元素。:

Exa:

  <style type="text/css">
        #container
        {
            width: 200px;
            background-color: red;
            height: 60px;
            color: white;
            position: relative;
            overflow: hidden;
        }

        #div1
        {
            background-color: blue;
            position: absolute;
            top:0px;
            left:0px;
            width: 300px;
        }
    </style>

<div id="container">   
        <div id="div1">This is div1</div> 
    <div>   

Add position: relative to container div element.:

Exa:

  <style type="text/css">
        #container
        {
            width: 200px;
            background-color: red;
            height: 60px;
            color: white;
            position: relative;
            overflow: hidden;
        }

        #div1
        {
            background-color: blue;
            position: absolute;
            top:0px;
            left:0px;
            width: 300px;
        }
    </style>

<div id="container">   
        <div id="div1">This is div1</div> 
    <div>   
绝對不後悔。 2024-10-16 22:52:10

添加

#container { position: relative; }

将隐藏溢出。

adding

#container { position: relative; }

will hide the overflow.

如痴如狂 2024-10-16 22:52:10

向元素添加绝对位置将从正常流中删除该元素。
它将自身绝对定位到相对定位的最近的父级。

这就是为什么在“容器”中添加“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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文