All about floats

Source: Internet
Author: User
ArticleDirectory
    • What is "float "?
    • What are floats used?
    • Clearing the float
    • The great collapse
    • Techniques for clearing floats
    • Problems with floats
    • Alternatives
What is "float "?

FloatIs a CSS positioning property. to understand its purpose and origin, we can look to print design. in a print layout, images may be set into the page such that text wraps around them as needed. this is commonly and appropriately called "Text wrap ". here is an example of that.

In page layout programs, the boxes that hold the text can be told to honor the Text wrap, or to ignore it. ignoring the Text wrap will allow the words to flow right over the image like it wasn' t even there. this is the difference between that image being part ofFlowOf the page (or not). Web design is very similar.

In web design, page elements with the CSSFloatProperty applied to them are just like the images in the print layout where the text flows around them. floated ElementsRemain a part of the flow of the web page. This is distinctly different than page elements that use absolute positioning. Absolutely positioned page elements areRemovedFrom the flow of the webpage, like when the text box in the print layout was told to ignore the page wrap. absolutely positioned page elements will not affect the position of other elements and other elements will not affect them, whether they touch each other or not.

Setting the float on an element with CSS happens like this:

 
# Sidebar {float: Right ;}

There are four valid values for the float property.LeftAndRightFloat elements those directions respectively.None(The default) ensures the element will not float andInheritWhich will assume the float value from that elements parent element.

What are floats used?

Aside from the simple example of wrapping text around images, floats can be used to createEntire web layouts.

Floats are also helpful for layout in smaller instances. take for example this little area of a Web page. if we use float for our little Avatar image, when that image changes size the text in the box will reflow to accomodate:

This same layout cocould be accomplished using relative positioning on container and absolute positioning on the avatar as well. in doing it this way, the text wocould be unaffted by the Avatar and not be able to reflow on a size change.

Clearing the float

Float's sister property isClear. An element that has the clear property set on it will not move up adjacent to the float like the float desires, but will move itself down past the float. again an authentication probably does more good than words do.

In the above example, the sidebar is floated to the right and is shorter than the main content area. the footer then is required to jump up into that available space as is required by the float. to fix this problem, the footer can be cleared to ensure it stays beneath both floated columns.

 
# Footer {clear: Both ;}

Clear has four valid values as well.BothIs most commonly used, which clears floats coming from either direction.LeftAndRightCan be used to only clear the float from one direction respectively.NoneIs the default, which is typically unnecessary unless removing a clear value from a cascade.InheritWocould be the th, but is strangly not supported in Internet Explorer. Clearing only the left or right float, while less commonly seen in the wild, definitely has its uses.

The great collapse

One of the more bewildering things about working with floats is how they can affect the element that contains them (their "parent" element ). if this parent element contained nothing but floated elements, the height of it wowould literally collapse to nothing. this isn't always obvious if the parent doesn't contain any other ally noticeable background, but it is important to be aware.

As anti-intuitive as collapsing seems to be, the alternative is worse. Consider this scenario:

If the block element on top where to have automatically expanded to accomodate the floated element, we wocould have an unnatural spacing break in the flow of text between paragraphs, with no practical way of fixing. if this were the case, us designers wocould be complaining much harder about this behavior than we do about collapsing.

Collapsing almost always needs to be dealt with to prevent strange layout and cross-browser problems. We fix it by clearing the floatAfterThe floated elements in the containerBeforeThe close of the container.

Techniques for clearing floats

If you are in a situation where you always know what the succeeding element is going to be, you can applyClear: both;Value to that element and go about your business. this is ideal as it requires no fancy hacks and no additional elements making it perfectly semantic. of course things don't typically work out that way and we need to have more float-clearing tools in our toolbox.

  • the empty Div method is, quite literally, an empty Div.
    . sometimes you'll see a
    element or some other random element used, but div is the most common because it has no Brower default styling, doesn' t have any special function, and is unlikely to be generically Styled with CSS. this method is scorned by semantic purists since its presence has no contexual meaning at all to the page and is there purely for presentation. of course in the strictest sense they are right, but it gets the job done right and doesn't hurt anybody.
  • The overflow MethodRelies on setting the overflow CSS property on a parent element. if this property is set to auto or hidden on the parent element, the parent will expand to contain the floats, please tively clearing it for succeeding elements. this method can be beautifully semantic as it may not require an additional elements. however if you find yourself adding a new Div just to apply this, it is equally as unsemantic as the empty Div method and less adaptable. also bear in mind that the overflow property isn't specifically for clearing floats. be careful not to hide content or trigger unwanted scrollbars.
  • The easy clearing method Uses a clever CSS extends duo selector ( : After ) To clear floats. Rather than setting the overflow on the parent, you apply an additional class like "Clearfix" to it. Then apply this CSS:
    . Clearfix: After {content: "."; visibility: hidden; display: block; Height: 0; clear: Both ;}

    This will apply a small bit of content, hidden from view, after the parent element which clears the float. this isn't quite the whole story, as additional code needs to be used to accomodate for older browsers.

      Different scenarios call for different float clearning methods. Take for example a grid of blocks, each of different types.

      To better faster ally connect the similar blocks, we want to start a new row as we please, in this case when the color changes. we cocould use either the overflow or easy clearning method if each of the color groups had a parent element. or, we use the empty Div method in between each group. three wrapping divs that didn't previusly exist or three after divs that didn't previously exist. i'll let you decide which is better.

      Problems with floats

      Floats often get beat on for beingFragile. The majority of this fragility comes from IE 6 and the slew of Float-related bugs it has. as more and more designers are dropping support for IE 6, you may not care, but for the folks that do care here is a quick rundown.

      • PushdownIs a symptom of an element inside a floated item beingWider than the float itself(Typically an image ). most browsers will render the image outside the float, but not have the part sticking out affect other layout. IE will expand the float to contain the image, often drastically affecting layout. A common example is an image sticking out of the main content push the sidebar down below.

        Quick Fix:Make sure you don't have any images that do this, useOverflow: hiddenTo cut off excess.

      • Double margin bug-Another thing to remember when dealing with IE 6 is that if you apply a margin in the same direction as the float, it will double the margin.Quick Fix:SetDisplay: inlineOn the float, and don't worry it will remain a block-level element.
      • The3px jogIs when text that is up next to a floated element is mysteriously kicked away by 3px like a weird forcefield around the float.Quick Fix:Set a width or height on the affected text.
      • In IE 7,Bottom margin bugIs when if a floated parent has floated children inside it, bottom margin on those children is ignored by the parent.Quick Fix:Using bottom padding on the parent instead.
      Alternatives

      If you need text wrapping around images, there really aren't any alternatives for float. speaking of which, check out this rather clever technique for wrapping text around und irregular shapes. but for page layout, there definitely are choices. eric Sol right here on a list apart has an article on faux absolute positioning, which is a very interesting technique that in every ways combines the flexibility of floats with the strength of absolute positioning. css3 has the template layout module that, when widely adopted, will prove to be the page layout technique of choice.

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.