CSS Clear Float method

Source: Internet
Author: User

HTML structure

1 <Divclass= "Wrap">2     <Divclass= "Div1">1</Div>3     <Divclass= "Div2">2</Div>4     <Divclass= "Div3">3</Div>5 </Div>

CSS Styles

1 . Wrap{margin:10px;Padding20px;border:5px Solid Gray;Background-color:#fff;}2 . Div1{float: Left;Height:100px;width:100px;Background-color:Lightgray;}3 . Div2{float: Left;Height:100px;width:100px;Background-color:Skyblue;}4 . Div3{float: Left;Height:100px;width:100px;Background-color:Red;}

You can see that the height of the outer wrap Div is not propped up because the inner element floats out of the text stream and we need to clear the float. Here are three common ways to clear floats.

1. Add an empty element after the last inner element, using the Clear:both

1 <Divclass= "Wrap">2     <Divclass= "Div1">1</Div>3     <Divclass= "Div2">2</Div>4     <Divclass= "Div3">3</Div>5     <Divclass= "Clear"></Div>6 </Div>
1 . Clear {Clear:both;}

2. Add Overflow:hidden or Auto properties to the outer element.

1 . Wrap {2    overflow:auto;  3    zoom:1;  /* Handling compatibility issues */4 }

3. Add a pseudo-element to the outer element after, using after simulation to create the element div.clear. (recommended, can be used as a public style)

1 <Divclass= "Wrap Clearfix">2     <Divclass= "Div1">1</Div>3     <Divclass= "Div2">2</Div>4     <Divclass= "Div3">3</Div>5 </Div>
1 . Clearfix{2 Zoom:1;/*dealing with IE compatibility issues*/3}4 . Clearfix:after{5 Display:Block;6 Visibility:Hidden;7 content:'. ';8 Clear:both;9 Height:0;Ten Overflow:Hidden; One}

CSS Clear Float method

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.