CSS box model
In : Uncategorized, Posted by admin on Mar.03, 2009
Each box has a content area (e.g., text, an image, etc.) and optional surrounding padding, border, and margin areas; the size of each area is specified by properties defined below. The following diagram shows how these areas relate and the terminology used to refer to pieces of margin, border, and padding:
The margin, border, and padding can be broken down into left, right, top, and bottom segments (e.g., in the diagram, “LM” for left margin, “RP” for right padding, “TB” for top border, etc.).
The perimeter of each of the four areas (content, padding, border, and margin) is called an “edge”, so each box has four edges:
- content edge or inner edge
- The content edge surrounds the element’s rendered content.
- padding edge
- The padding edge surrounds the box padding. If the padding has 0 width, the padding edge is the same as the content edge. The padding edge of a box defines the edges of the containing block established by the box.
- border edge
- The border edge surrounds the box’s border. If the border has 0 width, the border edge is the same as the padding edge.
- margin edge or outer edge
- The margin edge surrounds the box margin. If the margin has 0 width, the margin edge is the same as the border edge.
Each edge may be broken down into a left, right, top, and bottom edge.
The dimensions of the content area of a box — the content width and content height — depend on several factors: whether the element generating the box has the ‘width’ or ‘height’ property set, whether the box contains text or other boxes, whether the box is a table, etc. Box widths and heights are discussed in the chapter on visual formatting model details.
The box width is given by the sum of the left and right margins, border, and padding, and the content width. The height is given by the sum of the top and bottom margins, border, and padding, and the content height.
The background style of the various areas of a box are determined as follows:
- Content area: The ‘background’ property of the generating element.
- Padding area: The ‘background’ property of the generating element.
- Border area: The border properties of the generating element.
- Margin area: Margins are always transparent.
Problem with IE css hack
According to the W3C, an assigned ‘width’ (and ‘height’) of a box refers to the ‘content area’ of a box only. The padding, borders, and margins are then added to this value to arrive at the total box width. If the ‘width’ property is omitted, the total box width is the same as the ‘content area’ of the surrounding container element.
All well and good. Unfortunately, all CSS enabled versions of IE before IE6/strict use a different box model. In that model, the padding and borders are counted as part of any assigned ‘width’ or ‘height’. In the absence of borders and padding, the two models agree. However, if a box has an assigned “width’, and if borders and/or padding are added, the standard box model causes the overall box width (between the outer border edges) to increase, while in IE’s model the ‘content area’ gets squeezed by the same amount. This is a major problem for proper page layout.
Consider the following CSS:
- {width:100px; padding:10px; border:10px;}
When viewed in a ‘standards’ browser the dimension from border edge to border edge will be ’140px’. (100+10+10+10+10=140) Because IE5.x puts all these values inside the ‘width’, the border edge to border edge dimension will be ’100px’.
Note: For technical reasons it sometimes would be desirable to employ the old IE box model. It has been bruited about that CSS-3 will feature a way to choose between the two models, but the current standard model will no doubt remain the default.
