and couldn’t get along with the first 10 words saying: “When you float an element it becomes a block box”…
…Since “blue” is floated left, it is a “block” so i expected “red” to start in a new line but that didn’t happen !!! Red is placed at the right side of “blue” !
Either i dont understand what “block” means or i’m unable to understand what “float” does…
One problem is that HTML elements are, by default, either represented as blocks or inlines (or sometimes inline-block like form inputs) in browsers before you actually use any CSS.
A div is a block, be default. If you don’t add any CSS styling, then in a browser it is represented as a static block element (static basically meaning “not positioned”, equivalent to “position: static”). Static blocks have certain default properties, which you were looking for in your code:
-they try to be 100% width of their container
-their heights are determined by their (non-floated) content inside them
-their containers (if static blocks) will grow in height to contain them, just as they grow to contain their own content
-they start on a new line
-they can have vertical margins (inlines can’t)
-vertical margins are susceptible to margin-collapse
-can be centered within their containers with “auto” side margins (so long as they are less than 100% wide)
-they try to have their top left corner as close as possible to the top left corner of their container, or the page
-they will bump each other around in 2-d space on the page; that is, if a block above another block grows in height due to an increase in content (say you added more text), the block underneath gets bumped further down. This is a sign that they are within the “flow” of the document. They “see” each other and react to each other, and normally two blocks cannot share the same space (otherwise the universe will divide by zero and implode).
So, because you started your boxes as “div”, they would have started a new line just as you expected, because that’s what they normally do. And you could set heights and widths on them, and vertical margins, because they were blocks to start out with.
Floating a block doesn’t stop it from being a block, but floating an inline can make that inline have “block context” (likely what that sentence you quote is talking about). If you float an anchor (who is an inline by default), that anchor does not need the “display: block” statement because being floated puts it in block context already.
Floats are a special type of block, though, so they do not act the same as static blocks!
Floats
-do NOT try to be 100% width of their containers. Instead, their default width: auto means “width of content inside”, similar to an inline (“shrinkwrapping”)
-do NOT have to start a new line (setting “clear” on a float is one way to force it to a new line). Instead, floats can stack up alongside each other the way inline elements do. This is useful when you want two blocks next to each other on one line
-like static blocks, they CAN have user-set heights and widths
-like static blocks, they CAN have vertical margins
-vertical margins are not supposed to be able to collapse (except they sometimes do in IE, but… that’s IE for you)
-cannot use “auto” side margins for centering
So, when you floated a box, you changed some of its properties, and you also changed how the other elements on the page “see” it.
Floats are partially out of the document flow. Inline sibling elements can see them, but normally static blocks who are siblings don’t: they slide behind floats (big IE exception here, lawlz), and containers who have floats as children do not normally “see” those children. Sometimes this is not what a web developer wants to have! Sometimes you want your containers to enclose your floats.
IE’s Haslayout property makes IE act differently, so it’s good to be aware of it as a web developer.
You can see visually what floats do here if you look in a modern browser and then in IE7 (or IE6). IE8 doesn’t have Haslayout so it should look the same as a modern browser.
After playing with floats (just give them ugly background colours as you have been doing so you can see them) to get the basic idea down, read Paul’s Floats and bugs sticky.
Z-index, if someone didn’t say this earlier (I might have missed it) cannot be manually set on a box unless it is a positioned box (position: relative, absolute or fixed).