How does the height:100% of elements in CSS work? Self-adapting height

Source: Internet
Author: User

The Web browser considers the open width of the browser window when calculating the effective width. If you don't set any default values for width, the browser automatically tiles the contents of the page to fill the entire horizontal width. When you set the height of a page element to 100%, it is expected that this element will fill the height of the entire browser window, but in most cases, it has no effect. Do you know why height:100% it doesn't work?

For:

The height is calculated in a completely different way. In fact, the browser does not calculate the height of the content at all, unless the content is outside the window (causing the scroll bar to appear). Or you set an absolute height for the entire page. Otherwise, the browser will simply let the content piled down, the height of the page is not considered at all.

Because the page does not have a default height value, when you set the height of an element to a percentage height, you cannot calculate its height based on the height of the parent element. In other words, the height of the parent element is just a default value: height: auto; . When you ask the browser to calculate a percentage height based on such a default value, you can only get undefined results. is a null value, the browser does not react to this value.

How to use absolute positioning to make an adaptive screen height: add height:100% to the parent or position the element absolutely

1.<style>
*{padding:0;margin:0;}
body,html{height:100%;} Parent elements are added
#wrap {width:100%;height:100%;background:red;
/*position:absolute;left:0;top:0;*/
}

</style>
<title>Title</title>
<body>
<div id= "Wrap" ></div>
</body>
2. <style>

*{padding:0;}
        #wrap {width:100%;height:100%;p osition:absolute;left:0;top:0;} No absolute positioning height is not open
</style>
<title>Title</title>
<body>
<div id= "Wrap" ></div>
</body>

How does the height:100% of elements in CSS work? Self-adapting height

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.