Tailwind CSS for Dummies: Margins and Paddings
Tags: css, beginners, tutorial
Published: 2021-03-25 12:35:53

---
title: Tailwind CSS for Dummies: Margins and Paddings
published: true
description: 
tags: css, beginner, tutorial
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qg9khes9z5yqice9njqh.jpg
series: Tailwind CSS for Dummies
---
Follow me on Twitter: <a href="https://twitter.com/justericchapman?ref_src=twsrc%5Etfw" class="twitter-follow-button" data-show-count="false">Follow @justericchapman</a><script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>

<h2>Spacing</h2>
Tailwind spacing utilities allow to specified space between elements. The space could be outside an element border (margin) or inside an element border (padding). 

Look at the box the see the difference between margins and paddings:
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/segc4rsc69p729iexhw1.png) 

<h2>Margins</h2>
To set a margin all around the element (on all side), we need to use the margin utilities. The syntax is m-{number} The specified number is not a measurement unit. It only a Tailwind CSS number that reference a margin space. The bigger the number, the bigger the space. Here are some example:
```html
<div class="m-0">  margin: 0px;
<div class="m-1">  margin: 0.25rem;
<div class="m-4">  margin: 1rem; 
<div class="m-96"> margin: 24rem;
```
If we only want a margin for one side. We need to specify the side the margin will apply (top, right, bottom, left).

The syntax is m{side}-{number} Side can be replace with t (top), r (right), b (bottom), l (left) 

Here are some example:
```html
<div class="mt-10"> margin-top: 0px;
<div class="ml-1">  margin-left: 0.25rem;
<div class="mb-4">  margin-bottom: 1rem; 
<div class="mr-96"> margin-right: 24rem;
```
Let's do an example to see by our self:
```html
<h1>Welcome to this page</h1>
<p>In this page we will learn Tailwind CSS spacing utilities</p>
```
![blank example](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kkxbr3ggr0keofk0v6zf.png)
Note, using Tailwind our HTML tag as no default attributes. The h1 text look the same as p text and there is no spacing at all.

Let's add our first Tailwind margin utility:
```html
<h1 class="mt-10 ml-10">Welcome to this page</h1>
<p class="mt-5 ml-10">In this page we will learn Tailwind CSS spacing utilities</p>
```
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nw7wxtytnmyqyk0kzcjh.png)

<h2>Margin x and y</h2>
Tailwind allow to modify both x-axis margin and y-axis margin with one short syntax: m{axis}-10

For example to change left, right and top, bottom:
```html
<div class="ml-10 mr-10">
<div class="mt-10 mb-10">
same as
<div class="mx-10">
<div class="my-10">
```

<h2>Paddings</h2>
Paddings utilities work the exact same way, with same syntax as margins. Just replace the m{side}-{number} with a p{side}-{number}

Remember padding is the space inside element border. Here a example. For clarity purpose we will display a border to our html elements:
```html
<h1 class="border-2 mt-10 ml-10">Welcome to this page</h1>
<p class="border-2 mt-5 ml-10">In this page we will learn Tailwind CSS spacing utilities</p>
```
![border](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hdcxxrernbt84qgyzhsv.png) Note there is no space between element and is border.

Let's change that with paddings:
```html
<h1 class="p-5 mt-10 ml-10 border-2">Welcome to this page</h1>
<p class="p-5 mt-5 ml-10 border-2">In this page we will learn Tailwind CSS spacing utilities</p>
```
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fqy2r4ksxoy260cl4wvv.png) Much better :-)

<h2>Conclusion</h2>
That's it for today. Tomorrow the journey continue! Stay tune and follow-me to miss nothing.

You can follow me on Twitter: <a href="https://twitter.com/justericchapman?ref_src=twsrc%5Etfw" class="twitter-follow-button" data-show-count="false">Follow @justericchapman</a><script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>