CSS Margin vs Padding: All Differences Explained! (2023)
Tags: css, webdev, html, beginners
Published: 2023-01-07 09:24:00

Welcome to the ultimate guide to CSS margins and padding! If you're new to web development, you might be wondering, "what even are margins and padding, and why do they matter?" Well, fear not, because I'll break it all down for you in a simple way.

You see, margins and padding are both used to create space around elements on a web page. But, they work in slightly different ways and serve different purposes. Understanding the differences between these two concepts is crucial for creating visually appealing and user-friendly websites.

Throughout this lesson, we'll cover the basics of margins and padding, as well as dive into some more advanced concepts. 

Post Summarised 👇

> Margin creates space outside of an element and is transparent, while padding creates space inside of an element and has a background color.

---

### What exactly are CSS Margins? 

CSS margin is a way to **create space** around elements on a webpage. Imagine you have a puzzle with lots of different pieces. Each piece represents an element on your webpage, like a picture or a paragraph of text. The space around each puzzle piece is like the margin around each element on your webpage.

You can use CSS margin to make some elements on your webpage have more space around them, or to make other elements have less space. For example, if you have a really important picture on your webpage, you might want to give it a lot of margin so that it stands out more. Or, if you have a bunch of paragraphs of text that you want to keep close together, you might want to give them a smaller margin.

There are different properties of margin that you can use in CSS, like `margin-top`, `margin-bottom`, `margin-left`, and `margin-right`. These properties let you control the amount of margin on each side of an element.

Example: 

```css
p {
  margin: 20px;  /* Adds 20 pixels of margin around all sides of the paragraph element */
}

img {
  margin-top: 10px;  /* Adds 10 pixels of margin to the top of the img element */
  margin-bottom: 15px;  /* Adds 15 pixels of margin to the bottom of the img element */
}
```

Now you know the basics and what that exactly is, let's dive into Padding now. 

### What exactly is CSS Padding? 

CSS padding is just like the padding you might put **inside a box** to protect something delicate. On a webpage, padding is the space inside an element, between the element's content and its border.

Padding is a way to make elements on your webpage *bigger* or *smaller*. For example, if you have a button that you want to be really easy to press, you might want to give it a lot of padding so that it's big and easy to hit. Or, if you have a picture that you want to be really small and compact, you might want to give it a smaller amount of padding.

Just like with margin, there are different properties of padding that you can use in CSS, like padding-top, padding-bottom, padding-left, and padding-right. These properties let you control the amount of padding on each side of an element.

Example: 

```css
button {
  padding: 10px;  /* Adds 10 pixels of padding around all sides of the element */
}

img {
  padding-left: 5px;  /* Adds will add 5 pixels of padding to the left side of the element */
  padding-right: 20px;  /* Adds 20 pixels of padding to the right side of the element */
}
```

### The Main Difference

Margin refers to the **space outside** of an element, while padding refers to the **space inside** of an element. They both create space around elements, but they work in different ways.

Margin affects the layout of a page by creating space outside of an element. This space is **transparent**, meaning that any elements behind it will still be visible. Example: 

```css
.element {
  margin: 20px;
}
```

Above, we've applied a margin of 20 pixels to the `.element` class. This will create 20 pixels of space around the element on **all** sides. You can also specify different margin values for each side of the element using the following properties:

-   `margin-top`
-   `margin-right`
-   `margin-bottom`
-   `margin-left`


Padding, on the other hand, creates space inside of an element. This space is **not transparent**, meaning that it will have the same background color as the element itself. Example: 

```css
.element {
  padding: 20px;
}
```

Above we've applied a padding of 20 pixels to the `.element` class. This will create 20 pixels of space around the content inside the element on all sides. Just like with margin, you can also specify different padding values for each side of the element using the following properties:

-   `padding-top`
-   `padding-right`
-   `padding-bottom`
-   `padding-left`


It's important to note that margins and padding can be applied to any element on a web page, including text, images, and even the body element itself. They can be used to create a wide range of layouts, from simple and clean to more complex and visually striking.

---

### How to use margin and padding together in CSS? 

Margin and padding are two ways to create space around elements in CSS.  They can be used together to create complex and visually appealing layouts on web pages.

One common way to use margin and padding together is to create a **multi-column layout**. For example, let's say you want to create a three-column layout with equal-width columns and some space between the columns. Let's see how you can use margin and padding to do that? 

```css
.column {
  width: 33%;
  float: left;
  padding: 20px;
  box-sizing: border-box;
}

.column + .column {
  margin-left: 20px;
}

```

Above we've applied a padding of 20 pixels to the `.column` class and a margin of 20 pixels to the `.column + .column` selector. This will create 20 pixels of space inside each column and 20 pixels of space between each column.

Another way to use margin and padding together is to create a CTA button with some space around it. Example: 

```css
.button {
  display: inline-block;
  padding: 10px 20px;
  margin: 20px;
  background-color: #ff0000;
  color: #ffffff;
  font-weight: bold;
  border-radius: 5px;
}
```


Above we've applied a padding of 10 pixels on the top and bottom and 20 pixels on the left and right to the `.button` class, and a margin of 20 pixels on all sides. This will create some space around the button and make it stand out from the rest of the content on the page.

It's important to note that margins and padding can be applied to any element on a web page, and they can be used in a variety of different ways to create different effects. Experimenting with different combinations of margin and padding can help you create the desired layout for your web page.

### Takeway

Margin and padding are both important tools in web development and are often used together to create visually appealing layouts on web pages. Understanding the differences between these two properties is crucial in order to effectively use them in your projects. 

Extra Resources to learn: 

- [HTML Unleashed](https://www.rahul.biz/blog/html-unleashed)
- [How to Set CSS Margins and Padding (And Cool Layout Tricks)](https://www.sitepoint.com/set-css-margins-padding-cool-layout-tricks/)

Thank you for Reading | Share and Help others.