margin CSS property sets the margin area on all four sides of an element. It is a 简写 for margin-top , margin-right , margin-bottom ,和 margin-left .

The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.

The top and bottom margins have no effect on non- replaced inline elements, such as <span> or <code> .

注意: Margins create extra space around an element. In contrast, padding creates extra space within an element.

句法

/* Apply to all four sides */
margin: 1em;
margin: -3px;
/* vertical | horizontal */
margin: 5% auto;
/* top | horizontal | bottom */
margin: 1em auto 2em;
/* top | right | bottom | left */
margin: 2px 1em 0 auto;
/* Global values */
margin: inherit;
margin: initial;
margin: unset;
					

margin property may be specified using one, two, three, or four values. Each value is a <length> , a <percentage> , or the keyword auto . Negative values draw the element closer to its neighbors than it would be by default.

  • When one value is specified, it applies the same margin to all four sides .
  • When two values are specified, the first margin applies to the top and bottom , the second to the left and right .
  • When three values are specified, the first margin applies to the top , the second to the right and left , the third to the bottom .
  • When four values are specified, the margins apply to the top , right , bottom ,和 left in that order (clockwise).

<length>

The size of the margin as a fixed value.

<percentage>
The size of the margin as a percentage, relative to the width of the 包含块 .
auto

The browser selects a suitable margin to use. For example, in certain cases this value can be used to center an element.

形式句法

[ <length> | <percentage> | auto ]{1,4}
					

范例

Simple example

HTML

<div class="center">This element is centered.</div>
<div class="outside">This element is positioned outside of its container.</div>
					

CSS

.center {
  margin: auto;
  background: lime;
  width: 66%;
}
.outside {
  margin: 3rem 0 0 -3rem;
  background: cyan;
  width: 66%;
}
					

More examples

margin: 5%;                 /* All sides: 5% margin */
margin: 10px;               /* All sides: 10px margin */
margin: 1.6em 20px;         /* top and bottom: 1.6em margin */
                            /* left and right: 20px margin  */
margin: 10px 3% -1em;       /* top:            10px margin */
                            /* left and right: 3% margin   */
                            /* bottom:         -1em margin */
margin: 10px 3px 30px 5px;  /* top:    10px margin */
                            /* right:  3px margin  */
                            /* bottom: 30px margin */
                            /* left:   5px margin  */
margin: 2em auto;           /* top and bottom: 2em margin   */
                            /* Box is horizontally centered */
margin: auto;               /* top and bottom: 0 margin     */
                            /* Box is horizontally centered */
					

注意事项

Horizontal centering

To center something horizontally in modern browsers, you can use display : flex; justify-content : center; .

However, in older browsers like IE8-9 that do not support Flexible Box Layout, these are not available. In order to center an element inside its parent, use margin: 0 auto; .

Margin collapsing

Elements' top and bottom margins are sometimes collapsed into a single margin that is equal to the larger of the two margins. See Mastering margin collapsing 了解更多信息。

规范

规范 状态 注释
CSS 基本 Box 模型
The definition of 'margin' in that specification.
工作草案 No significant change.
CSS Level 2 (Revision 1)
The definition of 'margin' in that specification.
推荐 Removes the effect of top and bottom margins on inline elements.
CSS Level 1
The definition of 'margin' in that specification.
推荐 初始定义。
初始值 as each of the properties of the shorthand:
适用于 all elements, except elements with table display types other than table-caption , table and inline-table . It also applies to ::first-letter and ::first-line .
继承 no
百分比 refer to the width of the containing block
计算值 as each of the properties of the shorthand:
动画类型 a length

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
margin Chrome 完整支持 1 Edge 完整支持 12 Firefox 完整支持 1 IE 完整支持 3 Opera 完整支持 3.5 Safari 完整支持 1 WebView Android 完整支持 1 Chrome Android 完整支持 18 Firefox Android 完整支持 4 Opera Android 完整支持 10.1 Safari iOS 完整支持 1 Samsung Internet Android 完整支持 1.0
auto Chrome 完整支持 1 Edge 完整支持 12 注意事项
完整支持 12 注意事项
注意事项 auto value is not supported in quirks mode.
Firefox 完整支持 1 IE 完整支持 6 注意事项
完整支持 6 注意事项
注意事项 auto value is not supported in quirks mode.
Opera 完整支持 3.5 Safari 完整支持 1 WebView Android 完整支持 37 Chrome Android 完整支持 18 Firefox Android 完整支持 4 Opera Android 完整支持 14 Safari iOS 完整支持 1 Samsung Internet Android 完整支持 1.0

图例

完整支持

完整支持

见实现注意事项。

见实现注意事项。

另请参阅

元数据

  • 最后修改: