Description & Usage

The margin property creates empty transparent space around an element, separating it from neighboring elements outside its border.

Setting margin: 0 auto on a block element with a fixed or maximum width is a classic technique for horizontally centering containers on web pages.

Adjacent vertical margins on block elements exhibit margin collapsing, where neighboring margins combine into a single margin equal to the larger of the two values.

Property Specifications

Property FeatureSpecification
CategoryBox Model
Initial Value0
InheritedNo (not inherited)
Applies ToAll elements except table display types
Box Model Explorer
box-sizing:
margin: 20px
border: 4px
padding: 20px
Content (180px × 90px)
Rendered Element Size:180px × 90px
Fixed total width of 180px (Content auto-shrinks inside border)

Syntax

margin: 10px | 10px 20px | 10px 20px 30px 40px | auto;

Property Values

ValueDescription
10pxApplies 10px margin to top, right, bottom, and left sides.
10px 20px10px top/bottom, 20px left/right.
0 autoHorizontally centers a fixed-width block element inside its parent.

Code Examples

Horizontal Page Centering
.main-container { max-width: 1200px; margin: 0 auto; }
Centers block container horizontally on large screens.