Method for Solving Div adaptive height

Source: Internet
Author: User

Div indicates that the parent container does not adapt to the height based on the content. Let's look at the following code:

<div id="main"><div id="content"></div></div>

When there is a large amount of Content, even if the height of main is set to 100% or auto. Different browsers still cannot automatically stretch properly. The content height is relatively high, but the main height of the container still cannot be opened.

We can solve this problem in three ways.

1. Add a clear floating to let the parent container know the height. Note that there is a space in the floating container.

<div id="main"> <div id="content"></div> <div style="font: 0px/0px sans-serif;clear: both;display: block"> </div></div>

  

2. Add a container that exists in the Code but is not visually visible.

<div id="main"> <div id="content"></div> <div style="height:1px; margin-top:-1px;clear: both;overflow:hidden;"></div></div>

 

3. Add a BR and set the style to clear: both.

<div id="main"> <div id="content"></div> <br style="clear:both;" /></div>

 

Supplement:

<div id="main"> <div id="content">  <p>demo1</p>  <p>demo2</p>  <p>demo3</p></div> </div>#main { border:1px solid #999999; background-color:#CCCCCC; height:100%; overflow:hidden;}#content { float:left;}

  

None of the above three methods is the best solution, because in the concept of program code, we recommend that you do not add meaningless tag code as much as possible.

My solution is to apply the style directly in the outermost div.

#main { height:100%;overflow:hidden;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.