One of the most important concepts a Web developer can understand about CSS are floated elements, which serve a valuable function in aligning and positioning elements relative to each other on a Web page.

Cascading Style Sheets (CSS) are rapidly becoming the de facto standard for Web page layout and positioning. They're easy to use, don't require any special software, and work uniformly on most major browsers. Using them correctly, however, requires a sound analysis of the functional purpose of a particular layout, both to ensure that the resulting style sheets are logically and functionally correct and that they are portable across different browsers or viewing devices.

Often, Web developers fail to understand this particular aspect of style sheet usage, leading to code that contains numerous "hacks" to produce the desired end result. To mitigate this problem, it is worth spending some time to understand some of the core CSS drivers and concepts. And one of the most important concepts a developer can understand are so-called floated elements, which serve a valuable function in aligning and positioning elements relative to each other on a Web page.

This article provides a brief introduction to these floated elements, explaining the CSS float and clear directives and providing some examples of how you can use them to better position HTML elements on a Web page.

Floating elements

Essentially, an element that has the float property applied to it moves out of the normal flow of a page, "floating" in space such that content wraps around it. Thus, an element that is floated to the left will have text running down its right side and wrapping around its bottom edge; an element floated to the right will have content running down its left side.

The float property can take one of three possible values: left, which floats the element to the left; right, which floats it to the right; and none, which eliminates floating. Applying the float property to an element automatically turns it into a block-level element. Other non-styled elements ignore the float, acting as though it doesn't exist. If a float bumps into another float generated previously in the document and on the same side, it must remain either to the right of the previously-generated float (if sufficient space exists) or below it (if insufficient space exists).

Example 1: Wrapping text around an image

To better understand floats, let's consider a simple example: using the CSS float property as a replacement for HTML's ALIGN attribute. Consider the following example (Listing A), which floats an image to the left such that descriptive text wraps around it:

Listing A

<html>
<head>
    <style type="text/css">
    .floater {
        float: left;
        border: solid 1px black;
        padding: 5px;
        margin: 5px;
        width: 100px;
        height: 75px;
    }
    </style>
</head>

<body>

<img class="floater" src="pix2180.jpg" />

This is what it looks like.

In this case, the image is floated to the left, causing the content that follows to flow down its right side and then wrap around below it. Further, as you resize your browser window, the size of the "box" holding the text will dynamically adjust to the new dimensions.

Do you need help with CSS? Gain advice from Builder AU forums

Comments

1

Josh - 16/10/08

Thanks very much. This is one of the trickiest things to get your head around when starting to design with CSS and this is one of the clearest explanations I've come across. Nice one.

» Report offensive content

Leave a comment

You must read and type the 6 chars within 0..9 and A..F

* indicates mandatory fields.

1

Josh - 16/10/08

Thanks very much. This is one of the trickiest things to get your head around when starting to design with CSS ... more

Log in


Sign up | Forgot your password?

  • Staff Microsoft shows off IE9 preview

    This week, highlights from Microsoft's MIX10 conference and more in the Roundup. Read more »

    -- posted by Staff

  • Chris Duckett IE9's H.264 vote killed Ogg

    In a split decision by the judges, the winner of the W3C/WHATWG video codec consensus is H.264, taking home the future of video playback on the internet while loser Ogg goes home with nothing but thoughts of what might have been. Read more »

    -- posted by Chris Duckett

  • Staff Google launches Apps Marketplace

    Google launches and app store, while Mozilla plans to re-write its open-source license. More of this week's news in the Roundup. Read more »

    -- posted by Staff

What's on?

  • Optus Deal

    Broadband + home phone + PlayStation®3 in a single package price!