Margins

MamtaWardhani's avatar
Published Aug 8, 2021Updated Apr 28, 2025
Contribute to Docs

Margins in CSS create space around elements, separating them from others and the webpage edges. They help organize layout and improve readability.

Margins are an essential part of the CSS Box Model, which defines how elements are sized, positioned, and rendered on a webpage. Unlike padding (which creates space inside an element), margins create space outside the element’s border. They are extensively used in responsive web design to control spacing between components, create visual hierarchy, and improve overall layout aesthetics.

Syntax

selector {
  margin: value;
}

Parameters:

The margin property can accept from one to four values, which determine the margin size for different sides of an element:

  • one value: Applies the same margin to all four sides
  • two values: First value applies to top and bottom, second value applies to left and right
  • three values: First value applies to top, second value applies to left and right, third value applies to bottom
  • four values: Values apply to top, right, bottom, and left sides respectively (clockwise from top)

The margin property is a shorthand for the following CSS properties:

  • margin-top
  • margin-right
  • margin-bottom
  • margin-left

Return value:

The margin property doesn’t return a value. It simply alters the layout by applying spacing.

Margin Properties Values

Margin Property Description Example
margin-top Sets the top margin of an element margin-top: 20px;
margin-right Sets the right margin of an element margin-right: 15px;
margin-bottom Specifies the margin at the bottom of an element margin-bottom: 30px;
margin-left Determines the width of the margin on the left side margin-left: 10px;
margin Shorthand property that sets all four margin properties in one declaration margin: 10px 20px 15px 5px;

Example 1: Setting Basic Margins

This example demonstrates how to set basic margins around an HTML element using the margin shorthand property.

/* CSS code to set margins around a div element */
.box {
width: 300px;
height: 200px;
background-color: #f0f0f0;
border: 1px solid #333;
/* Apply a 20px margin to all four sides */
margin: 20px;
}

This creates a box with a 20-pixel margin on all sides, creating space between the box and any adjacent elements. The margin is applied outside the element’s border, pushing away neighboring elements.

The output generated will look like:

The element has 20 pixels of space on all sides, separating it from other elements on the page.

Example 2: Creating Asymmetrical Margins

This example shows how to create different margin sizes for each side of an element, allowing for precise layout control.

/* CSS code to set different margins for each side */
.content-box {
width: 400px;
background-color: #e0e0e0;
border: 1px solid #999;
/* top: 40px, right: 100px, bottom: 120px, left: 80px */
margin: 40px 100px 120px 80px;
/* Alternative approach using individual properties */
/*
margin-top: 40px;
margin-right: 100px;
margin-bottom: 120px;
margin-left: 80px;
*/
}

This code sets different margins for each side of the element, creating an asymmetrical layout. The top margin is 40px, right margin is 100px, bottom margin is 120px, and left margin is 80px. This approach is useful for creating visual hierarchy or when elements need different spacing on each side.

The output of this code will be:

The element has varying amounts of space on each side, creating an asymmetrical layout.

Example 3: Centering Elements with Margins

This example demonstrates how to center an element horizontally using the margin property with the auto value.

/* CSS code to center an element horizontally */
.centered-box {
width: 50%; /* Set a width (required for centering) */
background-color: #d5e8d4;
border: 1px solid #82b366;
padding: 20px;
/* Set left and right margins to auto for horizontal centering */
margin-left: auto;
margin-right: auto;
/* Shorthand version */
/* margin: 0 auto; */
}

Using margin: 0 auto; or setting both margin-left and margin-right to auto makes the browser automatically calculate equal margins on both sides, effectively centering the element horizontally within its parent container. This is one of the most common uses of margins in responsive layouts.

The output of this code will be:

The element will be horizontally centered within its parent container, with equal margins on both left and right sides.

Frequently Asked Questions

1. What does margin 10px, 5px, 15px, 20px mean?

When you specify margin: 10px 5px 15px 20px;, you’re setting different margin values for each side of the element in a clockwise direction starting from the top:

  • Top margin: 10px
  • Right margin: 5px
  • Bottom margin: 15px
  • Left margin: 20px

This syntax allows you to control the spacing around each side of an element with a single declaration.

2. What is CSS padding?

While margins create space outside an element’s border, padding creates space inside the element between its content and border. Padding affects the internal spacing of an element, while margins affect the external spacing between elements. Both are part of the CSS Box Model but serve different layout purposes.

3. What is margin right in CSS?

margin-right is a CSS property that specifically sets the right margin of an element. It creates space between the right border of the element and adjacent elements. For example, margin-right: 20px; adds 20 pixels of space to the right side of an element.

4. What is the syntax for margin?

The margin property can be written in several ways:

  • Single value: margin: 10px; (all sides)
  • Two values: margin: 10px 20px; (top/bottom, left/right)
  • Three values: margin: 10px 20px 15px; (top, left/right, bottom)
  • Four values: margin: 10px 20px 15px 5px; (top, right, bottom, left)

You can also set individual margins using the specific properties: margin-top, margin-right, margin-bottom, and margin-left.

5. What is box sizing in CSS?

Box sizing in CSS determines how the total width and height of an element is calculated. The default value, box-sizing: content-box;, means that padding and border are added to the element’s specified width and height. Using box-sizing: border-box; includes padding and border in the element’s width and height, making it easier to create predictable layouts. Margins are always outside the box model and are not affected by the box-sizing property.

Margins

margin
Shorthand property for margin-top, margin-bottom, margin-left and margin-right to create space around an element or for each side individually.
margin-bottom
Defines the bottom margin area for an element.
margin-left
Defines the left margin area for an element.
margin-right
Defines the right margin area for an element.
margin-top
Defines the top margin area for an element.

All contributors

Contribute to Docs

Learn CSS on Codecademy