简体   繁体   English

div 的高度不会随着内容而增长

[英]div doesn't grow in height with content

I've read almost every article on this forum about divs and growing height with its content.我已经阅读了这个论坛上几乎所有关于 div 和增长高度及其内容的文章。 I don't understand what I'm doing wrong and can't figure it out.我不明白我做错了什么,无法弄清楚。 Probably it's an easy thing, but I just don't see it any more.可能这是一件容易的事情,但我只是不再看到它了。

I tried the following CSS but can't get it working:我尝试了以下 CSS,但无法正常工作:

clear:both;
float: left;
overflow: auto;
overflow: hidden;

none of this all has the desired output.所有这些都没有所需的输出。

I posted my code on jsfiddle: http://jsfiddle.net/eAVy3/我在 jsfiddle 上发布了我的代码: http : //jsfiddle.net/eAVy3/

You will see that my footer (in red) is at the top in stead of on the bottom.你会看到我的页脚(红色)在顶部而不是底部。 The only way tho get something that looks like it is to give the id page_container a height.获得看起来像的东西的唯一方法是给 id page_container一个高度。 But that will be a fixed height and doesn't grow with the content.但这将是一个固定的高度,并且不会随着内容而增长。 What to do to get this right?该怎么做才能做到这一点?

You should reconsider making the position absolute;您应该重新考虑使职位绝对化; making the position absolute is puttinf the element out of flow so they don't occupy any height or width of the document.使位置绝对是使元素脱离流动,因此它们不占用文档的任何高度或宽度。 change to posiion : relative ;改变位置:相对; and you will start to figure it out你会开始明白

Update 2更新 2

try this :试试这个:

    #kolom_links {
width: 400px;
height: auto;
padding-left: 10px;
}

Working fiddle here: http://jsfiddle.net/eAVy3/3/在这里工作小提琴: http : //jsfiddle.net/eAVy3/3/

Absolute positioning (absolute positioning takes the div out of the normal flow of the document, which means it can't effect other things on the page like the footer)..绝对定位(绝对定位使div脱离了文档的正常流程,这意味着它不能像页脚那样影响页面上的其他东西)。

You need to float your divs instead:你需要浮动你的div

#kolom_links {
  float: left;
  margin-left: 100px;
}

#kolom_rechts {
  float: left;
  margin-left: 70px;
}

Now because both divs inside #page_container are floating, you need use clearfix css:现在因为 #page_container 中的两个 div 都是浮动的,您需要使用clearfix css:

Add class clearfix: <div id="page_container" class="clearfix">添加类 clearfix: <div id="page_container" class="clearfix">

Then add this clearfix to your CSS:然后将此清除修复添加到您的 CSS:

.clearfix:after {
     visibility: hidden;
     display: block;
     font-size: 0;
     content: " ";
     clear: both;
     height: 0;
     }
.clearfix { display: inline-block; }
/* start commented backslash hack \*/
* html .clearfix { height: 1%; }
.clearfix { display: block; }
/* close commented backslash hack */

It's a simple CSS issue: a container doesn't wrap around floated contents by default.这是一个简单的 CSS 问题:默认情况下,容器不会环绕浮动内容。 The easiest way to make it do so it with,让它这样做的最简单方法是,

.div_container {
  overflow: hidden;
}

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

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