Skip to content

[Edit] Css: margins #6637

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
163 changes: 153 additions & 10 deletions content/css/concepts/margins/margins.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,171 @@
---
Title: 'Margins'
Description: 'Margin refers to the space directly outside of the box. The margin property is used to specify the size of this space. css p { border: 1px solid aquamarine; margin: 20px; } '
Description: 'Creates space around an element, separating it from neighboring elements.'
Subjects:
- 'Web Development'
- 'Web Design'
- 'Web Development'
Tags:
- 'Margin'
- 'Box Model'
- 'Properties'
- 'Documentation'
- 'CSS'
- 'Margin'
- 'Positioning'
CatalogContent:
- 'learn-css'
- 'paths/front-end-engineer-career-path'
---

Margin refers to the space directly outside of the box. The margin property is used to specify the size of this space.
**Margins** in CSS are used to create space around an element, separating it from neighboring elements and the edges of the webpage. They control the layout by adjusting the distance between elements, providing better organization and 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

```pseudo
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
p {
border: 1px solid aquamarine;
/* 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;
}
```

Here, the code will place 20 pixels of space on the outside of the paragraph’s box on all four sides. This means that other HTML elements on the page cannot come within 20 pixels of the paragraph’s border.
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.](https://raw.githubusercontent.com/Codecademy/docs/main/media/margins-output-1.png)

## 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
/* 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.](https://raw.githubusercontent.com/Codecademy/docs/main/media/margins-output-2.png)

## Example 3: Centering Elements with Margins

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

```css
/* 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.](https://raw.githubusercontent.com/Codecademy/docs/main/media/margins-output-3.png)

## 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?

If you want to be even more specific about the amount of margin on each side of a box, you can use the following properties:
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.
Binary file added media/margins-output-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added media/margins-output-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added media/margins-output-3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.