The CSS Visual Formatting Model
All of this (and prev blogs posts) information comes from 1 awesome source that I recommend you buy. CSS Mastery (Advanced Web Standards Solutions)
Get the cheet sheet! http://www.milkaddict.com/wp-content/uploads/2009/11/css-cheat-sheet-v2.png
Box Model: [LINK WC3 ]
All elements are rectangles with characteristics ( offset, margin, border, padding, width, height ) See firebug for an example:
- Margins are transparent (not seen), used to control spacing
- Border adds a line around the item
- Padding creates a gap around the item.
IE Boxing Model (ugh)
IE uses a non-standard boxing model. (IE < version 7) (quirks mode). Width property is considered equal to the SUM of the widths of: content, padding & borders. Standard boxing model indicates width is the width of the element. So for IE v6 & earlier, if you specify a width and then padding & border – the item’s width is reduced to accommodate the added widths of the padding / border.
Solution: Don’t specify padding/border width along with content width. Want to know more? Go here:
http://en.wikipedia.org/wiki/Internet_Explorer_box_model_bug
Margin Collapsing:
This blog gives a nice summary. http://www.milkaddict.com/css-margin-collapsing/ The idea is that the box model will have items above collapse the margins of items below. Elements contained within other elements will have their margins collapse before the outer margins do.
Whose margins collapse:
- Inner elements collapse first (empty elements collapse in on themselves) – their margins fold.
- Elements lower down on the page
- Elements higher on the page
Block Level elements (block boxes) versus inline elements (inline boxes)
Block Level Elements are elements that are displayed as blockes or boxes of content:
- p
- h1
- h2
- div
Inline Elements are items that are displayed on one line (inline) such as:
- a
- strong
- span
The differences between these types is the DISPLAY property.
- display=block
- display=inline-block
- display=none /*no box at all*/
- Positioning
3 different types of positioning in HTML:
- normal flow (default) – based on position in HTML, left to right
- floats
- and absolute positioning
- Normal Positioning
Inline Boxes = normal flow dictates left to right horizontal stacking
Block Boxes = normal flow dictates top down vertical stacking
Relative Positioning
Allows the element to be positioned relative to its normal position. The element will continue to occupy the the original space (with or without the offset) – the implication can be overlap.
Absolute Positioning
Positioning is in relation to the nearest positioned ancestor (the tag in which it is nested).
Stacking (who is on top) can be controlled based on a z-index value. Higher the z-index, high up the box appears (more in front).
There is a bug in IE 5.5 & 6 when absolute position is set in relation to a relatively positioned element that DID NOT have dimensions set (IE just sets absolute in relation to canvas) – so set the dimensions.
Absolute positioned items are not considered part of the normal flow. They will not affect the layout of relative or normal flow positioned elements.
Fixed Positioning ( A subcategory of absolute )
The fixed element block is set relative to the viewport (window) and thus the element will always stay in the viewable window. (not supported by IE6 and below, IE7 partially supports it… There are workarounds [ugh])
Float Positioning
This is a complex and very important topic that you need to understand. That said, after reading it, I’ll refer you to some other sources as I don’t want to recreate what they have.
This site covers FLOAT & CLEAR: http://www.positioniseverything.net/easyclearing.html
I also recommend a framework that Erick Flemming and I are using on our project. The guy who wrote it is German, so you know it’s good ;-), YAML – Yet Another Multicolumn Layout.
YAML: Bulletproof & Flexible Layouts Made Simple
http://www.yaml.de/en/home.html
"Yet Another Multicolumn Layout" (YAML) is an (X)HTML/CSS framework for creating modern and flexible floated layouts. The structure is extremely versatile in its programming and absolutely accessible for end users.
He even has a solid YAML BUILDER. http://builder.yaml.de/