CSS:使相对定位的父级适合绝对定位的子级的高度
我正在尝试制作一个简单的幻灯片。到目前为止,基本标记如下所示:
<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).
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
绝对定位的项目在逻辑上与其父项相关联,但在“物理上”不相关。它们不是布局的一部分,因此父项无法真正看到它们有多大。您需要自己编写大小代码,或者使用 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.
您可以使用 jquery 来使用它:
这将调整“slide-container”div 的大小,使其与“slide”div 具有相同的高度。
您可以通过在触发“调整大小”事件时调用该函数来使其响应:
希望这会有所帮助。
You can use this using jquery:
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:
Hope this helps.
执行此操作的完美方法是设置相对父级的尺寸,即高度和高度。宽度以适合绝对子项。
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.