可溢出:隐藏;用于隐藏未定位的内容但仍显示定位的内容?

发布于 2024-12-01 23:20:25 字数 1172 浏览 0 评论 0原文

首先,看看这个: http://jsfiddle.net/Udvgm/

HTML:

<div id="container">
    <div id="tooWide">
        <p>This is just way too wide! We should clip it.</p>
    </div>
    <div id="relativeParent">
        <div id="absoluteChild">
            <p>text</p>
        </div>
    </div>
</div>
<div id="container2">
    <p>This is some text which should be overlapped.</p>
</div>

CSS:

#container {
    background: grey;
    width: 450px;
}
#relativeParent{
    height: 40px;
    width: 400px;
    position: relative;
    background: green;
}

#absoluteChild{
    position: absolute;
    width: 100px;
    height: 60px;
    top: 0px;
    left: 10px;
    z-index: 2;
    background: blue;
}
#tooWide {
    background: red;
    width: 600px;
}

我想知道蓝色框(#absoluteChild)是否有可能溢出到灰色框(#container)之外,但红色框(#tooWide)的溢出部分被隐藏。

在您建议之前,请使用 overflow:hidden; Overflow-y:visible;(或overflow-x:hidden;overflow:visible;)会导致浏览器抛出一些不需要的滚动条。

First of all, take a look at this: http://jsfiddle.net/Udvgm/

HTML:

<div id="container">
    <div id="tooWide">
        <p>This is just way too wide! We should clip it.</p>
    </div>
    <div id="relativeParent">
        <div id="absoluteChild">
            <p>text</p>
        </div>
    </div>
</div>
<div id="container2">
    <p>This is some text which should be overlapped.</p>
</div>

CSS:

#container {
    background: grey;
    width: 450px;
}
#relativeParent{
    height: 40px;
    width: 400px;
    position: relative;
    background: green;
}

#absoluteChild{
    position: absolute;
    width: 100px;
    height: 60px;
    top: 0px;
    left: 10px;
    z-index: 2;
    background: blue;
}
#tooWide {
    background: red;
    width: 600px;
}

I am wondering if it is possible for the blue box (#absoluteChild) to overflow outside the grey box (#container), but the overflowing parts of the red box (#tooWide) to be hidden.

Before you suggest it, using overflow: hidden; overflow-y: visible; (or overflow-x: hidden; overflow: visible;) causes the browser to throw in some unwanted scrollbars.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

树深时见影 2024-12-08 23:20:25

不幸的是,在你的情况下这是不可能的。

在你建议之前,使用overflow:hidden;溢出-y:可见;
(或overflow-x:hidden;overflow:visible;)使浏览器
添加一些不需要的滚动条。

来自规范:

overflow-xoverflow-y 的计算值与
它们的指定值,除了一些与 visible 的组合
不可能:如果一个被指定为 visible 并且另一个被指定为
scrollauto,然后将 visible 设置为 auto

一个有用的页面,包含示例和更好的解释:http://www.brunildo.org/test/Overflowxy2 .html

Unfortunately, it's not possible in your circumstances.

Before you suggest it, using overflow: hidden; overflow-y: visible;
(or overflow-x: hidden; overflow: visible;) causes the browser to
throw in some unwanted scrollbars.

From the spec:

The computed values of overflow-x and overflow-y are the same as
their specified values, except that some combinations with visible
are not possible: if one is specified as visible and the other is
scroll or auto, then visible is set to auto.

A helpful page with examples and a better explanation: http://www.brunildo.org/test/Overflowxy2.html

且行且努力 2024-12-08 23:20:25

当您有一个带有 overflow:hidden 的块和一个带有 position:absolute 的块时,直到带有溢出的块和绝对定位块的所有父级都具有 position :static,绝对定位块不会被隐藏。

我不知道您想要如何处理代码,但是如果您想使用 overflow 定位块中的某个块,则可以使用 overflow< 将定位上下文移动到块之外/code>,因此绝对定位的块将是可见的并且可以定位在周围。

这是一个小提琴: http://jsfiddle.net/kizu/Udvgm/3/

When you have a block with overflow: hidden and a block with position: absolute inside of it, until block with overflow and all parents of absolute positioned block have position: static, the absolute positioned block won't be hidden.

I don't know that you want to do with your code, but if you want to position some block from the block with overflow, you can move positioning context outside of the block with overflow, so absolute positioned block would be visible and could be positioned around.

Here is a fiddle: http://jsfiddle.net/kizu/Udvgm/3/

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