Floating would be my choice, but it really depends on what you want to achieve. If you can provide a more concrete example, I could give you a clear reason as to what and why I think you should use. However, here is a brief set of pros and cons that I came across (I assume that by positioning you mean absolute positioning):
Positioning pros:
- very precise positioning relative to the next ancestor, labeled as relative position - provides greater flexibility.
- allows elements to be in a different order visually than in the DOM
Positioning Cons:
- It is more difficult to align other elements, since the positioned element is no longer in the document flow, and also because of the required level of accuracy.
- Other elements ignore an absolutely positioned element, which means that you may have potential overlap if you do not take into account the minimum and maximum size of the positioned element
- itβs harder to implement a footer if you use absolute positioning for your columns.
Floating pros:
- really easy to create simple and advanced layouts
- no footer problems
- without worrying about accuracy, the browser takes care of it
- stretching parent containers
Float:
- Lots of pitfalls for those less experienced with floating point mockups that can lead to lots of questions asked on SO :)
As for the clear: both elements mentioned by Sebastian, there is a simple way. Let's say you have a container div and 2 floating divs inside.
Html:
<div id="con"> <div class="float"></div> <div class="float"></div> </div>
CSS
#con { background:#f0f; } .float { float:left; width:100px; height:100px; background:#0ff; }
if you were to run this code, you would notice that the div container (magenta color) is only one pixel maximum, while the floating divs were correct - this is the problem mentioned by Sebastian. Now you can take his advice and add br or swim into a container that would not be very semantic - and here is a slightly more elegant solution. Just add overflow: hidden; into the div container like this:
#con { background:#f0f; overflow:hidden; }
Your container should now wrap the floating divs correctly.
Darko z
source share