CSS

底部边距 | margin-bottom

margin-bottom

CSS的 margin-bottom 属性用于设置元素的底部外边距,允许设置负数值。一个正数值将让它相对于正常流与邻近块更远,而负数值将使得更近。

/* <length> values */ margin-bottom: 10px; /* An absolute length */ margin-bottom: 1em; /* relative to the text size */ margin-bottom: 5%; /* relative to the nearest block container's width */ /* Keyword values */ margin-bottom: auto; /* Global values */ margin-bottom: inherit; margin-bottom: initial; margin-bottom: unset;

该属性对于不可替代(non-replaced 元素在规范中有概念,请自行搜索)的行级元素没有效果,比如:<tt>或者<span>.

初始值0
适用元素all elements, except elements with table display types other than table-caption, table and inline-table. It also applies to ::first-letter.
是否是继承属性no
Percentagesrefer to the width of the containing block
适用媒体visual
计算值the percentage as specified or the absolute length
Animation typea length
正规顺序the unique non-ambiguous order defined by the formal grammar

语法

该margin-bottom属性被指定为关键字auto,或<length>或<percentage>。它的值可以是正值,零值或负值。

<length>定义了一个确定的宽度值,参见 <length>以了解更多可能的值。<percentage><percentage>始终与包含该元素的容器宽度有关。auto参见margin。

正式语法

<length> | <percentage> | auto

示例

HTML

<div class="container"> <div class="box0">Box 0</div> <div class="box1">Box 1</div> <div class="box2">Box one's negative margin pulls me up</div> </div>

CSS

CSS的div来设置margin-bottom和height

.box0 {     margin-bottom:1em;     height:3em; } .box1 {     margin-bottom:-1.5em;     height:4em; } .box2 {     border:1px dashed black;     border-width:1px 0;     margin-bottom:2em; }

容器和隔板的一些定义边缘的影响可以更清楚地看到

.container {     background-color:orange;     width:320px;     border:1px solid black; } div {     width:320px;     background-color:gold; }

规范

SpecificationStatusComment
CSS Basic Box ModelThe definition of 'margin-bottom' in that specification.Working DraftNo significant change
CSS TransitionsThe definition of 'margin-bottom' in that specification.Working DraftDefines margin-bottom as animatable.
CSS Level 2 (Revision 1)The definition of 'margin-bottom' in that specification.RecommendationRemoves its effect on inline elements.
CSS Level 1The definition of 'margin-bottom' in that specification.RecommendationInitial definition

Browser compatibility

FeatureChromeEdgeFirefox (Gecko)Internet ExplorerOperaSafari (WebKit)
Basic support1.0(Yes)1.0 (1.7 or earlier)3.03.51.0 (85)
auto value1.0(Yes)1.0 (1.7 or earlier)6.0 (strict mode)3.51.0 (85)

FeatureAndroidEdgeFirefox Mobile (Gecko)IE PhoneOpera MobileSafari Mobile
Basic support1.0(Yes)1.0 (1)6.06.01.0