Clear CSS float, css float

Source: Internet
Author: User

Clear CSS float, css float
CSS clear Floating Method 1. Give the parent a height attribute

<Style type = "text/css">. div1 {background: #000080; border: 1px solid red;/* solution Code */height: 200px ;}. div2 {background: #800080; border: 1px solid red; height: 100px; margin-top: 10px }. left {float: left; width: 20%; height: 200px; background: # DDD }. right {float: right; width: 30%; height: 80px; background: # DDD} </style> <div class = "div1"> <div class = "left"> Left </div> <div class = "right"> Right </div> </div> <div class = "div2"> div2 </div>

Principle: manually defining a height for the parent div solves the problem that the parent div cannot automatically obtain the height.

Advantage: simple and less code.

Disadvantage: It is only suitable for fixed height la S and must be accurate.

2. Add an empty div label at the end to give the empty div clear: both attribute.
<Style type = "text/css">. div1 {background: #000080; border: 1px solid red }. div2 {background: #800080; border: 1px solid red; height: 100px; margin-top: 10px }. left {float: left; width: 20%; height: 200px; background: # DDD }. right {float: right; width: 30%; height: 80px; background: # DDD}/* clear floating Code */. div {clear: both} -- * clear empty div used for floating * -- </style> <div class = "div1"> <div class = "left"> Left </div> <div class = "right"> Right </div> <div class = "div"> </div> <div class = "div2"> div2 </div>

Principle: add an empty div to the front and clear the floating with clear: both

A bit: simple, less code, good browser support

Disadvantages: not easy to understand, not conducive to layout

3. Define a pseudo class for the parent: after
<Style>. div1 {border: 1px solid red; background-color: green;}/* clearing floating code is equivalent to adding an empty div to div1 */. div1: after {content: ""; clear: both; display: block ;}. left {width: 20%; height: 200px; float: left; background-color: red ;}. right {width: 30%; height: 100px; float: right; background-color: skyblue ;}. div2 {background-color: black; border: 1px solid yellow ;}</style> 

Principle: It is equivalent to adding a child level to the parent level (empty div, clear: both attribute, and content must be included, even if the content is empty)

Advantage: the browser supports good behavior and is not prone to strange problems.

Disadvantages: the code is too large to be easily understood by beginners.

4. Define overflow: hidden for the parent level
<Style>. div1 {border: 1px solid red; background-color: green;/* clear floating Code */overflow: hidden ;}. left {width: 20%; height: 200px; float: left; background-color: red ;}. right {width: 30%; height: 100px; float: right; background-color: skyblue ;}. div2 {background-color: black; border: 1px solid yellow ;} </style> 

Principle: width must be defined, and height cannot be defined. The over: hidden browser is used to automatically detect the height of floating elements.

Advantage: less code

Disadvantage: if the child level is a positioning element and the Child Level Width is greater than the parent level, extra content will be hidden.

5. Define overflow: auto for the parent
<Style>. div1 {border: 1px solid red; background-color: green;/* clear floating Code */overflow: auto;}/* The Child width cannot exceed the parent width, otherwise a scroll bar will appear */. left {width: 20%; height: 200px; float: left; background-color: red ;}. right {width: 30%; height: 1000px; float: right; background-color: skyblue ;}. div2 {background-color: black; border: 1px solid yellow ;} </style> 

Principle: width must be defined, and height cannot be defined. Use the over: auto browser to automatically detect the height of floating Elements

Advantage: less code

Disadvantage: if the child width is greater than the parent width, the excess content will be hidden.

 

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.