重叠两个 div 并“清除”他们的父div

发布于 2024-09-04 16:45:15 字数 1209 浏览 4 评论 0原文

我的 CSS-fu 让我失望了: 我想做的是将两个子 div (具有 可变 高度)定位为重叠。

使用:位置:绝对;顶部:0px; left: 0px; 是我知道的唯一方法,将父级设置为 position:relative

这样做的问题是,子 div 会根据 CSS 规范从布局中取出,将父 div 缩小到高度:0px,这样我就无法清除该 div 并在下面放置任何内容。

下面我令人惊叹的 ASCII 艺术详细说明了我想要的内容...有什么想法吗?

顺便说一句,我需要这些 div 完全重叠以实现平滑的 jQuery 淡入淡出,并且可能尝试一些新的 Webkit 转换,例如 Apple 的 cardflip 演示: https://developer.apple.com/library/archive/ Samplecode/CardFlip/Introduction/Intro.html

如果有另一种方法可以让它们在 CSS 中完全重叠,和/或如果有另一种方法可以通过两个子 div 获得类似卡片翻转的操作(使用 jQuery 或 Webkit/CSS)高度可变,我洗耳恭听!

|-------------------------------------------------|
|  Parent div                                     |
|  |-------------------------------------------|  |
|  |                                           |  |
|  |          DIVS 1 & 2 (overlapped)          |  |
|  |                                           |  |
|  |-------------------------------------------|  |
|-------------------------------------------------|

...more content below, after clearing the parent...

My CSS-fu is letting me down here:
What I'd like to do is position two child divs (with variable heights) to be overlapping.

Using: position: absolute; top: 0px; left: 0px; is the only way I know how, with the parent set to position: relative.

The problem with this is that the child divs are taken out of the layout as per the CSS spec, shrinking the parent div to height: 0px, so that I can't clear that div and put any content below.

My amazing ASCII art below details what I'm going for... any ideas?

As an aside, I need these divs to be overlapping exactly for smooth jQuery fades, and maybe trying out some of the new Webkit transforms, a la Apple's cardflip demo:
https://developer.apple.com/library/archive/samplecode/CardFlip/Introduction/Intro.html

If there's another way to get them overlapped exactly in CSS, and/or if there's another way to get cardflip-like action (using jQuery or Webkit/CSS) with two child divs of variable heights, I'm all ears!

|-------------------------------------------------|
|  Parent div                                     |
|  |-------------------------------------------|  |
|  |                                           |  |
|  |          DIVS 1 & 2 (overlapped)          |  |
|  |                                           |  |
|  |-------------------------------------------|  |
|-------------------------------------------------|

...more content below, after clearing the parent...

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

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

发布评论

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

评论(2

水波映月 2024-09-11 16:45:15

将其中之一设置为 postition:absolute 怎么样?这样,一个子 div 仍将为父 div 提供高度,但另一个将从流程中取出。

.parent { position: relative; }
.child1 { position: absolute; top:0; left:0; }
.child2 { position: relative; }

只是一个 jQuery 建议:

var height1 = $(".child1").height();
var height2 = $(".child2").height();
if(height1 > height2)
    $(".child2").height(height1);
else
    $(".child1").height(height2);

现在您将在两个

之间实现无缝淡入淡出

How about just setting one of them to postition:absolute? That way one child div will still give height to the parent div, but the other will be taken out of the flow.

.parent { position: relative; }
.child1 { position: absolute; top:0; left:0; }
.child2 { position: relative; }

Just a jQuery suggestion:

var height1 = $(".child1").height();
var height2 = $(".child2").height();
if(height1 > height2)
    $(".child2").height(height1);
else
    $(".child1").height(height2);

And now you'll have seamless fades between the two <div>'s

旧人 2024-09-11 16:45:15

position:relative 和负边距怎么样?

我的头顶上浮现出:

.parent {}

.child1 {
    height: 200px;
}

.child2 {
    margin-top: -200px;
    height: 200px;
}

What about position: relative and negative margins?

Off the top of my head:

.parent {}

.child1 {
    height: 200px;
}

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