Movatterモバイル変換


[0]ホーム

URL:


previous  next  contents  properties  index  


8Box model

Contents

Note: Several sections of this specification have been updated by other specifications. Please, see"Cascading Style Sheets (CSS) — The Official Definition" in the latestCSS Snapshot for a list of specifications and the sections they replace.

The CSS Working Group is also developingCSS level 2 revision 2 (CSS 2.2).

The CSS box model describes the rectangular boxes that aregenerated for elements in thedocumenttree and laid out according to thevisual formattingmodel.

8.1Box dimensions

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:

Image illustrating the relationship between content, padding, borders, and margins.   [D]

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:

content edgeorinner edge
The content edge surrounds the rectangle given by thewidth andheightof the box, which often depend on the element'srendered content.The four content edges define thebox'scontent box.
padding edge
The padding edge surrounds the box padding. If the paddinghas 0 width, the padding edge is the same as the content edge.The four padding edges define thebox'spadding box.
border edge
The border edge surrounds the box's border. If the borderhas 0 width, the border edge is the same as the padding edge.The four border edges define the box'sborderbox.
margin edgeorouteredge
The margin edge surrounds the box margin. If the marginhas 0 width, the margin edge is the same as the border edge.The four margin edges define the box'smarginbox.

Each edge may be broken down into a top, right, bottom, and leftedge.

The dimensions of the content area of a box — thecontent width andcontent height — depend on several factors: whether the element generatingthe box has the'width'or'height' propertyset, whether the box contains text or other boxes, whether thebox is a table, etc. Box widths and heights are discussedin the chapter onvisual formattingmodel details.

The background style of the content, padding, and border areas of abox is specified by the'background' property of thegenerating element. Margin backgrounds are always transparent.

8.2Example of margins, padding, and borders

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.)

Image illustrating how parent and child margins, borders,and padding relate.   [D]

Note that:

8.3Margin properties:'margin-top','margin-right','margin-bottom','margin-left', and'margin'

Margin properties specify the width of themargin area of a box. The'margin' shorthand property sets themargin for all four sides while the other margin properties only settheir respective side. These properties apply to all elements, butvertical margins will not have any effect on non-replaced inlineelements.

The properties defined in this section refer to the<margin-width>value type, which may take one of the following values:

<length>
Specifies a fixed width.
<percentage>
The percentage is calculatedwith respect to thewidth of the generated box'scontaining block.Note that this is true for'margin-top' and'margin-bottom' as well.If the containing block's width depends on this element, then theresulting layout is undefined in CSS 2.1.
auto
See the section oncalculating widths and margins for behavior.

Negative values for margin properties are allowed, but there may beimplementation-specific limits.

'margin-top','margin-bottom'
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.

'margin-right','margin-left'
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 }
'margin'
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'margin-top','margin-right','margin-bottom', and'margin-left' at the same place inthe style sheet.

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) */}

8.3.1Collapsing margins

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 tocollapse, and theresulting combined margin is called acollapsed margin.

Adjoining vertical margins collapse, except:

Horizontal margins never collapse.

Two margins areadjoining if and only if:

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 tocollapse through it. In this case,the position of the element depends on its relationship with the otherelements whose margins are being collapsed.

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.

8.4Padding properties:'padding-top','padding-right','padding-bottom','padding-left', and'padding'

The padding properties specify the width of thepadding area of a box. The'padding' shorthand property sets thepadding for all four sides while the other padding properties only settheir respective side.

The properties defined in this section refer to the<padding-width> value type, which may take one of the following values:

<length>
Specifies a fixed width.
<percentage>
The percentage is calculated withrespect to thewidth of the generated box'scontaining block, even for'padding-top' and'padding-bottom'.If the containing block's width depends on this element, then theresulting layout is undefined in CSS 2.1.

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.

'padding-top','padding-right','padding-bottom','padding-left'
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 }
'padding'
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'padding-top','padding-right','padding-bottom', and'padding-left' at the same placein the style sheet.

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 ('padding-top' and'padding-bottom') and a '2em'horizontal padding ('padding-right' and'padding-left'). The 'em' unit isrelative to the element's font size: '1em' is equal to the size of thefont in use.

8.5Border properties

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.

8.5.1Border width:'border-top-width','border-right-width','border-bottom-width','border-left-width', and'border-width'

The border width properties specify the width of theborder area. The propertiesdefined in this section refer to the<border-width>value type, which may take one of the following values:

thin
A thin border.
medium
A medium border.
thick
A thick border.
<length>
The border's thickness has an explicit value. Explicitborder widths cannot be negative.

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.

'border-top-width','border-right-width','border-bottom-width','border-left-width'
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.

'border-width'
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 */

8.5.2Border color:'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.

'border-top-color','border-right-color','border-bottom-color','border-left-color'
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
'border-color'
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:

<color>
Specifies a color value.
transparent
The border is transparent (though it may have width).

The'border-color'property can have from one to four component values, and the valuesare 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 valueof the element's'color' property as thecomputed value for the border color.

Example(s):

In this example, the border will be a solid black line.

p {   color: black;   background: white;   border: solid;}

8.5.3Border style:'border-top-style','border-right-style','border-bottom-style','border-left-style', and'border-style'

The border style properties specify the line style of a box'sborder (solid, double, dashed, etc.). The properties defined in thissection refer to the<border-style>value type, which may take one of the following values:

none
No border; the computed border width is zero.
hidden
Same as 'none', except in terms ofborder conflictresolution fortable elements.
dotted
The border is a series of dots.
dashed
The border is a series of short line segments.
solid
The border is a single line segment.
double
The border is two solid lines. The sum ofthe two lines and the space between themequals the value of'border-width'.
groove
The border looks as though it were carvedinto the canvas.
ridge
The opposite of 'groove': the borderlooks as though it were coming out of the canvas.
inset
The border makes the box look as thoughit were embedded in the canvas.
outset
The opposite of 'inset': theborder makes the box look as thoughit were coming out of the canvas.

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.

'border-top-style','border-right-style','border-bottom-style','border-left-style'
Value:  <border-style> |inherit
Initial:  none
Applies to:  all elements
Inherited:  no
Percentages:  N/A
Media:  visual
Computed value:  as specified
'border-style'
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.

8.5.4Border shorthand properties:'border-top','border-right','border-bottom','border-left', and'border'

'border-top','border-right','border-bottom','border-left'
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 }
'border'
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'margin' and'padding' properties, the'border' property cannot set differentvalues on the four borders. To do so, one or more of the other borderproperties must be used.

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'border-left' setting thewidth, style, and color. Since the color value is not given by the'border-left' property, itwill be taken from the'color'property. The fact that the'color' property is set after the'border-left' property is notrelevant.

8.6The box model for inline elements in bidirectional context

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'direction' property is 'ltr', theleft-most generated box of the first line box in which the elementappears has the left margin, left border and left padding, and theright-most generated box of the last line box in which the elementappears has the right padding, right border and right margin.

When the element's'direction' property is 'rtl', theright-most generated box of the first line box in which the elementappears has the right padding, right border and right margin, and theleft-most generated box of the last line box in which the elementappears has the left margin, left border and left padding.


previous  next  contents  properties  index  


[8]ページ先頭

©2009-2025 Movatter.jp