CSS margin Property
Description
The margin property defines the space outside an element’s border and controls how far that element sits from neighboring boxes and the edges of its container. It is a fundamental part of the CSS box model alongside padding and border, and unlike padding or border it creates empty exterior space rather than increasing the element’s painted interior. Because margins are external to the box, they affect layout by pushing other elements away rather than altering an element’s internal content area or background.
Margins behave differently depending on the formatting context: block-level and inline-level boxes treat margins in different ways, and the element’s display type determines much of that behavior (display). Some margin interactions are non-intuitive — for example, vertical margins between certain block boxes can combine in collapsing scenarios, while margins on floated or absolutely positioned elements interact with surrounding content differently. Floated elements shift and influence text flow, and positioned elements are taken out of normal flow, so margin effects vary with how an element participates in layout (float, position).
Developers commonly use margins as a lightweight spacing tool to create gutters, separate components, or nudge elements relative to one another; their external nature makes them well suited for creating whitespace without altering an element’s internal measurements. That said, how margins influence the final rendered layout also depends on related box-sizing and clipping behavior — an element’s sizing model and how overflow is handled can change what parts of the layout are visible or hidden when margins push content around (box-sizing, overflow). Understanding how margins collapse, propagate, and interact with different layout contexts is important for predictable, maintainable spacing in CSS.
Definition
- Initial value
- See individual properties
- Applies to
- All elements
- Inherited
- No
- Computed value
- See individual properties
- Animatable
- Yes
- JavaScript syntax
- object.style.margin
Interactive Demo
Syntax
margin: [ <length> | <percentage> | auto ]{1,4}
Values
- <length>Specifies a fixed width. Negative Values are allowed.
- <percentage>A percentage relative to the width of the containing block. Negative values are allowed.
- autoauto is replaced by some suitable value, e.g. it can be used for centering of blocks.
Can have between 1 to 4 values:
1 value, all 4 sides have the same margin.
2 values, the top and bottom margins are set to the first value and the right and left margins are set to the second.
3 values, the top is set to the first value, the left and right are set to the second, and the bottom is set to the third.
4 values, they apply to the top, right, bottom, and left, respectively.
Example
Browser Support
The following information will show you the current browser support for the CSS margin property. Hover over a browser icon to see the version that first introduced support for this CSS property.
This property is supported by all modern browsers.
Desktop
Tablets & Mobile
Last updated by CSSPortal on: 1st January 2026
