简体   繁体   English

内部div溢出容器

[英]Inner div overflowing container

I have two div s inside a div . 我的div内有两个div I want the second div to fill up to the bottom of the container. 我希望第二个div填充到容器的底部。 I tried various height: 100%; 我尝试了各种height: 100%; , height: inherit; height: inherit; , height: auto; height: auto; , etc. and different values for display css property, but didn't succeed. 等等,并且display css属性的值不同,但是没有成功。 Please help. 请帮忙。
Html: HTML:

<div style='height: 100px; width: 100px; background-color: black; border: 3px solid black;'>
    <div style='background-color: red;'>
        <label>Test</label>
    </div>
    <div style='height: inherit; background-color: green;'>
    </div>
</div>

Fiddle 小提琴
Note: The second div has some rows and then a footer. 注意:第二个div有一些行,然后有一个页脚。 I want the rows to be hidden as per the height. 我希望根据高度隐藏行。 But the footer of the second div should be visible. 但是第二个div的页脚应该可见。
Another note: 另一个注意事项:
The container is re-sizable (using JQuery Re-size). 容器是可调整大小的(使用JQuery Re-size)。 Hence I do not want to set the height of the second div . 因此,我不想设置第二个div的高度。 That will make it static. 这将使其静止。 I want the second div to have dynamic height. 我希望第二个div具有动态高度。 ie Expanding yo the bottom of the container, always. 即总是在容器底部扩大。

Try This 尝试这个

**overflow:hidden;**

<div style='height: 100px; width: 100px; background-color: black; border: 3px solid black;overflow:hidden;'>
    <div style='background-color: red;'>
        <label>Test</label>
    </div>
    <div style='height: inherit; background-color: green;'>
    </div>
</div>

Or Else you have to master div height auto and inner keep 100% some content inside. 否则,您必须掌握div高度的自动设置,并在内部保留100%的某些内容。

<div style='height: auto; width: 100px; background-color: black; border: 3px solid black;'>
    <div style='background-color: red;'>
        <label>Test</label>
    </div>
    <div style='height: 100%; background-color: green;'>
        &nbsp;
    </div>
</div>

when you do height: inherit; 当你做height: inherit; , the target container acquires the height of parent, that's why, your inner green div is taking height:100px and hence it is overshooting. ,目标容器获取父对象的高度,这就是为什么您的内部绿色div height:100px ,因此它会超调。

You should NOT DO overflow:hidden , as it will eat up your lower content. 您不应该执行overflow:hidden ,因为它会吞噬您的较低内容。

What you should do is to either give percentage height to both your containers like 您应该做的是给两个容器都指定百分比高度,例如

<div id="parentDiv" style='height: 100px; width: 100px; 
                             background-color: black; border: 3px solid black;'>
    <div id="topDiv" style='background-color: red;height:30%'>
        <label>Test</label>
    </div>
    <div id="lowerDiv" style='height: 70%; background-color: green;'>
    </div>
</div>

or use javascript to set height of your containers, something like 或使用javascript设置容器的高度,例如

$(window).resize(function(){
      var _heightT= $('#parentDiv').height();
      $('#topDiv').height(_height*0.3);
      $('#lowerDiv').height(_height*0.7);

 })

I would suggest to give your Parent container a fixed height(deduced according to the window size, through javascript/jQuery), so that it is consistent across all browsers, and your inner containers, a percentage height, or atleast your top container a fixed height, and lower container a min-height and overflow-y:auto 我建议给您的父容器一个固定的高度(根据窗口大小,通过javascript / jQuery推断),以便在所有浏览器中保持一致,并且您的内部容器的高度百分比是固定的,或者您的顶部容器至少固定一个高度高度,并降低容器的min-heightoverflow-y:auto

How about something like this: 这样的事情怎么样:

HTML: HTML:

<div id="con">
    <div id="top">
        <label>Test</label>
    </div>
    <div id="bottom">sdsdfsdfsdfs sdfs dfsdf sdf sd ff</div>
</div>

CSS: CSS:

#con {
    height: 200px;
    width: 100px;
    background-color: black;
    border: 3px solid black;
    position: relative;

}
#top {
    background-color: red;
    position: absolute;
    width: 100%;
}
#bottom {
    top: 0px;
    left: 0px;
    background-color: #F5F5F5;
    width: 100%;
    height: 100%;
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    box-sizing: border-box;
    background-color: green;
}

Take a look and see what you think. 看一看,看看您的想法。 (you will have to push down inside to put text etc using padding-top: 20px; (您将必须使用padding-top: 20px;向下推入内部以放置文本等padding-top: 20px;

DEMO HERE 此处演示

Very simple: 很简单:

<div style='overflow:hidden;height: 100px; width: 100px; background-color: black; border: 3px solid black;'>
    <div style='background-color: red;'>
        <label>Test</label>
    </div>
    <div style='height: inherit; background-color: green;'>
    </div>
</div>

UPDATE UPDATE

According that we don't want to use overflow:hidden 据此,我们不想使用overflow:hidden

Updated FIDDLE 更新了FIDDLE

<div style='height: auto; width: 100px; background-color: black; border: 3px solid black;'>
    <div style='background-color: red;'>
        <label>Header</label>
    </div>
    <div style='height: 100%; background-color: green;'>
        <label>Body</label>
        <p>Some text here</p>
        <p>Some text here</p>
        <p>Some text here</p>
        <p>Some text here</p>
    </div>
</div>

You could use a percentage based height like you suggested, but the thing is when you set the bottom div to height:100%; 您可以像建议的那样使用基于百分比的高度,但是事情是当您将底部div设置为height:100%; that means 100% of the parent div's height which is 100px and it'll go outside the box, instead you could give the top div a 25% height and the bottom div 75% height, like this: 这意味着父div高度的100%为100px,它将移到框外,而您可以将顶部div的高度设置为25%,将底部div的高度设置为75%,如下所示:

<div style='height: 100px; width: 100px; background-color: black; border: 3px solid black;'>
    <div style='height:25%; background-color: red;'>
    <label>Test</label>
    </div>
    <div style='height: 75%; background-color: green;'>
    </div>
</div>

Fiddle 小提琴

When you do height:inherit , it takes the height value from the parent div , which is the same as saying height:100% . 当您执行height:inherit ,它将从父div获取高度值,该值与说height:100% But this causes the div to overflow because there is another inner-div child inside the main container div , which is taking a height equal to the default line-height of the label tag. 但这会导致div溢出,因为主容器div还有另一个inner-div子级,该子级的高度等于label标签的默认line-height You can try giving the inner div tags separate heights: 您可以尝试给内部div标签指定单独的高度:

HTML :(same as your markup, just adding classes so you don't have to give inline styling) HTML :(与您的标记相同,只是添加类,因此您不必给出内联样式)

<div class="p">
    <div class="c1">
        <label>Test</label>
    </div>
    <div class="c2"></div>
</div>

CSS : CSS

.p {
    height: 100px;
    width: 100px;
    background-color: black;
    border: 3px solid black;
}
.c1 {
    height:20%;
    background-color: red;
}
.c2 {
    height: 80%;
    background-color: green;
}

DEMO DEMO

You can do this with display:table property in CSS. 您可以使用CSS中的display:table属性来执行此操作。 See more 查看更多

Add display:table to the wrap div and display:table-row for the children. display:table添加到wrap div并为子级添加display:table display:table-row

Working Demo 工作演示

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

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