The layout process performed by browsers usually has the following pattern:
- Parent element renderer determines its own width.
- Parent element goes over children and:
- Place the child element renderer (sets its x and y).
- Calls child layout if needed(they are dirty or we are in a global layout or some other reason) - this calculates the child's height.
- Parent uses children accumulative heights and the heights of the margins and paddings to set it own height - this will be used by the parent renderer's parent.
- Sets its dirty bit to false.
The renderer's width is calculated using the container block's width , the renderer's style "width" property, the margins and borders. For example the width of the following div:
<div style="width:30%"/>
Would be calculated by Webkit as following (class RenderBox method calcWidth):
-
The container width is the maximum of the containers availableWidth and 0. The availableWidth in this case is the contentWidth which is calculated as:
clientWidth() - paddingLeft() - paddingRight()
clientWidth
andclientHeight
represent the interior of an object excluding border and scrollbar. -
The elements width is the "width" style attribute. It will be calculated as an absolute value by computing the percentage of the container width.
-
The horizontal borders and paddings are now added.
So far this was the calculation of the preferred width. Now the minimum and maximum widths will be calculated.
- If the preferred width is higher than the maximum width, then maximum width is used.
- If it is lower than the minimum width (the smallest unbreakable unit), then the minimum width is used.