Tailwind Margin Basics: How Add and Use Margin in Tailwind CSS

Tailwind CSS makes it extremely easy to apply margin to your HTML page.

Basic Usage

Before we dive into the specifics, let’s understand the basic structure for using margin classes in Tailwind CSS. Margin classes follow a simple naming convention: m-{size}, where {size} can be one of the predefined margin sizes, such as 0, 1, 2, 3, 4, 5, 6, 8, 9, 10, 11, 14, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 60, 64, 72, 80, 96, , auto.

In addition you can use margin auto:

Here’s how you use margin classes in your HTML elements:

<div class="m-4">
  <!-- Your content goes here -->
</div>

In this example, the m-4 class adds a margin of 1.25rem (or 20px) to all four sides of the <div> element.

Adding Margin to All 4 Sides

To add margin to all four sides of an element, simply use the m-{size} class. For instance, if you want to add a margin of 2rem (or 32px) to all sides, you can do this:

<div class="m-8">
  <!-- Your content goes here -->
</div>

Adding Margin to One Side Only

If you need to add margin to a single side of an element, you can use the following classes:

  • mt-{size}: Margin top
  • mr-{size}: Margin right
  • mb-{size}: Margin bottom
  • ml-{size}: Margin left

For example, to add a margin of 1.5rem (or 24px) to the top of an element, you can use the mt-6 class like this:

<div class="mt-6">
  <!-- Your content goes here -->
</div>

Adding Horizontal Margin

Adding margin to only the left and right sides of an element can be achieved by using the mx-{size} class. Here’s an example of adding a horizontal margin of 3rem (or 48px):

<div class="mx-12">
  <!-- Your content goes here -->
</div>

Adding Vertical Margin

Similarly, to add margin to only the top and bottom sides, use the my-{size} class. Suppose you want to add a vertical margin of 2rem (or 32px):

<div class="my-8">
  <!-- Your content goes here -->
</div>

Add Negative Margin

Tailwind CSS also allows you to add negative margins when needed. Negative margins can be useful for overlapping elements or fine-tuning your layout. To apply a negative margin, use the -{size} notation, like this:

<div class="-ml-4">
  <!-- Your content goes here -->
</div>

In this example, a negative margin of 1rem (or 16px) is applied to the left side of the element.

Adding Custom Margin Values

While Tailwind CSS provides a range of predefined margin sizes, there might be occasions when you require a specific margin size that isn’t covered by the standard classes. Fortunately, Tailwind CSS allows you to add custom margin values effortlessly.

To add a custom margin size, follow these steps:

  1. Modify Your Configuration: Open your Tailwind CSS configuration file (often named tailwind.config.js) and locate the theme section. Inside the extend property, add a margin key to define your custom margin values.

    // tailwind.config.js
    
    module.exports = {
      theme: {
        extend: {
          margin: {
            'custom': '2rem', // You can use any value you need here
          },
        },
      },
      // ...
    }
    
  2. Use the Custom Margin Class: After defining your custom margin value in the configuration, you can apply it directly to your HTML elements. For instance, if you’ve defined a custom margin size, you can use it like this:

<div class="m-custom">
 <!-- Your content goes here -->
</div>

This will apply a 2rem (32px) margin to the element’s margins.

author's bio photo

Hi there! I am Avic Ndugu.

I have published 100+ blog posts on HTML, CSS, Javascript, React and other related topics. When I am not writing, I enjoy reading, hiking and listening to podcasts.