Clear float in css, and clear float in css

Source: Internet
Author: User

Clear float in css, and clear float in css

(1) Clear floating with empty tags

This is currently the most common method to clear floating. An empty tag can be a div tag or a P tag. In theory, it can be any tag. This method adds a clear floating empty label at the end of the parent element to be cleared. Its CSS code is as follows:Clear: both. This method is concise and clear, which is convenient and practical, but its disadvantage is obvious, that is, adding meaningless structural elements. In addition, there is a similar method,<Br clear = "all"/>You can also achieve the same effect.

(2) Clear floating with the after pseudo object

All browsers support the "after" pseudo object, so "after" should be applicable and there is no compatibility problem. Its CSS code:# Layout: after {display: block; clear: both; content: ""; visibility: hidden; height: 0 ;}. Note: 1. In this method, you must set the pseudo object to clear floating elements.Height: 0Otherwise, the element is several pixels higher than the actual one. 2,ContentAttribute is required, but its value can be null. This method is practical and effective, and does not need to add meaningless labels. However, CSS code is complicated and not concise and clear enough.

(3) Use the overflow attribute

This method effectively solves the drawbacks of adding unintentional code by clearing the floating through the empty Tag Element. To use this method, you only need to define the CSS attributes in the elements to clear the float:Overflow: autoThe purpose is to make highly adaptive,Zoom: 1To be compatible with IE6 (zoom does not comply with W3C standards), you can also useHeight: 1%.


What is the clear floating in CSS?

I have a blog post on Sina Blog about how to clear floating in css, which is helpful to you:

The height of elements in the DIV layer cannot be changed automatically after the float attribute is used:

In some cases, the height or width is not set to automatically adjust the content of the outer layer.
However, if the element in the layer uses the float attribute, the elements will be removed from the file stream, and the container layer cannot be expanded. As a result, the outer layer size cannot be automatically adjusted based on the content.
This article summarizes several methods that are compatible with the current mainstream browsers for your reference.

1. Clear floating
Add the last edge of the element with floating attributes in the layer to clear
</SPAN> divstyle = "background: # eee;"> </SPAN> divstyle = "height: 100px; width: 100px; background: Green;
Float: left; "> </SPAN> div> </SPAN> divstyle =" height: 100px; width: 100px; background: Red; float: right; "> </SPAN> div> style =" clear: both; ">></SPAN> div>

2. Set display to table
In general, the outer width is fixed. After setting the width, you can set the outer display mode to: display: table.

</SPAN> divstyle = "background: # eee;
Width: 500px; display: table; "> </SPAN> divstyle =" height: 100px; width: 100px; background: Green;
Float: left; "> </SPAN> div> </SPAN> divstyle =" height: 100px; width: 100px; background: Red; float: right; "> </SPAN> div>

3. Set overflow to hidden.
Set overflow of the outer layer to: hidden:

</SPAN> divstyle = "background: # eee; overflow: hidden;
"> </SPAN> divstyle =" height: 100px; width: 100px; background: Green; float: left; "> </SPAN> div> </SPAN> divstyle =" height: 100px; width: 100px; background: Red; float: right; "> </SPAN> div> <&#...... remaining full text>

How to clear div + css float?

1. Clear the floating div without a height by default, unless you have set the height. In the editing window, the software shows you the existence of a div, and the code remains unchanged.

2. if you want to clear floating in a format similar to <div class = "clear: both"> </div>, then you 'd better place this div behind the same-level elements with floating attributes (for example, your code is li. However, the next level of ul can only be li, so you can write it as follows: <li style = "clear: both; height: 0; visibility: hidden"> </li> ); if overflow: auto is used, this setting should be placed in the parent container with floating attribute elements (such as your ul ), in addition, this method can clear the float in non-ie and ie7 + browsers and is invalid for ie6, so you should write zoom: 1 in the settings, and use of pseudo objects (: after ). There are a lot of details about how to clear the float and the nature of the float. I hope you can check it out. Understanding more, then you will truly understand its inner and be able to control it.
 

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.