How to solve the problem that the parent container height cannot be automatically opened

Source: Internet
Author: User

Developers who are just getting started with website design layout will certainly encounter this problem, that is, when all sub-containers are floating, the height of the parent container cannot be automatically opened. This problem is invisible when we do not set borders or backgrounds for rich containers. The following code is used.

<Div style = "background: # F99; width: 400px; border: 1px solid # 3CF;"> <div style = "float: left; background: # 36F; width: 180px; height: 180px; "> I'm a son container. </div> <div style = "float: left; background: # F60; width: 180px; height: 180px;"> I'm the other son container. </div>

After running, most people will think it is obvious that a parent container contains two sub-containers, but it is not that simple. The height of the parent container is displayed as 0, what you can see is the border of the parent container, and the height is not supported by the height of the two internal sub-containers. At this time, if we set the background for the parent container, it will not be displayed.
The solution is actually relatively simple, that is, adding an attribute to the parent container, overflow: hidden.
Another effective method is to add

<Div style = "clear: both;"> </div>

To clear the floating. This is also a good practice. It is usually used when the parent container needs to set a fixed height, but it is afraid that the content is too large and cannot be displayed due to overflow settings. Both methods are valid and can be tested in practice.

Related Article

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.