简体   繁体   English

根据子DIV高度具有父DIV高度自动大小

[英]Have parent DIV height auto-size based on child DIV height

I have a parent container that encapsulates a number of child containers. 我有一个父容器,它封装了许多子容器。 The child containers have heights depending on the amount of text they hold. 子容器的高度取决于它们保存的文本量。 I would like the child containers to determine the height of the parent container. 我希望子容器确定父容器的高度。 So if a child container has a height of 40px, then the parent container height should be 80px. 因此,如果子容器的高度为40px,则父容器高度应为80px。 If the child container has a height of 100px, then the parent container height should be 140px (40px fixed + 100px variable). 如果子容器的高度为100px,则父容器高度应为140px(40px固定+ 100px变量)。

I've tried leaving the height of the parent container undefined or set as auto or set at 100%, but they all leave my parent container with a height that is too small to hold the child container. 我已经尝试将父容器的高度保留为undefined或设置为auto或设置为100%,但它们都使我的父容器的高度太小而无法容纳子容器。 If I give my parent container height a set pixel then the problem goes away. 如果我给我的父容器高度设置一个像素,那么问题就会消失。

My parent DIV with fixed height(divs behave as expected) 我的父DIV具有固定高度(div表现如预期)

.resultsbox{
    width: 800px;
    position: relative;
    left: 0px;
    right: 0px;
    margin-left: auto;
    margin-right: auto;
    border-top-style: solid;
    border-top-width: 2px;
    border-top-color: #DDD;
    border-radius: 5px;
    height: 400px;
}

jsFiddle example with set height: 设置高度的jsFiddle示例:

http://jsfiddle.net/Uj5FP/6/ http://jsfiddle.net/Uj5FP/6/

My parent DIV with no height set(parent height isn't enough to hold child) 我的父DIV没有设置身高(父母身高不足以容纳孩子)

.resultsbox{
    width: 800px;
    position: relative;
    left: 0px;
    right: 0px;
    margin-left: auto;
    margin-right: auto;
    border-top-style: solid;
    border-top-width: 2px;
    border-top-color: #DDD;
    border-radius: 5px;
}

jsFiddle with no height set: 没有高度设置的jsFiddle:

http://jsfiddle.net/Uj5FP/7/ http://jsfiddle.net/Uj5FP/7/

How do I make the parent height auto-adjust correctly? 如何正确调整父高度?

Here is another attempt. 这是另一种尝试。 Wrap your absolutely place elements in <div class='absolute-panel'> : 将您的绝对位置元素包装在<div class='absolute-panel'>

<div class='resultsbox'>
    <div class='absolute-panel'>
        <div class='reviewtitle'>    
          <strong>Fun</strong>
        </div>
        .
        .
        .
        <div class='reviewstatsright'>    
            <span class='text-info'>
                <dt>Rate</dt><dd>$50</dd>
            </span>
            <span class='text-info'>
                <dt>Tip</dt><dd>$10</dd>
            </span>
        </div>
    </div><!-- .absolute-panel -->
    <div class='reviewbody'>
        <p>What a great time!</p>
         <cite>-Rodger</cite>
    </div>
</div>

For the CSS: 对于CSS:

.absolute-panel {
    border: 1px solid blue;
    width: 800px;
    height: 150px;
    position: relative;
    left: 0px;
    right: 0px;
}

For the .resultsbox , take out the absolute positioning... 对于.resultsbox ,取出绝对定位......

.resultsbox {
    width: 800px;
    margin-left: auto;
    margin-right: auto;
    border-top-style: solid;
    border-top-width: 2px;
    border-top-color: #DDD;
    border-radius: 5px;
    border-left: 1px solid red;
}

For the .reviewbody take out the absolute positioning... and add 40px bottom padding (if needed): 对于.reviewbody取出绝对定位...并添加40px底部填充(如果需要):

.reviewbody {
    margin-top: 20px;
    margin-bottom: 20px;
    padding-bottom: 40px;
    border: 1px dotted red;
}

Comment 评论
I added some colored borders to keep track of things and these can be removed. 我添加了一些彩色边框来跟踪事物,这些可以删除。

The trick is to define a fixed height panel to add all your precisely positioned elements. 诀窍是定义一个固定高度的面板来添加所有精确定位的元素。

After this, add your review body which has a variable height. 在此之后,添加具有可变高度的审阅主体。

The fiddle: http://jsfiddle.net/audetwebdesign/8mRVp/ 小提琴: http//jsfiddle.net/audetwebdesign/8mRVp/

There are a lot of things on this page you could do different and much simpler, but that is a different conversation. 这个页面上有很多东西你可以做的不同而且更简单,但这是一个不同的对话。

I think what you are really asking about is clear fix options. 我认为你真正要问的是明确的修复选项。 The most basic is just adding overflow:hidden to the parent element. 最基本的是添加overflow:hidden到父元素。

And another option that I use for clear fix is: http://nicolasgallagher.com/micro-clearfix-hack/ 我用于明确修复的另一个选项是: http//nicolasgallagher.com/micro-clearfix-hack/

The difference is, if you have any content spilling out of the box, overflow:hidden will hide it and micro clearfix will show it. 不同的是,如果你有任何内容溢出开箱即可,溢出:隐藏将隐藏它,微清除将显示它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM