📄 box.html
字号:
<P>Margin properties specify the width of the <ahref="#box-margin-area">margin area</a> of a box. The<a href="box.html#propdef-margin" class="noxref"><span class="propinst-margin">'margin'</span></a> shorthand property setsthe margin for all four sides while the other margin properties onlyset their respective side. <P>The properties defined in this section refer to the <spanclass="index-def" title="<margin-width>::definition of"><aname="value-def-margin-width"><strong><margin-width></strong></a></span>value type, which may take one of the following values:<dl><dt><a href="syndata.html#value-def-length" class="noxref"><span class="value-inst-length"><strong><length></strong></span></a><dd>Specifies a fixed width.<dt><a href="syndata.html#value-def-percentage" class="noxref"><span class="value-inst-percentage"><strong><percentage></strong></span></a><dd>The percentage is calculatedwith respect to the <em>width</em> of the generated box's <a href="visuren.html#containing-block">containing block</a>.This is true for <a href="box.html#propdef-margin-top" class="noxref"><spanclass="propinst-margin-top">'margin-top'</span></a> and <a href="box.html#propdef-margin-bottom" class="noxref"><spanclass="propinst-margin-bottom">'margin-bottom'</span></a>, except in the <a href="page.html#page-context">page context</a>,where percentages refer to page box height.<dt><strong>auto</strong><dd>See the section on <ahref="visudet.html#Computing_widths_and_margins">computing widths and margins</a> for behavior.</dl><P>Negative values for margin properties are allowed, but there may beimplementation-specific limits. <div class="propdef"><dl><dt><span class="index-def" title="'margin-top'"><a name="propdef-margin-top" class="propdef-title"><strong>'margin-top'</strong></a></span>, <span class="index-def" title="'margin-right'"><a name="propdef-margin-right" class="propdef-title"><strong>'margin-right'</strong></a></span>, <span class="index-def" title="'margin-bottom'"><a name="propdef-margin-bottom" class="propdef-title"><strong>'margin-bottom'</strong></a></span>, <span class="index-def" title="'margin-left'"><a name="propdef-margin-left" class="propdef-title"><strong>'margin-left'</strong></a></span><dd><table class="propinfo" cellspacing=0 cellpadding=0><tr valign=baseline><td><em>Value:</em> <td><a href="box.html#value-def-margin-width" class="noxref"><span class="value-inst-margin-width"><margin-width></span></a> | <a href="cascade.html#value-def-inherit" class="noxref"><span class="value-inst-inherit">inherit</span></a><tr valign=baseline><td><em>Initial:</em> <td>0<tr valign=baseline><td><em>Applies to:</em> <td>all elements<tr valign=baseline><td><em>Inherited:</em> <td>no<tr valign=baseline><td><em>Percentages:</em> <td>refer to width ofcontaining block<tr valign=baseline><td><em>Media:</em> <td><a href="media.html#visual-media-group" class="noxref">visual</a></table></dl></div><P>These properties set the top, right, bottom, and left margin of abox.<div class="example"><P style="display:none">Example(s):</P><P><PRE>H1 { margin-top: 2em }</PRE></div><div class="propdef"><dl><dt><span class="index-def" title="'margin'"><a name="propdef-margin" class="propdef-title"><strong>'margin'</strong></a></span><dd><table class="propinfo" cellspacing=0 cellpadding=0><tr valign=baseline><td><em>Value:</em> <td><a href="box.html#value-def-margin-width" class="noxref"><span class="value-inst-margin-width"><margin-width></span></a>{1,4} | <a href="cascade.html#value-def-inherit" class="noxref"><span class="value-inst-inherit">inherit</span></a><tr valign=baseline><td><em>Initial:</em> <td>not defined for shorthand properties<tr valign=baseline><td><em>Applies to:</em> <td>all elements<tr valign=baseline><td><em>Inherited:</em> <td>no<tr valign=baseline><td><em>Percentages:</em> <td>refer to width of containing block<tr valign=baseline><td><em>Media:</em> <td><a href="media.html#visual-media-group" class="noxref">visual</a></table></dl></div><P> The <a href="box.html#propdef-margin" class="noxref"><span class="propinst-margin">'margin'</span></a> property is ashorthand property for setting <a href="box.html#propdef-margin-top" class="noxref"><spanclass="propinst-margin-top">'margin-top'</span></a>, <a href="box.html#propdef-margin-right" class="noxref"><spanclass="propinst-margin-right">'margin-right'</span></a>, <a href="box.html#propdef-margin-bottom" class="noxref"><spanclass="propinst-margin-bottom">'margin-bottom'</span></a>, and <a href="box.html#propdef-margin-left" class="noxref"><spanclass="propinst-margin-left">'margin-left'</span></a> at the same place inthe style sheet.<P> If there is only one 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.<div class="example"><P style="display:none">Example(s):</P><P><PRE>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 */</PRE><P> The last rule of the example above is equivalent to the examplebelow:<PRE>BODY { margin-top: 1em; margin-right: 2em; margin-bottom: 3em; margin-left: 2em; /* copied from opposite side (right) */}</PRE></div><H3>8.3.1 <a name="collapsing-margins">Collapsing margins</a></H3><P>In this specification, the expression <a name="x24"><span class="index-def"title="collapsing margin"><dfn>collapsing margins</dfn></span></a> meansthat adjoining margins (no padding or border areas separate them) oftwo or more boxes (which may be next to one another or nested) combineto form a single margin.<P>In CSS2, <a name="x25"><span class="index-inst" title="horizontalmargin|margin::horizontal">horizontal margins</span></a> never collapse.<P><a name="x27"><span class="index-inst" title="vertical margin|margin::vertical">Vertical margins</span></a> may collapse between certain boxes:</p><ul><li>Two or more adjoining vertical margins of <ahref="visuren.html#block-box">block</a> boxes in the <ahref="visuren.html#normal-flow">normal flow</a> collapse. Theresulting margin width is the maximum of the adjoining margin widths.In the case of negative margins, the absolute maximum of the negativeadjoining margins is deducted from the maximum of the positiveadjoining margins. If there are no positive margins, the absolutemaximum of the negative adjoining margins is deducted from zero.<li>Vertical margins between a <ahref="visuren.html#floats">floated</a> box and any other box do notcollapse.<li>Margins of <ahref="visuren.html#absolutely-positioned">absolutely</a> andrelatively positioned boxes do not collapse.</ul><P>Pleaseconsult the <a href="#mpb-examples">examples of margin, padding, andborders</a> for an illustration of collapsed margins.<H2>8.4 <a name="padding-properties">Padding properties</a>:<a href="box.html#propdef-padding-top" class="noxref"><span class="propinst-padding-top">'padding-top'</span></a>,<a href="box.html#propdef-padding-right" class="noxref"><span class="propinst-padding-right">'padding-right'</span></a>,<a href="box.html#propdef-padding-bottom" class="noxref"><span class="propinst-padding-bottom">'padding-bottom'</span></a>,<a href="box.html#propdef-padding-left" class="noxref"><span class="propinst-padding-left">'padding-left'</span></a>, and<a href="box.html#propdef-padding" class="noxref"><span class="propinst-padding">'padding'</span></a></H2> <P>The padding properties specify the width of the <ahref="#box-padding-area">padding area</a> of a box. The <a href="box.html#propdef-padding" class="noxref"><spanclass="propinst-padding">'padding'</span></a> shorthand property sets thepadding for all four sides while the other padding properties only settheir respective side. <P>The properties defined in this section refer to the <spanclass="index-def" title="<padding-width>::definition of"><aname="value-def-padding-width"><strong><padding-width></strong></a></span> value type, which may take one of the following values:</p><dl><dt><a href="syndata.html#value-def-length" class="noxref"><span class="value-inst-length"><strong><length></strong></span></a><dd>Specifies a fixed width.<dt><a href="syndata.html#value-def-percentage" class="noxref"><span class="value-inst-percentage"><strong><percentage></strong></span></a><dd>The percentage is calculated withrespect to the <em>width</em> of the generated box's <ahref="visuren.html#containing-block">containing block</a>, even for<a href="box.html#propdef-padding-top" class="noxref"><span class="propinst-padding-top">'padding-top'</span></a> and <a href="box.html#propdef-padding-bottom" class="noxref"><spanclass="propinst-padding-bottom">'padding-bottom'</span></a>.</dl><P>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.<div class="propdef"><dl><dt><span class="index-def" title="'padding-top'"><a name="propdef-padding-top" class="propdef-title"><strong>'padding-top'</strong></a></span>, <span class="index-def" title="'padding-right'"><a name="propdef-padding-right" class="propdef-title"><strong>'padding-right'</strong></a></span>, <span class="index-def" title="'padding-bottom'"><a name="propdef-padding-bottom" class="propdef-title"><strong>'padding-bottom'</strong></a></span>, <span class="index-def" title="'padding-left'"><a name="propdef-padding-left" class="propdef-title"><strong>'padding-left'</strong></a></span><dd><table class="propinfo" cellspacing=0 cellpadding=0><tr valign=baseline><td><em>Value:</em> <td><a href="box.html#value-def-padding-width" class="noxref"><span class="value-inst-padding-width"><padding-width></span></a> | <a href="cascade.html#value-def-inherit" class="noxref"><span class="value-inst-inherit">inherit</span></a><tr valign=baseline><td><em>Initial:</em> <td>0<tr valign=baseline><td><em>Applies to:</em> <td>all elements<tr valign=baseline><td><em>Inherited:</em> <td>no<tr valign=baseline><td><em>Percentages:</em> <td>refer to width of containing block<tr valign=baseline><td><em>Media:</em> <td><a href="media.html#visual-media-group" class="noxref">visual</a></table></dl></div><P>These properties set the top, right, bottom, and left padding ofa box.<div class="example"><P style="display:none">Example(s):</P><P><PRE>BLOCKQUOTE { padding-top: 0.3em }</PRE></div><div class="propdef"><dl><dt><span class="index-def" title="'padding'"><a name="propdef-padding" class="propdef-title"><strong>'padding'</strong></a></span><dd><table class="propinfo" cellspacing=0 cellpadding=0><tr valign=baseline><td><em>Value:</em> <td><a href="box.html#value-def-padding-width" class="noxref"><span class="value-inst-padding-width"><padding-width></span></a>{1,4} | <a href="cascade.html#value-def-inherit" class="noxref"><span class="value-inst-inherit">inherit</span></a><tr valign=baseline><td><em>Initial:</em> <td>not defined for shorthand properties<tr valign=baseline><td><em>Applies to:</em> <td>all elements<tr valign=baseline><td><em>Inherited:</em> <td>no<tr valign=baseline><td><em>Percentages:</em> <td>refer to width of containing block<tr valign=baseline><td><em>Media:</em> <td><a href="media.html#visual-media-group" class="noxref">visual</a></table></dl></div><P> The <a href="box.html#propdef-padding" class="noxref"><span class="propinst-padding">'padding'</span></a> property is ashorthand property for setting <a href="box.html#propdef-padding-top" class="noxref"><spanclass="propinst-padding-top">'padding-top'</span></a>, <a href="box.html#propdef-padding-right" class="noxref"><span
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -