129

我有一些容器,它们的孩子只是绝对/相对定位。如何设置容器高度以便他们的孩子在里面?

这是代码:

HTML

<section id="foo">
    <header>Foo</header>
    <article>
        <div class="one"></div>
        <div class="two"></div>
    </article>
</section>    

<div style="clear:both">Clear won't do.</div>
<!-- I want to have a gap between sections here -->

<section id="bar">
    <header>bar</header>
    <article>
        <div class="one"></div><div></div>
        <div class="two"></div>
    </article>
</section>  

CSS

article {
    position: relative;
}

.one {
    position: absolute;
    top: 10px;
    left: 10px;
    background: red;
    width: 30px;
    height: 30px;
}

.two {
    position: absolute;
    top: 10px;
    right: 10px;
    background: blue;
    width: 30px;
    height: 30px;
}

这是一个jsfiddle。我希望“条形”文本出现在 4 个正方形之间,而不是在它们后面。

http://jsfiddle.net/Ht9Qy/

有什么简单的修复方法吗?

请注意,我不知道这些孩子的身高,也无法为容器设置 height: xxx。

4

7 回答 7

97

如果我理解您要正确执行的操作,那么我认为在保持孩子绝对定位的同时使用 CSS 是不可能的。

绝对定位的元素完全从文档流中移除,因此它们的尺寸不能改变其父元素的尺寸。

如果你真的必须在保持子元素为 的同时实现这种position: absolute效果,你可以使用 JavaScript 通过在渲染后找到绝对定位的子元素的高度,并使用它来设置父元素的高度。

或者,只需使用float: left/float:right和边距来获得相同的定位效果,同时将子级保持在文档流中,然后您可以overflow: hidden在父级(或任何其他 clearfix 技术)上使用以使其高度扩展到其子级的高度。

article {
    position: relative;
    overflow: hidden;
}

.one {
    position: relative;
    float: left;
    margin-top: 10px;
    margin-left: 10px;
    background: red;
    width: 30px;
    height: 30px;
}

.two {
    position: relative;
    float: right;
    margin-top: 10px;
    margin-right: 10px;
    background: blue;
    width: 30px;
    height: 30px;
}
于 2012-11-24T21:54:47.127 回答
37

这是我的解决方法,
在您的示例中,您可以添加具有“相同样式”的 .one 和 .two 元素的第三个元素,但没有绝对位置并且具有隐藏的可见性:

HTML

<article>
   <div class="one"></div>
   <div class="two"></div>
   <div class="three"></div>
</article>

CSS

.three{
    height: 30px;
    z-index: -1;
    visibility: hidden;
    width:0!important; /* if you got unnecessary horizontal scroll*/
}
于 2017-09-18T14:05:42.003 回答
4

这是一个迟到的答案,但是通过查看源代码,我注意到当视频全屏时,“mejs-container-fullscreen”类被添加到“mejs-container”元素中。因此,可以基于此类更改样式。

.mejs-container.mejs-container-fullscreen {
    // This rule applies only to the container when in fullscreen
    padding-top: 57%;
}

此外,如果您希望使用 CSS 使您的 MediaElement 视频流畅,下面是 Chris Coyier 的一个绝妙技巧:http: //css-tricks.com/rundown-of-handling-flexible-media/

只需将其添加到您的 CSS 中:

.mejs-container {
    width: 100% !important;
    height: auto !important;
    padding-top: 57%;
}
.mejs-overlay, .mejs-poster {
    width: 100% !important;
    height: 100% !important;
}
.mejs-mediaelement video {
    position: absolute;
    top: 0; left: 0; right: 0; bottom: 0;
    width: 100% !important;
    height: 100% !important;
}

我希望它有所帮助。

于 2014-04-09T21:49:00.830 回答
4

您可以使用网格来做到这一点:

article {
    display: grid;
}

.one {
    grid-area: 1 / 1 / 2 / 2;
}

.two {
    grid-area: 1 / 1 / 2 / 2;
}
于 2020-09-02T18:58:55.180 回答
0

这种布局问题现在可以用 flexbox 解决,避免需要知道高度或使用绝对定位或浮动来控制布局。OP 的主要问题是如何让父母包含未知高度的孩子,并且他们想在特定布局内做到这一点。将父容器的高度设置为“fit-content”即可;使用“display:flex”和“justify-content:space-between”会产生我认为OP试图创建的部分/列布局。

<section id="foo">
    <header>Foo</header>
    <article>
        <div class="main one"></div>
        <div class="main two"></div>
    </article>
</section>    

<div style="clear:both">Clear won't do.</div>

<section id="bar">
    <header>bar</header>
    <article>
        <div class="main one"></div><div></div>
        <div class="main two"></div>
    </article>
</section> 

* { text-align: center; }
article {
    height: fit-content ;
    display: flex;
    justify-content: space-between;
    background: whitesmoke;
}
article div { 
    background: yellow;     
    margin:20px;
    width: 30px;
    height: 30px;
    }

.one {
    background: red;
}

.two {
    background: blue;
}

我修改了 OP 的小提琴:http: //jsfiddle.net/taL4s9fj/

flexbox 上的 css-tricks: https ://css-tricks.com/snippets/css/a-guide-to-flexbox/

于 2019-08-08T15:52:00.257 回答
0

article {
    position: relative;
   
}

//clear the float

article::after{
  content: '';
  clear: both;
  
}

.one {
    position: relative;
    float:left
    margin-top: 10px;
    margin-left: 10px;
    background: red;
    width: 30px;
    height: 30px;
}

.two {
    position: relative;
    float: right;
    margin-top: 10px;
    margin-right: 10px;
    background: blue;
    width: 30px;
    height: 30px;
}

于 2021-05-17T11:39:25.617 回答
-20

这是另一个较晚的答案,但我想出了一种将“条形”文本放置在四个正方形之间的相当简单的方法。这是我所做的更改;在 bar 部分,我将“bar”文本包裹在 center 和 div 标签中。

<header><center><div class="bar">bar</div></center></header>

在 CSS 部分,我创建了一个“bar”类,用于上面的 div 标签。添加后,条形文本位于四个彩色块之间。

.bar{
    position: relative;
}
于 2015-04-23T22:18:30.050 回答