16
Mar

CSS In Depth Part 2: Floats & Positions

Last week, in the first of our CSS In Depth post, we discussed the difference between paddings and margins, and what the box model is.

This week we’ll be discussing positions and floats as well as what the differences are and when it’s best to use them. Both floats and positions deal with the relationship of elements between each other. Without these, padding and margins would be ineffectual.

What Is Position?

Position rules are used to position the element in the document flow. The position rule can take four values: static, relative, absolute and fixed. The default value of every element is static, where each element comes after each one another in order.

Elements that are position can be moved around using the left, top, bottom and right rules and a pixel or percentage value.

If an element is not positioned inside of another positioned element, the left, top, bottom and right rules will calculate their values using the browser window. For example, if we have an element that’s 100 x 100px and positioned by itself, with a left of 20px, it will be exactly 20px to the left most side of the user’s window, no matter how big or small that is.

css1

However, if we position that element inside of another positioned element with a left of 20px, the 20px will be calculate from the left side of the element, not the browser window.

css2

Positioning seems to work well in IE with little bugs, however I generally use positioning as a last resort. It seems that floating elements allows for less bugs when dealing with repeated backgrounds, expanding content and layouts in general.

Relative Positioning

Relative position is close to floats in terms of how they work. They can be moved around using either the top, right, left, bottom rules or by margins like floats can. Relative positioning changes the element without influencing the layout of other elements.

Therefor, the element will technically remain in the document flow, but will be “relatively” position in it. Therefor if we have 3 elements and gave the middle element a position relative and a bottom of 40px, the element would move up 40px, but the element below it will not move at all, as if the middle element never moved.


div { width: 100px; height: 100px; border: 1px solid #000; background: #CCC;}
div.middle { position: relative; bottom: 40px; }

<div></div>
	<div class="middle"></div>
<div></div>

css3

Absolute Positioning

Absolute positioning removes the element from the document flow altogether. The other elements around it will move together and act as if the absolute positioned element never existed.

css4

This rule is generally best for using when an item breaks the general layout box.

Fixed Positioning

Fixed positioning is the same as absolute positioning, except it’s always positioned against the user’s browser window. Therefor, the element remains motionless as the user scrolls up or down in the browser. Unfortunately, fixed positioning doesn’t work at all in IE6 and below.

What Are Floats?

Floats are elements that are literally “floated”, so that they render side by side to each other. The float rule accepts three values: left, right and none. Floated elements can only be moved using margins, not by using the top, left, right or bottom rules.

Let’s say we have 2 boxes with a width of 50%. Normally, these boxes will appear one beneath the other, but if we give both boxes a float left, each box will render side by side.

css5

If there’s no height and width specified on the floated element, the element will automatically size itself to the content inside just like inline elements, even though a floated element is consider a block element. Also, non-floated elements will ignore floated ones in document flow, creating layout issues, so it’s important to either float all your elements in a container, or none of them.

Floating your elements left and right will solve a lot of weird browser issues. Unfortunately, modern browser deal with float rights differently than older browsers (including the IEs).

In modern browsers, you can either float right the element before or after the left float element, and it will render the same. For example, if out boxes had a width of 30% and the first one had a float left, and the second a float right, it would render correctly like:

css6

However, in older browsers, the floated right element has to come before the floated left element, otherwise it will render slightly below the left element. So, if we were to take the same example as above and check it out in an older version of Safari, or even in IE7, here’s an idea of how it would render:

css7

Which is better?

Like I said before, I prefer to float my elements and only use positions when breaking the layout. However, you’ll quickly come to figure out which works for your coding style better, so try experimenting with different floats and positions for all your layouts!

The In Depth CSS Series

Your Turn To Talk

I hope you liked this second post of our CSS In Depth series. Make sure you grab the RSS feed so you’ll know when we publish the next part!

Please let us know what you think by leaving a comment below ;)

jump to the comment form ↓

  • User Gravatar Rodney Keeling
    March 16th, 2010

    Great article; I’m excited for the next two parts of the series. And I didn’t know that tidbit about floating in older browsers. Thanks!

  • User Gravatar ESN
    March 17th, 2010

    I didn’t know that the right element has to come before the floated left element in order to appear on the same level. Thank you for the tip.

  • User Gravatar Pixil
    March 17th, 2010

    Floats are so essiential to my workflow :-) Clearing your floats is just as important. I recently switched from using a clear div to clear:after method. Looking forword to the next series!

  • User Gravatar jay design
    March 17th, 2010

    I didn’t realise that the element floating right has to come before the left float to render properly in older browsers.. Thanks for the heads up!

  • User Gravatar Bryan
    March 17th, 2010

    This is awesome. I love the in-depth analysis. It really helps me get a better understanding of how floats and positioning values work. Thanks for this. Keep it up

  • User Gravatar Jan Pietrzyk
    March 17th, 2010

    @jay design

    This is exactly the reason why I prefer positioning.Most of the time the markup stays more accessible when you just position Elements acording to the layout. The only reason to use flots is that there are simple methods to contain things.

    @topic
    Whe I readf theheadline I was truely anticipating sth. like what happens when you float positioned elements, position your float relative again…. However nice introduction to the topic, but I don’t think that there is much of a choice most of the time. Since there is no real Layout property in CSS you just abuse text propertys to work out a Layout under any circumstances and hope for the best in terms of robustness.

  • User Gravatar Russell Hampton
    March 17th, 2010

    Very informative. I’m a floater myself but it’s nice to get more perspective on the positions and how they can be applies. Thanks for the insight!

  • User Gravatar Amber Weinberg
    March 17th, 2010

    Yup, putting the right floated element in front of the first has always been difficult to remember, but all of the IEs require that :/

  • User Gravatar Joe
    March 17th, 2010

    I’m a beginner to web design and am currently trying to build my personal portfolio site. I really appreciate these articles as these are the exact issues I’m pulling my hair out over as I work. Thanks for the in-depth, yet easily understandable, tips. If you know of any resources related to these topics that might help me even more, please let me know. I look forward to your next articles!

  • User Gravatar Beth McLain | Web Designer
    March 17th, 2010

    The information aboutCSS positions and floats are very useful to me.
    thanks for posting…

  • User Gravatar Amber Weinberg
    March 17th, 2010

    @Jan That’s interesting that you prefer to position your elements over floating. To me, it’s better to float because it plays nicer when expanding content and using repeated bkg images..but that’s just my experience :P

    I’m not sure I understand though, what you mean about abusing text properties because of layout?

  • User Gravatar Kristie
    March 18th, 2010

    Thanks, this is a good refresher. Whenever I leave web dev for a time, this is the stuff that immediately leaves my head. It’s not all that intuitive I don’t think.

    thanks for your help.

  • User Gravatar logolitic
    March 18th, 2010

    very interesting and useful information, I`m curently thinking to start learn coding because it`s very useful for me as a designer.

    THanks a lot, you really made me think about it

  • User Gravatar ikramhakimi
    March 18th, 2010

    hi all,

    thanks for the information, I also love float, but
    sometimes I don’t float too much. recently i prefer this solution:

    .container {
    width: 820px;
    margin: 0 auto;
    }

    .left {
    float: left;
    width: 400px;
    background: #ccc;
    }

    .right {
    width: 400px;
    margin-left: 420px;
    background: #ccc;
    }

    so we will have 20px spacing between .left and .right,

    thanks again!

  • User Gravatar nando
    March 29th, 2010

    Hi Amber, thanks for sharing this great post!

  • User Gravatar Anatoly Gilderman
    April 7th, 2010

    Thanks for this very wonderful article. Can’t wait for the next one.

    I used to have difficulties in CSS and when I finally realized its importance, I did not stop until I know every single information about it.

  • User Gravatar Derrick Workman
    April 7th, 2010

    I too use floats when coding my sites, but it gave me some new insight into absolute positioning as well.

    Another great CSS article, Thanks!

Who Linked To This Post?

  1. designfloat.com
  2. In Depth CSS Part 2: Floats & Positioning :: Freelance XHTML, CSS and WordPress Developer Amber Weinberg :: Nashville, TN
  3. CSS Brigit | CSS In Depth: Floats & Positions
  4. CSS in Depth: Floats and Positions | Design Newz
  5. In Depth CSS Part 4: New CSS3 Styles :: Freelance XHTML, CSS and WordPress Developer Amber Weinberg :: Nashville, TN
  6. MyInkTrail: Best of the Design Community, March 2010 | MyInkBlog

Share your thoughts, leave a comment!