CSS margin Property
Sets the margin area on all four sides of an element creating space outside the border
Syntax
margin: <length> | <percentage> | auto;Values
| Value | Description |
|---|---|
| 0 | No margin |
| auto | Browser selects appropriate margin, used for centering |
| <length> | Fixed margin (1-4 values for each side) |
| <percentage> | Percentage of containing block width |
Example
.centered {
max-width: 800px;
margin: 0 auto;
}Understanding CSS margin
The CSS margin property sets the margin area on all four sides of an element creating space outside the border. As part of the Box Model module in CSS, it is one of the most commonly used properties for controlling the visual presentation of web pages.
You can set margin to values such as 0, auto, <length>, <percentage>, among others. Each value changes how the browser renders the affected element, giving you fine-grained control over your page layout and design. Choosing the right value depends on the specific design requirements of your project.
Common use cases for the margin property include responsive web design, component-based layouts, and creating visually consistent interfaces across devices. It works closely with related properties like margin-top, margin-right, margin-bottom to achieve complex styling effects. Understanding how these properties interact helps you write cleaner, more maintainable stylesheets.
Browser support for margin is excellent across all modern browsers including Chrome, Firefox, Safari, and Edge. For older browsers, consider using fallback values or progressive enhancement strategies. The property can also be set dynamically via JavaScript using element.style.margin or the CSS custom properties (variables) approach for theming.
Related Properties
Explore More CSS Properties
Browse our complete reference of 251 CSS properties with syntax, examples, and tips.