CSS:使相对定位的父级适合绝对定位的子级的高度

发布于 2024-12-22 14:51:32 字数 1030 浏览 0 评论 0原文

我正在尝试制作一个简单的幻灯片。到目前为止,基本标记如下所示:

<h1>My Slideshow</h1>

<p>This paragraph behaves as expected.</p>

<div class="slide-container">
  <div class="slide">
    <h2>First Slide</h2>
    <p>Some stuff on this slide…</p>
  </div>

  <div class="slide">
    <h2>Second Slide</h2>
    <p>And some more stuff here…</p>
  </div>
</div>

<p>This paragraph will disappear beneath the stacked images.</p>

这是相应的 CSS:

.slide-container {
  position: relative;
}

.slide {
  position: absolute;
  top: 0;

  /* just for the looks */
  width: 20em;
  padding: 0 1em;
  border: 1px solid steelblue;
  background: white;
}

问题是,.slide-container 不适合其子级(或多个子级)的高度。幻灯片(参见屏幕截图)。

在此处输入图像描述

我知道我可以手动设置 .slide-container 的高度,但我想把它放在一个高度可变的流体网格中。有什么办法可以实现这一点吗?

I am trying to build a simple slideshow. So far, the basic markup looks like this:

<h1>My Slideshow</h1>

<p>This paragraph behaves as expected.</p>

<div class="slide-container">
  <div class="slide">
    <h2>First Slide</h2>
    <p>Some stuff on this slide…</p>
  </div>

  <div class="slide">
    <h2>Second Slide</h2>
    <p>And some more stuff here…</p>
  </div>
</div>

<p>This paragraph will disappear beneath the stacked images.</p>

This is the corresponding CSS:

.slide-container {
  position: relative;
}

.slide {
  position: absolute;
  top: 0;

  /* just for the looks */
  width: 20em;
  padding: 0 1em;
  border: 1px solid steelblue;
  background: white;
}

The problem is, that the .slide-container does not fit to the height of its child (or children) .slide (see screenshot).

enter image description here

I know i can set the height of the .slide-container manually, but i want to put this in a fluid grid, where the height is variable. Is there any way to achieve this?

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

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

发布评论

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

评论(3

一梦等七年七年为一梦 2024-12-29 14:51:32

绝对定位的项目在逻辑上与其父项相关联,但在“物理上”不相关。它们不是布局的一部分,因此父项无法真正看到它们有多大。您需要自己编写大小代码,或者使用 JavaScript 嗅探它并在运行时设置它。

Absolutely-positioned items are logically-associated with their parent, but not "physically". They're not part of the layout, so the parent item can't really see how big they are. You need to code the size yourself, or sniff it with JavaScript and set it at run-time.

无人接听 2024-12-29 14:51:32

您可以使用 jquery 来使用它:

    function sliderheight(){
        divHeight = $('.slide').height();
        $('.slide-container').css({'height' : divHeight});
    }
    sliderheight();

这将调整“slide-container”div 的大小,使其与“slide”div 具有相同的高度。

您可以通过在触发“调整大小”事件时调用该函数来使其响应:

   $(window).resize(sliderheight);

希望这会有所帮助。

You can use this using jquery:

    function sliderheight(){
        divHeight = $('.slide').height();
        $('.slide-container').css({'height' : divHeight});
    }
    sliderheight();

This will resize the 'slide-container' div to have the same height as the 'slide' div.

You can make it responsive by calling the function on the firing of a 'resize' event:

   $(window).resize(sliderheight);

Hope this helps.

青朷 2024-12-29 14:51:32

执行此操作的完美方法是设置相对父级的尺寸,即高度和高度。宽度以适合绝对子项。

http://learn.shayhowe.com/advanced- html-css/detailed-css-positioning/

这家伙写的足以理解CSS定位的基础知识。实际上,相对父级用于逻辑定位其所有绝对子级。但它不共享物理位置,因此它不会伸展自己以覆盖相对的孩子。

Perfect way of doing this is to set the relative parent's dimensions i.e. height & width to fit the absolute children.

http://learn.shayhowe.com/advanced-html-css/detailed-css-positioning/

This guy wrote enough to understand the basics of css positioning. Actually the relative parent is used to position all its absolute child logically. But it does not share the physical position as a result it does not stretch itself to cover the relative children.

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