简体   繁体   English

CSS高度:100%问题

[英]CSS Height:100% issue

I'm trying to get the div wrapper to surround all the divs within it so depending on the amount of content the height of wrapper will grow. 我正在尝试使div wrapper围绕其中的所有div,因此取决于wrapper高度的内容量将增加。

I guessed that the way of doing this would be to set height: 100% but as you can see from the screen grab below, this is not the case. 我猜想这样做的方法是设置height: 100%但是正如您从下面的屏幕抓图中看到的,情况并非如此。

屏风高度:100%问题

Where it says 'No :-(' is what having height: 100% is doing where ideally I would like wrapper to be at the bottom where it says 'Yes' and I have drawn a red line. 它说“否:-(”的地方是height: 100%位置在理想情况下,我希望wrapper在底部说“是”的地方,我画了一条红线。

Any help is much appreciated. 任何帮助深表感谢。

If you are using floats, giving the container overflow:hidden might fix the problem. 如果使用浮点数,则使容器溢出:隐藏可能会解决此问题。 If no fixed size is given to the div, this makes it stretch over the floated elements. 如果没有给div固定大小,则它会在浮动元素上延伸。

If you have absolutely positioned elements inside the container, it would be good to see the html/css for a solution. 如果您在容器内放置了绝对位置的元素,那么最好查看html / css作为解决方案。

Sounds like you need a clearfix. 听起来您需要一个clearfix。

http://css-tricks.com/snippets/css/clear-fix/ http://css-tricks.com/snippets/css/clear-fix/

You'll want to define the clearfix class (as stated in the above link) add .clearfix to the #wrapper. 您将需要定义clearfix类(如上面的链接所述),将.clearfix添加到#wrapper中。

Can you post a link to the css? 您可以发布指向CSS的链接吗? The first thing that comes to my mind is the position attribute of the divs inside the wrapper. 我想到的第一件事是包装器内div的position属性。 If they are set to float or absolute they will not be contained in the wrapper. 如果将它们设置为float或absolute,则它们将不包含在包装器中。 That is intended behavior. 那是预期的行为。 ie Here is a nice article about containing floats: http://complexspiral.com/publications/containing-floats/ 即,这是一篇关于包含浮点数的好文章: http : //complexspiral.com/publications/ contains-floats /

If, as is likely, that is the problem, you can either relative-position the inside divs or, if you are using floats, you can add an invisible block-displayed hr at the end of the wrapper, like so: 如果很可能是问题所在,则可以将内部div相对放置,或者如果使用浮点数,则可以在包装器的末尾添加不可见的块显示hr,如下所示:

<div id="wrapper">
            /*All divs to be contained here*/
            <hr style="display:block;clear:left;visibility:hidden;">
</div>

The clear:left; 清除:左; is what gets rid of the "floating" of the previous elements. 是摆脱先前元素“浮动”的要素。 THe 'left' should be changed according to your floats. “左”应该根据您的浮动来改变。 More in the article above, this is the method i like best. 在以上文章中,这是我最喜欢的方法。

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

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