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.
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.
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; }
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.
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.
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:
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:
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!