Contents
The CSS box model describes the rectangular boxes that aregenerated for elements in thedocumenttree and laid out according to thevisual formattingmodel.
Each box has acontent area (e.g.,text, an image, etc.) and optional surroundingpadding,border, andmargin areas; the sizeof each area is specified by properties defined below. The followingdiagram shows how these areas relate and the terminology used to referto pieces of margin, border, and padding:
The margin, border, and padding can be broken down into top, right,bottom, and left 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:
Each edge may be broken down into a top, right, bottom, and leftedge.
The dimensions of the content area of a box — the
The background style of the content, padding, and border areas of abox is specified by the
This example illustrates how margins, padding, and bordersinteract. The example HTML document:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"><HTML> <HEAD> <TITLE>Examples of margins, padding, and borders</TITLE> <STYLE type="text/css"> UL { background: yellow; margin: 12px 12px 12px 12px; padding: 3px 3px 3px 3px; /* No borders set */ } LI { color: white; /* text color is white */ background: blue; /* Content, padding will be blue */ margin: 12px 12px 12px 12px; padding: 12px 0px 12px 12px; /* Note 0px padding right */ list-style: none /* no glyphs before a list item */ /* No borders set */ } LI.withborder { border-style: dashed; border-width: medium; /* sets border width on all sides */ border-color: lime; } </STYLE> </HEAD> <BODY> <UL> <LI>First element of list <LI>Second element of list is a bit longer to illustrate wrapping. </UL> </BODY></HTML>
results in adocument tree with(among other relationships) a UL element that has two LIchildren.
The first of the following diagrams illustrates what this examplewould produce. The second illustrates the relationship between themargins, padding, and borders of the UL elements and those of itschildren LI elements. (Image is not to scale.)
Note that:
Margin properties specify the width of themargin area of a box. The
The properties defined in this section refer to the
Negative values for margin properties are allowed, but there may beimplementation-specific limits.
Value: | <margin-width> |inherit |
Initial: | 0 |
Applies to: | all elements except elements with table display types other than table-caption, table and inline-table |
Inherited: | no |
Percentages: | refer to width of containing block |
Media: | visual |
Computed value: | the percentage as specified or the absolute length |
These properties have no effect on non-replaced inlineelements.
Value: | <margin-width> |inherit |
Initial: | 0 |
Applies to: | all elements except elements with table display types other than table-caption, table and inline-table |
Inherited: | no |
Percentages: | refer to width of containing block |
Media: | visual |
Computed value: | the percentage as specified or the absolute length |
These properties set the top, right, bottom, and left margin of abox.
Example(s):
h1 { margin-top: 2em }
Value: | <margin-width>{1,4} |inherit |
Initial: | see individual properties |
Applies to: | all elements except elements with table display types other than table-caption, table and inline-table |
Inherited: | no |
Percentages: | refer to width of containing block |
Media: | visual |
Computed value: | see individual properties |
The'margin' property is ashorthand property for setting
If there is only one component value, it applies to allsides. If there are two values, the top and bottom marginsare set to the first value and the right and left margins areset to the second. If there are three values, the top isset to the first value, the left and right are set to thesecond, and the bottom is set to the third. If there arefour values, they apply to the top, right, bottom, and left,respectively.
Example(s):
body { margin: 2em } /* all margins set to 2em */body { margin: 1em 2em } /* top & bottom = 1em, right & left = 2em */body { margin: 1em 2em 3em } /* top=1em, right=2em, bottom=3em, left=2em */
The last rule of the example above is equivalent to the examplebelow:
body { margin-top: 1em; margin-right: 2em; margin-bottom: 3em; margin-left: 2em; /* copied from opposite side (right) */}
In CSS, the adjoining margins of two or more boxes (which might ormight not be siblings) can combine to form a single margin. Marginsthat combine this way are said to
Adjoining vertical margins collapse, except:
Horizontal margins never collapse.
Two margins are
A collapsed margin is considered adjoining to another margin if anyof its component margins is adjoining to that margin.
Note. Adjoining margins can begenerated by elements that are not related as siblings or ancestors.
Note the above rules imply that:
When two or more margins collapse, the resulting margin width isthe maximum of the collapsing margins' widths. In the case of negativemargins, the maximum of the absolute values of the negative adjoiningmargins is deducted from the maximum of the positive adjoiningmargins. If there are no positive margins, the maximum of theabsolute values of the adjoining margins is deducted from zero.
If the top and bottom margins of a box areadjoining, then it is possible for margins to
Note that the positions of elements that have been collapsedthrough have no effect on the positions of the other elements withwhose margins they are being collapsed; the top border edge positionis only required for laying out descendants of these elements.
The padding properties specify the width of thepadding area of a box. The
The properties defined in this section refer to the
Unlike margin properties, values for padding values cannot benegative. Like margin properties, percentage values for paddingproperties refer to the width of the generated box's containing block.
Value: | <padding-width> |inherit |
Initial: | 0 |
Applies to: | all elements except table-row-group, table-header-group, table-footer-group, table-row, table-column-group and table-column |
Inherited: | no |
Percentages: | refer to width of containing block |
Media: | visual |
Computed value: | the percentage as specified or the absolute length |
These properties set the top, right, bottom, and left padding ofa box.
Example(s):
blockquote { padding-top: 0.3em }
Value: | <padding-width>{1,4} |inherit |
Initial: | see individual properties |
Applies to: | all elements except table-row-group, table-header-group, table-footer-group, table-row, table-column-group and table-column |
Inherited: | no |
Percentages: | refer to width of containing block |
Media: | visual |
Computed value: | see individual properties |
The'padding' property is ashorthand property for setting
If there is only one component value, it applies to allsides. If there are two values, the top and bottom paddingsare set to the first value and the right and left paddings areset to the second. If there are three values, the top isset to the first value, the left and right are set to thesecond, and the bottom is set to the third. If there arefour values, they apply to the top, right, bottom, and left,respectively.
The surface color or image of the padding area is specified viathe'background' property:
Example(s):
h1 { background: white; padding: 1em 2em;}
The example above specifies a '1em' vertical padding (
The border properties specify the width, color, and style of theborder area of a box. These propertiesapply to all elements.
Note.Notably for HTML, user agents may render borders for certain user interface elements (e.g.,buttons, menus, etc.) differently than for"ordinary" elements.
The border width properties specify the width of theborder area. The propertiesdefined in this section refer to the
The interpretation of the first three values depends on the useragent. The following relationships must hold, however:
'thin' <='medium' <= 'thick'.
Furthermore, these widths must be constant throughout a document.
Value: | <border-width> |inherit |
Initial: | medium |
Applies to: | all elements |
Inherited: | no |
Percentages: | N/A |
Media: | visual |
Computed value: | absolute length; '0' if the border style is 'none' or 'hidden' |
These properties set the width of the top, right, bottom,and left border of a box.
Value: | <border-width>{1,4} |inherit |
Initial: | see individual properties |
Applies to: | all elements |
Inherited: | no |
Percentages: | N/A |
Media: | visual |
Computed value: | see individual properties |
This property is a shorthand property for setting'border-top-width','border-right-width','border-bottom-width', and'border-left-width' atthe same place in the style sheet.
If there is only one component value, it applies to allsides. If there are two values, the top and bottom bordersare set to the first value and the right and left areset to the second. If there are three values, the top isset to the first value, the left and right are set to thesecond, and the bottom is set to the third. If there arefour values, they apply to the top, right, bottom, and left,respectively.
Example(s):
In the examples below, the comments indicate the resulting widthsof the top, right, bottom, and left borders:
h1 { border-width: thin } /* thin thin thin thin */h1 { border-width: thin thick } /* thin thick thin thick */h1 { border-width: thin thick medium } /* thin thick medium thick */
The border color properties specify the color of a box's border.
Value: | <color> | transparent |inherit |
Initial: | the value of the 'color' property |
Applies to: | all elements |
Inherited: | no |
Percentages: | N/A |
Media: | visual |
Computed value: | when taken from the 'color' property, the computed value of 'color'; otherwise, as specified |
Value: | [<color> | transparent ]{1,4} |inherit |
Initial: | see individual properties |
Applies to: | all elements |
Inherited: | no |
Percentages: | N/A |
Media: | visual |
Computed value: | see individual properties |
The'border-color'property sets the color of the four borders. Values have the followingmeanings:
The'border-color'property can have from one to four component values, and the valuesare set on the different sides as for
If an element's border color is not specified with a border property, user agents must use the valueof the element's
Example(s):
In this example, the border will be a solid black line.
p { color: black; background: white; border: solid;}
The border style properties specify the line style of a box'sborder (solid, double, dashed, etc.). The properties defined in thissection refer to the
All borders are drawn on top of the box's background. The color ofborders drawn for values of 'groove', 'ridge', 'inset', and 'outset'depends on the element'sbordercolor properties, but UAs may choose their own algorithm tocalculate the actual colors used. For instance, if the 'border-color'has the value 'silver', then a UA could use a gradient of colors fromwhite to dark gray to indicate a sloping border.
Value: | <border-style> |inherit |
Initial: | none |
Applies to: | all elements |
Inherited: | no |
Percentages: | N/A |
Media: | visual |
Computed value: | as specified |
Value: | <border-style>{1,4} |inherit |
Initial: | see individual properties |
Applies to: | all elements |
Inherited: | no |
Percentages: | N/A |
Media: | visual |
Computed value: | see individual properties |
The'border-style'property sets the style of the four borders. It can have from one tofour component values, and the values are set on the different sides as for'border-width' above.
Example(s):
#xy34 { border-style: solid dotted }
In the above example, the horizontal borders will be 'solid' andthe vertical borders will be 'dotted'.
Since the initial value of the border styles is 'none', no borderswill be visible unless the border style is set.
Value: | [<border-width> ||<border-style> ||<'border-top-color'> ] |inherit |
Initial: | see individual properties |
Applies to: | all elements |
Inherited: | no |
Percentages: | N/A |
Media: | visual |
Computed value: | see individual properties |
This is a shorthand property for setting the width, style, andcolor of the top, right, bottom, and left border of a box.
Example(s):
h1 { border-bottom: thick solid red }
The above rule will set the width, style, and color of the borderbelow the H1 element. Omitted values are set totheirinitial values. Sincethe following rule does not specify a border color, the border willhave the color specified by the'color' property:
H1 { border-bottom: thick solid }
Value: | [<border-width> ||<border-style> ||<'border-top-color'> ] |inherit |
Initial: | see individual properties |
Applies to: | all elements |
Inherited: | no |
Percentages: | N/A |
Media: | visual |
Computed value: | see individual properties |
The'border' property is ashorthand property for setting the same width, color, and style forall four borders of a box. Unlike the shorthand
Example(s):
For example, the first rule below isequivalent to the set of four rules shown after it:
p { border: solid red }p { border-top: solid red; border-right: solid red; border-bottom: solid red; border-left: solid red}
Since, to some extent, the properties have overlappingfunctionality, the order in which the rules are specified isimportant.
Example(s):
Consider this example:
blockquote { border: solid red; border-left: double; color: black;}
In the above example, the color of the left border is black,while the other borders are red. This is due to
For each line box, UAs must take the inline boxes generated foreach element and render the margins, borders and padding in visualorder (not logical order).
When the element's
When the element's