margin-top, -right, -bottom, -left
padding-top,... padding
The CSS box model describes the rectangular boxes that are generated for elements in the document tree and laid out according to the visual formatting model. The page box is a special kind of box that is described in detail in the section on paged media.
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
is 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".
content edge or inner edge | surrounds the element's rendered content. | ||||
padding edge | surrounds the box padding. If the padding has 0 width, the padding edge is the same as the content edge. of a box defines the edges of the containing block established by the box. 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 surrounds the box margin. | If the margin has 0 width, the margin edge is the same as the border edge. |
Each edge is 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:
background
property of the generating element.
background
property of the generating element.
within a table:
<table border=1 bgcolor=pink cellpadding=0 cellspacing=0 ><tr><td>
<h5 style=margin: 8px 60px 4px 20px; padding:2em 8em>heading<h5>
heading |
heading |
<STYLE type="text/css"> N.B. px or em... is required (6/22/08 ie doesn't seem to like list) UL { background: green; margin: 8px 60px 4px 20px; /* top,right, bottom, left */ padding: 3px 20px 1px 5px; /* No borders set */ } LI { color:red; /* text color is red */ background: gray; /* Content, padding will be gray */ 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-color: black; border-style: dashed; border-width: medium; } /* sets border width on all sides */ </STYLE> <UL> <LI>First element of list <LI class="withborder">Second element of list is longer to illustrate wrapping. </UL>
|
results in a document tree with (among other relationships) a UL element that has two LI children.
The first of the following diagrams illustrates what this example would produce. The second illustrates the relationship between the margins, padding, and borders of the UL elements and those of its children LI elements.
margin-top
,
margin-right
,
margin-bottom
,
margin-left
, and
margin
Margin properties specify the width of the margin area of a box.
The
margin shorthand property sets
the margin for all four sides.
The properties defined in this section refer to the <margin-width> value type:
<length> | Specifies a fixed width. relative units are: i.fem: font-size ex: x-height px: pixels, |
<percentage> | The percentage is calculated
with respect to the width of the generated boxs
containing block.
This is true for margin-top and margin-bottom ,
except in the page context,
where percentages refer to page box height.
|
auto | See computing widths and margins for behavior.
Negative values for margin properties are allowed, but there may be implementation-specific limits. |
These properties set the top, right, bottom, and left margin of a box.
H1 { margin-top: 2em }
margin
Value: <margin-width>{1,4} | inherit;
Initial: not defined for shorthand properties;
Applies to: all elements;
not Inherited ;
Percentages: refer to width of containing block
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) */ }
In this specification, the expression collapsing margins means that adjoining margins (no padding or border areas separate them) of two or more boxes (may be next to one another or nested) combine to form a single margin.
In CSS2, horizontal margins never collapse.
Vertical margins may collapse between certain boxes:
See examples of margin, padding, and borders for an illustration of collapsed margins.
padding-top
,
padding-right
,
padding-bottom
,
padding-left
, and
padding
The padding properties specify the width of the padding area of a box. The padding shorthand property sets the padding for all four sides
The properties defined in this section refer to the <padding-width>
value type:
<length>
<percentage>
The percentage is calculated with respect to the width of the generated box's containing block, even for
padding-top
and padding-bottom
.
Values for padding values cannot be negative.
Values for padding properties refer to the width of the generated box's containing block.
These properties set the top, right, bottom, and left padding of a box.
BLOCKQUOTE { padding-top: 0.3em }
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 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
i.e:
1 | 1 | 1 | padding: all | |||
2 2 | 2 2 | 4 2 | padding top+bot left+right | |||
1 | 3 | 3 | padding: top left+right bot | |||
padding: top right bot left |
The surface color or image of the padding area is specified via
the background
property:
H1 { background:pink; padding: 4em 8em 16em; } <h1>eXaMple</h1>
<img src=gifs/Garfield.gif style= background:pink; padding: 8em 16em 32em; >
![]() |
The example above specifies a 4em padding-top and an 8em horizontal padding padding-right + padding-left and a 16em padding-bottom) .
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.
HTML user agents may render borders for certain elements (e.g., buttons, menus, etc.) differently than for
"ordinary" elements.
border-top-width, -right-width, -bottom-width, -left-width, border-width
The border width properties specify the width of the border area.
thin
, medium
or thick
<length>
The border's thickness has an explicit positive value.
The interpretation of thin, medium, thick depends on the user agent.
Thin <= medium <= thick. (dugh!)
and must be constant throughout a document.
Value:<border-width>| inherit
Initial:medium; Applies to:all elements ; Not inherited
border-width
Value:<border-width>{1,4} | inherit
Initial:see individual properties; Applies to:all elements ; Not inherited
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 */IE doesn't do it
H1 { border-width: thin thick } /* thin thick thin thick */ H1 { border-width: thin thick medium } /* thin thick medium thick */
border-top-color, border-right-color, border-bottom-color, border-left-color and border-color
The border color properties specify the color of a box's border.
Initial:the value of the 'color' property; Applies to:all elements ; Not inherited
border-color
Value:<color>{1,4} | transparent | inherit
Initial:see individual properties; Applies to:all elements ; Not inherited
The border-color property sets the color of the four borders.
transparent
The border is transparent (though it may have width).
The border-color property 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; }
<img src=gifs/Garfield.gif style=border-width:3;>
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,
none | No border. This value forces the computed value of border-width to be '0'. | ||||||||
hidden | Same as none except in terms of border conflict
resolution for table elements.
| ||||||||
dotted | a series of dots. | ||||||||
dashed | a series of short line segments. | ||||||||
solid | a single line segment. | ||||||||
double | two solid lines. The sum of the two lines and the space between them
equals the value of border-width (i.e. border-width must be greater than 3)
<img src=gifs/Garfield.gif style=border-width:3;border-style:double;> ![]() groove
| looks as though it were carved into the canvas.
| ridge
| The opposite of | grove the border looks as though it were coming out of the canvas.
inset
| entire box look as though it were embedded in the canvas.
| outset
| The opposite of | inset the border makes the entire box look as though it were coming out of the canvas.
|
All borders are drawn on top of the boxs background.
The color of
borders drawn for values of groove
, ridge
, inset
, and outset
depends on the elements color
property.
Conforming HTML user agents may
interpret dotted
, dashed
, double
, groove
, ridge
, inset
, and outset
to be solid
.
border-bottom-style
, border-left-style
Value:<border-style> | inherit
Initial:none; Applies to:all elements; Not inherited
border-style
Initial:see individual properties; Applies to:all elements; Not inherited
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.
border-top
,
border-bottom
,
border-right
,
border-left
, and
border
border-bottom
, border-left
Value:[ <border-top-width> || <border-style> || <color> ] | inherit
Initial:see individual properties; Applies to:all elements ; Not inherited
This is a shorthand property for setting the width, style, and color of the top, right, bottom, and left border of a box.
td { border-bottom: thick solid red }
border-bottom: thick solid red |
The above rule will set the width, style, and color of the border
below the element. Omitted values are set to
their initial values. Since
the following rule does not specify a border color, the border will
have the color specified by the color
property:
H1 { border-bottom: thick solid }
border
Value:[ <border-width> || <border-style> || <color> ] | inherit
Initial:see individual properties; Applies to:all elements ; Not inherited
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.
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 some properties have overlapping functionality,
the order in which the rules are specified IS important.
and den da maser sayd
In the above example, the color of the left border SHOULD BE 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, itborder-left-color
will be taken from the color
property. The fact that the color
property is set after the
border-left property is not relevant.