Contents
The following sections include the definitions of box properties and other details about box generation.
Margin properties specify the width of the margin area of a box. The 'margin' shorthand property sets the margin for all four sides while the other margin properties only set their respective side. These properties apply to all elements generating block-level boxes \ that are not absolutely positioned and they apply to elements for which 'display' is set to 'caption'.
The properties defined in this section refer to the <margin-width> value type, which may take one of the following values:
Negative values for margin properties are allowed, but there may be implementation-specific limits. Percentage values for margin properties refer to the width of the generated box's containing block.
Property name: | 'margin-top' |
---|---|
Value: | <margin-width> | inherit |
Initial: | 0 |
Applies to: | all elements |
Inherited: | no |
Percentage values: | refer to width of containing block |
Media groups: | visual |
This property sets the top margin of a generated box.
H1 { margin-top: 2em }
Property name: | 'margin-right' |
---|---|
Value: | <margin-width> | inherit |
Initial: | 0 |
Applies to: | all elements |
Inherited: | no |
Percentage values: | refer to width of containing block |
Media groups: | visual |
This property sets the right margin of a generated box.
H1 { margin-right: 12.3% }
Property name: | 'margin-bottom' |
---|---|
Value: | <margin-width> | inherit |
Initial: | 0 |
Applies to: | all elements |
Inherited: | no |
Percentage values: | refer to width of containing block |
Media groups: | visual |
This property sets the bottom margin of a generated box.
H1 { margin-bottom: 3px }
Property name: | 'margin-left' |
---|---|
Value: | <margin-width> | inherit |
Initial: | 0 |
Applies to: | all elements |
Inherited: | no |
Percentage values: | refer to width of containing block |
Media groups: | visual |
This property sets the left margin of a generated box.
H1 { margin-left: 2em }
Property name: | 'margin' |
---|---|
Value: | <margin-width>{1,4} | inherit |
Initial: | not defined for shorthand properties |
Applies to: | all elements |
Inherited: | no |
Percentage values: | refer to width of containing block |
Media groups: | visual |
The 'margin' property is a shorthand property for setting 'margin-top', 'margin-right' 'margin-bottom' and 'margin-left' at the same place in the style sheet.
If there is only one value, it applies to all sides. If there are two values, the top and bottom margins are set to the first value and the right and left margins are set to the second. If there are three values, the top is set to the first value, the left and right are set to the second, and the bottom is set to the third. If there are four values, they apply to the top, right, bottom, and left, respectively.
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 example below:
BODY { margin-top: 1em; margin-right: 2em; margin-bottom: 3em; margin-left: 2em; /* copied from opposite side (right) */ }
The padding properties specify the width of the content area of a box. The 'padding' shorthand property sets the padding for all four sides while the other padding properties only set their respective side. These properties apply to all elements.
The properties defined in this section refer to the <padding-width> value type, which may take one of the following values:
Unlike margin properties, values for padding values cannot be negative. Like margin properties, percentage values for padding properties refer to the width of the generated box's containing block.
Property name: | 'padding-top' |
---|---|
Value: | <padding-width> | inherit |
Initial: | 0 |
Applies to: | all elements |
Inherited: | no |
Percentage values: | refer to width of containing block |
Media groups: | visual |
This property sets the top padding of a generated box.
BLOCKQUOTE { padding-top: 0.3em }
Property name: | 'padding-right' |
---|---|
Value: | <padding-width> | inherit |
Initial: | 0 |
Applies to: | all elements |
Inherited: | no |
Percentage values: | refer to width of containing block |
Media groups: | visual |
This property sets the right padding of a generated box.
BLOCKQUOTE { padding-right: 10px }
Property name: | 'padding-bottom' |
---|---|
Value: | <padding-width> | inherit |
Initial: | 0 |
Applies to: | all elements |
Inherited: | no |
Percentage values: | refer to width of containing block |
Media groups: | visual |
This property sets the bottom padding of a generated box.
BLOCKQUOTE { padding-bottom: 2em }
Property name: | 'padding-left' |
---|---|
Value: | <padding-width> | inherit |
Initial: | 0 |
Applies to: | all elements |
Inherited: | no |
Percentage values: | refer to width of containing block |
Media groups: | visual |
This property sets the left padding of a generated box.
BLOCKQUOTE { padding-left: 20% }
Property name: | 'padding' |
---|---|
Value: | <padding-width>{1,4} | inherit |
Initial: | not defined for shorthand properties |
Applies to: | all elements |
Inherited: | no |
Percentage values: | refer to width of containing block |
Media groups: | visual |
The 'padding' property is a shorthand property for setting 'padding-top', 'padding-right', 'padding-bottom', and 'padding-left' at the same place in the style sheet.
If there is only one value, it applies to all sides. If there are two values, the top and bottom paddings are set to the first value and the right and left paddings are set to the second. If there are three values, the top is set to the first value, the left and right are set to the second, and the bottom is set to the third. If there are four values, they apply to the top, right, bottom, and left, respectively.
The surface color or image of the padding area is specified via the 'background' property:
H1 { background: white; padding: 1em 2em; }
The example above specifies a '1em' vertical padding ('padding-top' and 'padding-bottom') and a '2em' horizontal padding ('padding-right' and 'padding-left'). The 'em' unit is relative to the element's font size: '1em' is equal to the size of the font in use.
The border properties specify the width, color, and style of the border area of a box. These properties apply to all elements.
The border width properties specify the width of the border area. The properties defined in this section refer to the <border-width> value type, which may take one of the following values:
The interpretation of the first three values depends on the user agent. The following relationships must hold, however:
'thin' <='medium' <= 'thick'.
Furthermore, these widths must be constant throughout a document.
Property name: | 'border-top-width' |
---|---|
Value: | <border-width> | inherit |
Initial: | medium |
Applies to: | all elements |
Inherited: | no |
Percentage values: | N/A |
Media groups: | visual |
This property sets the width of the top border of a box.
H1 { border: solid thick red } P { border: solid thick blue }
In the example above, H1 and P elements will have the same border width regardless of font size. To achieve relative widths, the 'em' unit can be used:
H1 { border: solid 0.5em }
Property name: | 'border-right-width' |
---|---|
Value: | <border-width> | inherit |
Initial: | medium |
Applies to: | all elements |
Inherited: | no |
Percentage values: | N/A |
Media groups: | visual |
This property sets the width of the right border of a box.
Property name: | 'border-bottom-width' |
---|---|
Value: | <border-width> | inherit |
Initial: | medium |
Applies to: | all elements |
Inherited: | no |
Percentage values: | N/A |
Media groups: | visual |
This property sets the width of the bottom border of a box.
Property name: | 'border-left-width' |
---|---|
Value: | <border-width> | inherit |
Initial: | medium |
Applies to: | all elements |
Inherited: | no |
Percentage values: | N/A |
Media groups: | visual |
This property sets the width of the left border of a box.
Property name: | 'border-width' |
---|---|
Value: | <border-width>{1,4} | inherit |
Initial: | see individual properties |
Applies to: | all elements |
Inherited: | no |
Percentage values: | N/A |
Media groups: | visual |
This property is a shorthand property for setting 'border-top-width', 'border-right-width', 'border-bottom-width', and 'border-left-width' at the same place in the style sheet.
If there is only one value, it applies to all sides. If there are two values, the top and bottom borders are set to the first value and the right and left are set to the second. If there are three values, the top is set to the first value, the left and right are set to the second, and the bottom is set to the third. If there are four values, they apply to the top, right, bottom, and left, respectively.
In the examples below, the comments indicate the resulting widths of 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.
Property name: | 'border-top-color' |
---|---|
Value: | <color> | inherit |
Initial: | the value of the 'color' property |
Applies to: | all elements |
Inherited: | no |
Percentage values: | N/A |
Media groups: | visual |
Property name: | 'border-right-color' |
---|---|
Value: | <color> | inherit |
Initial: | the value of the 'color' property |
Applies to: | all elements |
Inherited: | no |
Percentage values: | N/A |
Media groups: | visual |
Property name: | 'border-bottom-color' |
---|---|
Value: | <color> | inherit |
Initial: | the value of the 'color' property |
Applies to: | all elements |
Inherited: | no |
Percentage values: | N/A |
Media groups: | visual |
Property name: | 'border-left-color' |
---|---|
Value: | <color> | inherit |
Initial: | the value of the 'color' property |
Applies to: | all elements |
Inherited: | no |
Percentage values: | N/A |
Media groups: | visual |
Property name: | 'border-color' |
---|---|
Value: | <color>{1,4} | inherit |
Initial: | see individual properties |
Applies to: | all elements |
Inherited: | no |
Percentage values: | N/A |
Media groups: | visual |
The 'border-color' property sets the color of the four borders. 'border-color' can have from one to four values, and the values are set on the different sides as for 'border-width'.
If an element's border color is not specified with a border property, user agents must use the value of the element's 'color' property as the computed value for the border color.
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's border (solid, double, dashed, etc.). The properties defined in this section refer to the <border-style> value type, which make take one of the following:
All borders are drawn on top of the box's background. The color of borders drawn for values of 'groove', 'ridge', 'inset', and 'outset' depends on the element's 'color' property.
Conforming HTML user agents may interpret 'dotted', 'dashed', 'double', 'groove', 'ridge', 'inset', and 'outset' to be 'solid'.
Property name: | 'border-top-style' |
---|---|
Value: | <border-style> | inherit |
Initial: | none |
Applies to: | all elements |
Inherited: | no |
Percentage values: | N/A |
Media groups: | visual |
Property name: | 'border-right-style' |
---|---|
Value: | <border-style> | inherit |
Initial: | none |
Applies to: | all elements |
Inherited: | no |
Percentage values: | N/A |
Media groups: | visual |
Property name: | 'border-bottom-style' |
---|---|
Value: | <border-style> | inherit |
Initial: | none |
Applies to: | all elements |
Inherited: | no |
Percentage values: | N/A |
Media groups: | visual |
Property name: | 'border-left-style' |
---|---|
Value: | <border-style> | inherit |
Initial: | none |
Applies to: | all elements |
Inherited: | no |
Percentage values: | N/A |
Media groups: | visual |
Property name: | 'border-style' |
---|---|
Value: | <border-style>{1,4} | inherit |
Initial: | see individual properties |
Applies to: | all elements |
Inherited: | no |
Percentage values: | N/A |
Media groups: | visual |
The 'border-style' property sets the style of the four borders. It can have from one to four values, and the values are set on the different sides as for 'border-width' above.
#xy34 { border-style: solid dotted }
In the above example, the horizontal borders will be 'solid' and the vertical borders will be 'dotted'.
Since the initial value of the border styles is 'none', no borders will be visible unless the border style is set.
Property name: | 'border-top' |
---|---|
Value: | [ <'border-top-width'> || <'border-style'> || <color> ] | inherit |
Initial: | see individual properties |
Applies to: | all elements |
Inherited: | no |
Percentage values: | N/A |
Media groups: | visual |
This is a shorthand property for setting the width, style, and color of the top border of a box.
H1 { border-bottom: thick solid red }
The above rule will set the width, style, and color of the border below the H1 element. Omitted values will be set to their initial values:
H1 { border-bottom: thick solid }
Since the color value is omitted in the above example, the border color will be the same as the 'color' value of the element itself.
Note that while the 'border-style' property accepts up to four values, this property only accepts one style value.
Property name: | 'border-bottom' |
---|---|
Value: | [ <'border-bottom-width'> || <'border-style'> || <color> ] | inherit |
Initial: | see individual properties |
Applies to: | all elements |
Inherited: | no |
Percentage values: | N/A |
Media groups: | visual |
This is a shorthand property for setting the width, style, and color of the bottom border of a box. Otherwise, it behaves like 'border-top'.
Property name: | 'border-right' |
---|---|
Value: | [ <'border-right-width'> || <'border-style'> || <color> ] | inherit |
Initial: | see individual properties |
Applies to: | all elements |
Inherited: | no |
Percentage values: | N/A |
Media groups: | visual |
This is a shorthand property for setting the width, style, and color of the right border of a box. Otherwise, it behaves like 'border-top'.
Property name: | 'border-left' |
---|---|
Value: | [ <'border-left-width'> || <'border-style'> || <color> ] | inherit |
Initial: | see individual properties |
Applies to: | all elements |
Inherited: | no |
Percentage values: | N/A |
Media groups: | visual |
This is a shorthand property for setting the width, style, and color of the left border of a box. Otherwise, it behaves like 'border-top'.
Property name: | 'border' |
---|---|
Value: | [ <'border-width'> || <'border-style'> || <color> ] | inherit |
Initial: | see individual properties |
Applies to: | all elements |
Inherited: | no |
Percentage values: | N/A |
Media groups: | visual |
The 'border' property is a shorthand property for setting the same width, color, and style for all four borders of a box. Unlike the shorthand 'margin' and 'padding' properties, the 'border' property cannot set different values on the four borders. To do so, one or more of the other border properties must be used. Note that while the 'border-width' property accepts up to four length values, this property only accepts one.
For example, the first rule below is equivalent 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 overlapping functionality, the order in which the rules are specified is important.
Consider this example:
BLOCKQUOTE { border-color: 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 'border-left' setting the width, style, and color. Since the color value is not given by the 'border-left' property, it will be taken from the 'color' property. The fact that the 'color' property is set after the 'border-left' property is not relevant.
In CSS2, all box positions are calculated with respect to the edges of a rectangular box called a containing block.
The root of the document tree generates a box that serves as the initial containing block for subsequent layout. If the 'width' property for the root element has the value 'auto', the user agent supplies an initial width (e.g., the user agent uses the current width of the viewport).
The initial containing block cannot be positioned (i.e., user agents skip the 'position' property) or floated (i.e., user agents skip the 'float' property).
The containing block for an box other than the root box determined as follows:
When the containing block is established by a block-level box, it has the same width, height, and position as the parent's padding edge. When the containing block is established by an inline box, it has the same width, height, and position as the padding edge of the first line box generated by the inline box.
Relatively positioned inline boxes must be considered specially since (1) the rendered content of an inline element may not be rectangular in shape but (2) a relatively positioned box establishes a new containing block and containing blocks must be rectangular. The reference edges for descendants are the following:
As discussed in the section on box dimensions, the content width of a box is either assigned explicitly (via the 'width' property) or calculated "top-down" (based on box dimensions and the available horizontal space of its containing block). The following sections explain the exact computation of a box's width-related properties.
Property name: | 'width' |
---|---|
Value: | <length> | <percentage> | auto | inherit |
Initial: | auto |
Applies to: | all elements but non-replaced inline elements, table rows and row groups |
Inherited: | no |
Percentage values: | refer to width of containing block |
Media groups: | visual |
This property specifies a box's content width. Values have the following meanings:
Negative values for 'width' are illegal. User agents must skip this property for elements with 'display' set to 'row' or 'row-span'
For example:
IMG.icon { width: 100px }
The 'width' property does not apply to non-replaced inline elements. The width of the line boxes is determined by their containing block.
For other types of boxes in the normal flow and floats, a box's margin, padding, border, and content width must equal the width of its containing block. These widths are specified with the following seven properties: 'margin-left', 'border-left', 'padding-left', 'width', 'padding-right', 'border-right', and 'margin-right'.
Three of these properties ('margin-left', 'width', and 'margin-right') may take the value 'auto'. All properties with 'auto' values must be assigned computed values by the user agent during layout. User agents must assign computed values as follows.
When the 'width' property has the value 'auto', user agents should assign it a computed value based on computed margin widths. In addition:
User agents should assign computed values for 'margin-left', 'width', and 'margin-right' as follows:
The width of an absolutely positioned box is specified via the source element's 'width' property.
However, if the 'width' property has the value 'auto', the width of the box is given by the 'left' and 'right' properties. Note that these take the place of the 'margin-left' and 'margin-right' properties, which don't apply to absolutely positioned elements.
If all three properties have the value 'auto', the box has exactly the width of its containing block.
Property name: | 'min-width' |
---|---|
Value: | <length> | <percentage> | inherit |
Initial: | 0 |
Applies to: | all |
Inherited: | no |
Percentage values: | refer to parent's width |
Media groups: | visual |
Property name: | 'max-width' |
---|---|
Value: | <length> | <percentage> | inherit |
Initial: | 100% |
Applies to: | all |
Inherited: | no |
Percentage values: | refer to parent's width |
Media groups: | visual |
These two properties allow authors to constrain box widths to a certain range. Values have the following meanings:
The following algorithm describes how the two properties influence the computed value of the 'width' property:
As discussed in the section on box dimensions, the content height of a box is either assigned explicitly (via the 'height' property) or is the minimal height necessary to include the vertical content of the element and that of all its flowed children. This is the height necessary before any relative offset of children.
Property name: | 'height' |
---|---|
Value: | <length> | <percentage> | auto | inherit |
Initial: | auto |
Applies to: | all elements but non-replaced inline elements, table columns and column groups |
Inherited: | no |
Percentage values: | see prose |
Media groups: | visual |
This property specifies a box's content height. Values have the following meanings:
Negative values for 'height' are illegal. User agents must skip this property for elements with 'display' set to 'col' or 'column-span'
IMG.icon { height: 100px }
The height may be enforced by the user interface (e.g., a scrollbar).
When the 'height' property has the value 'auto', user agents should assign it a computed value based on the space required by the element's rendered content. In addition:
The height of an absolutely positioned box is specified via the source element's 'height' property. However, specifying a percentage value for 'height' when the parent element's height is set to 'auto' results in undefined behavior.
If the 'height' has the value 'auto', the height of the box is given by the 'top' and 'bottom' properties. Note that these take the place of the 'margin-top' and 'margin-bottom' properties, which don't apply to absolutely positioned elements.
If all three properties have the value 'auto', the box has exactly the height of its containing block.
It is sometimes useful to constrain the height of elements to a certain range. Two properties offer this functionality:
Property name: | 'min-height' |
---|---|
Value: | <length> | <percentage> | inherit |
Initial: | 0 |
Applies to: | all |
Inherited: | no |
Percentage values: | refer to parent's height |
Media groups: | visual |
Property name: | 'max-height' |
---|---|
Value: | <length> | <percentage> | inherit |
Initial: | 100% |
Applies to: | all |
Inherited: | no |
Percentage values: | refer to parent's height |
Media groups: | visual |
These two properties allow authors to constrain box heights to a certain range. Values have the following meanings:
The following algorithm describes how the two properties influence the computed value of the 'height' property:
In CSS2, horizontal margins never collapse.
Vertical margins may collapse for certain types of boxes:
In most cases, after collapsing the vertical margins, the result is visually more pleasing and closer to what the designer expects. Please consult the examples of margin, padding, and borders for an illustration of collapsed margins.
As described in the section on inline formatting contexts, user agents flow inline boxes into a vertical stack of line boxes. The height of a line box is determined as follows:
In the case of an inline replaced element, the 'height' property determines the height of the inline box.
Note that if all the boxes in the line box are aligned along their bottoms, the line box will be exactly the height of the tallest box. If, however, the boxes are aligned along a common baseline, the line box top and bottom may not touch the top and bottom of the tallest box.
Note that top and bottom margins, borders, and padding specified for inline elements do not enter into the calculation of line box heights.
Since the height of an inline box may be greater than ('line-height' > 1) the font size of text in the box, there may be space above and below rendered glyphs. The difference between the font size and the computed value of 'line-height' is called the leading. Half the leading is called the half-leading.
User agents should center the glyph vertically in the box (adding half-leading on the top and bottom). For example, if a piece of text is '12pt' high and the 'line-height' value is '14pt', 2pts of extra space should be added (e.g., 1pt above and 1pt below the box). Empty elements influence these calculations just like elements with content.
When the 'line-height' value is less than the font size, the final line box height will be less than the font size and the rendered glyphs will "bleed" outside the box. If such a box touches the edge of a line box, the rendered glyphs will also "bleed" into the adjacent line box.
Although margins, borders, and padding do not enter into the line box height calculation, they are still rendered around inline boxes (except when boxes are split across lines). This means that if the height of a line box is shorter than the outer edges of the boxes it contains, backgrounds and colors may "bleed" into adjacent line boxes.
Property name: | 'line-height' |
---|---|
Value: | normal | <number> | <length> | <percentage> | inherit |
Initial: | normal |
Applies to: | all elements |
Inherited: | yes |
Percentage values: | relative to the font size of the element itself |
Media groups: | visual |
The property specifies the height of an inline box. The height generally refers to the font size of the element's text. For replaced elements, the height refers to the intrinsic height.
Values for this property have the following meanings:
The three rules in the example below have the same resultant line height:
DIV { line-height: 1.2; font-size: 10pt } /* number */ DIV { line-height: 1.2em; font-size: 10pt } /* length */ DIV { line-height: 120%; font-size: 10pt } /* percentage */
When an element contains text that is rendered in more than one font, user agents should determine the 'line-height' value according to the largest font size.
Note. Generally, when there is only one value of 'line-height' affecting all line boxes in a paragraph (and not tall images), the above will ensure that baselines of successive lines are exactly 'line-height' apart. This is important when columns of text in different fonts have to be aligned, for example in a table.
Property name: | 'vertical-align' |
---|---|
Value: | baseline | sub | super | top | text-top | middle | bottom | text-bottom | <percentage> | <length> | inherit |
Initial: | baseline |
Applies to: | inline elements |
Inherited: | no |
Percentage values: | refer to the 'line-height' of the element itself |
Media groups: | visual |
This property affects the vertical positioning inside a line box of the boxes generated by an inline element. The following values only have meaning with respect to a parent inline element; they have no effect if no parent exists:
The remaining values refer to the line box in which the generated box appears:
A floating box is positioned subject to the following constraints: