Float and clear

Source: Internet
Author: User

Float and clear

Floating means taking the elements out of the regular document stream. What should I do? First, the effect of text on images in traditional publications can be achieved, and second, the block-level elements originally listed (stacked) from top to bottom can be changed to parallel between left and right to achievePartitionLayout.

Floating and clearing are the swords used to organize the page layout.FloatAndClearAttribute.

After the floating element is detached from the regular document stream, the elements that followed it will be raised up to the same level as the floating element when the space permits.

View results

How to enclose floating Elements

The floating element is out of the mainstream (document Liu), and its parent element (organization) cannot see it, so it escaped from the encirclement. this is not what the Organization wants, so we will introduce how to enclose floating sub-elements.

 

<section>        <p>It's fun to float.</p></section><footer>Here is the footer.</footer>
Add overflow: hidden to the parent Element

Add overflow: hidden to the parent element of the floating element, and force the parent element to enclose the floating element.

 
<style>        section {            border: 1px solid blue;            margin: 0 0 10px 0;            overflow: hidden;         }        img {            float: left;        }</style>
 

 

Floating parent element at the same time 

"Same stream and pollution" also float the parent element. after a floating section, no matter whether other child elements are floating or not, it will closely enclose (contract) all its child elements. Therefore, you need to set the width so that the section is the same as the width of the view. besides, because the section is floating, footer tries to squeeze it up. to force footer to stay under section, clear floating effects for footer. the cleared element is not promoted to the side of the floating element.

 
 <style>        section {            border: 1px solid blue;            margin: 0 0 10px 0;            /*overflow: hidden;*/            float: left;            width: 100%;        }        img {            float: left;        }        footer {            clear: left;            border: 1px solid red;        }    </style>
 

 

Add non-floating clear elements 

For the parent ElementLastAdd a non-floating child element and clear the child element. the pseudo element is used again. after inserts a pseudo element after the element content rather than after the element.

 
<section class = "clearfix">
 

 

.clearfix:after {content: ".";display: block;height: 0;visibility: hidden;clear: both;}

 

 

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.