--- title: margin-top slug: Web/CSS/margin-top translation_of: Web/CSS/margin-top ---
margin-top 属性用来设置元素的顶部外边距,你也可以使用负值。
这个属性对于不可替换的inline元素没有效果,比如 {{HTMLElement("tt")}} 或者 {{HTMLElement("span")}}.
{{cssinfo}}
/* <length> values */ margin-top: 10px; /* 绝对长度 */ margin-top: 1em; /*相对于字体大小 */ margin-top: 5%; /*相对于最相邻的父级元素块(block)的宽度*/ /* Keyword values */ margin-top: auto; /* Global values */ margin-top: inherit; margin-top: initial; margin-top: unset;
<length><percentage>auto.content { margin-top: 5%; }
.sidebox { margin-top: 10px; }
.logo { margin-top: -5px; }
#footer { margin-top: 1em; }
| Specification | Status | Comment |
|---|---|---|
| {{SpecName('CSS3 Box', '#the-margin', 'margin-top')}} | {{Spec2('CSS3 Box')}} | 无改动 |
| {{SpecName('CSS3 Transitions', '#animatable-css', 'margin-top')}} | {{Spec2('CSS3 Transitions')}} | 定义 margin-top 为可动画的属性. |
| {{SpecName('CSS2.1', 'box.html#margin-properties', 'margin-top')}} | {{Spec2('CSS2.1')}} | 去除了在inline元素的效果。 |
| {{SpecName('CSS1', '#margin-top', 'margin-top')}} | {{Spec2('CSS1')}} | 最初定义了该属性 |