The Wayback Machine - https://web.archive.org/web/20240926154105/https://www.geeksforgeeks.org/how-to-add-space-between-elements-in-tailwind-css/
Open In App

How to Add Space Between Elements in Tailwind CSS ?

Last Updated : 19 Feb, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Spacing between elements is a critical aspect of web design, affecting readability, flow, and the overall aesthetic of a website. Tailwind CSS, a utility-first CSS framework, provides an extensive set of spacing utilities that make it basic to control padding, margin, and gap spaces within and around elements. This article will delve into the various ways you can add spacing between elements using Tailwind CSS, complete with detailed descriptions and HTML code examples.

Add Space using the Margin

Margin utilities are used to add spaces outside an element, affecting its position relative to other elements.

  • Add Margin to All Sides
  • Add Margin to Specific Side
  • Add Margin to X-axis and Y-axis

Syntax:

<div class="m-4 bg-gray-200">All sides margin</div>
<div class="mt-4 bg-gray-200">Top margin only</div>
<div class="mx-4 my-2 bg-gray-200">X and Y axis margin</div>

Example: Add spaces between elements using margin utilities.

HTML
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content=
        "width=device-width, initial-scale=1.0">
    <title>Rounded Corners with Tailwind CSS</title>
    <link href= 
        "https://unpkg.com/tailwindcss@^1.0/dist/tailwind.min.css"
        rel="stylesheet"> 
</head>

<body class="p-10">
    <div class="m-4 bg-green-200 w-24 h-24">
        All sides Margin
    </div><br>
    
    <div class="mt-4 bg-green-200 w-24 h-24">
        Top Margin
    </div><br>
    
    <div class="mx-4 my-2 bg-green-200 w-24 h-24">
        X and Y axis Margin
    </div>
</body>

</html>

Output

spacing-2

Gap Utilities

Tailwind CSS introduces gap utilities (gap) with the Flexbox that provides a way to evenly space child elements within a parent container.

Example: Add spaces between elements using gap utilities.

HTML
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content=
        "width=device-width, initial-scale=1.0">
    <title>Gap Utilities</title>
    <link href=
"https://unpkg.com/tailwindcss@^1.0/dist/tailwind.min.css" 
        rel="stylesheet">
</head>

<body class="p-10">
    <div class="flex gap-2">
        <div class="w-24 h-24 bg-green-500"></div>
        <div class="w-24 h-24 bg-green-300"></div>
    </div><br>

    <div class="flex gap-4">
        <div class="w-24 h-24 bg-green-500"></div>
        <div class="w-24 h-24 bg-green-300"></div>
    </div><br>

    <div class="flex gap-8">
        <div class="w-24 h-24 bg-green-500"></div>
        <div class="w-24 h-24 bg-green-300"></div>
    </div>
</body>

</html>

Output

spacing-3



Similar Reads

How to Add Horizontal & Vertical Space Between Elements in Tailwind CSS ?
Tailwind CSS simplifies web design with its utility-first approach. Creating websites that look good is important in web development. Tailwind CSS is a tool that helps make this easier. One important thing to learn is how to put space between elements. Below are the approaches to Add Horizontal &amp; Vertical Space Between Elements in Tailwind CSS:
3 min read
What are the space-x and space-y Classes in Tailwind CSS ?
In Tailwind CSS, space-x and space-y classes are used to add spacing between child elements along the horizontal (x-axis) and vertical (y-axis) directions, respectively. These classes are beneficial in flex container scenarios for managing the spacing between flex items. Syntax:&lt;div class="flex space-x-4"&gt; &lt;!-- Child elements with horizont
2 min read
Tailwind CSS Space Between
This class accepts lots of values in tailwind CSS in which all the properties are covered as in class form. It is the alternative to the CSS Space Between property. This class is used for controlling the space between child elements. Space Between classes: space-y-0space-y-reverse-space-y-0space-x-0space-x-reverse-space-x-0Note: You can change the
3 min read
How to add custom styles and ways to add custom styles in Tailwind-CSS ?
Tailwind is a popular CSS framework that makes your User Interface design making the process easier. It provides low-level classes and those classes combine to create styles for various components. Tailwind-CSS comes with a bunch of predefined styling but as a developer, we need more custom styling and the styles offered by tailwind are not enough
3 min read
Storybook-tailwind. How should I add tailwind to storybook
In this article, we will guide you through the process of adding Tailwind CSS to Storybook to enhance your UI development. Storybook is an open-source UI that helps developers create reusable, organized UI components with proper documentation independently. Tailwind CSS is basically a utility-first CSS framework for building rapid custom UI. Prereq
2 min read
How to Add a @tailwind CSS Rule for CSS Checker ?
Tailwind CSS is basically a Utility first CSS framework for building rapid custom UI. It is a highly customizable, low-level CSS framework that gives you all of the building blocks that you need. Also, it is a cool way to write inline styling and achieve an awesome interface without writing a single line of your own CSS. In this article, we will ex
4 min read
How to Align Items and Space with Flex in Tailwind CSS?
Flex allows you to structure the elements in a container with precise control over alignment and spacing. Tailwind CSS provides utility classes to use Flexbox for aligning items and distributing space between them. By using classes like justify-between and justify-around, you can easily create layouts that balance spacing and align content effectiv
3 min read
What is the use of the tailwind.config.js file in Tailwind CSS ?
The tailwind.config.js file in Tailwind CSS serves as a central configuration hub for customizing the framework. It allows you to define colors, fonts, breakpoints, and more, tailoring Tailwind to fit your project's specific design and functionality requirements. Syntax:/** @type {import('tailwindcss').Config} */module.exports = { content: [], them
1 min read
How to add Shadows to Elements in Tailwind CSS ?
Adding shadows to elements in Tailwind CSS is done using utility classes like shadow-sm, shadow-md, and shadow-lg to apply different levels of box shadows. Additionally, you can use the hover:shadow-[size] class for a hover effect with shadows. PreviewSyntax&lt;div class="shadow-* "&gt; This element has a medium box shadow.&lt;/div&gt;Here, the Sta
1 min read
How to add space between elements ?
In this article, we will know how to add space between elements in the HTML table, along with understanding its implementation through the examples. We can use normal CSS to make changes in the table, where we will be using border property to give spacing between different tbody elements. No prior and deep knowledge is required it is just basic CSS
2 min read
How to add space (" ") after an element using :after selector in CSS ?
The :after selector in CSS is used to add the same content multiple times after the content of other elements. It inserts something after the content of each selected element. Syntax: :after { // CSS Property}Example 1: This example uses :after selector to add space after an element. C/C++ Code &lt;!-- HTML code to add space after the selected elem
1 min read
How to add new colors to tailwind-css and keep the originals ones ?
You can easily add new colors to Tailwind CSS and keep the originals ones using customization configuration. You can configure your colors under the colors key in the theme section of your tailwind.config.js file. Follow the below step to add the tailwind.config.js file in your folder. Step 1: Run the below code to your folder's terminal. This will
2 min read
How to Add Icon in Form Input Field in Tailwind CSS ?
User interface components are essential to current web development since they improve the user experience. To give visual clues and enhance the usability of numerous components, including input fields, icons, in particular, are frequently utilized. In this article, we will see how to effectively include Tailwind CSS icons into input elements to imp
3 min read
How to Add New Colors in Tailwind CSS ?
Tailwind CSS is a popular utility-first CSS framework that offers a wide range of pre-designed styles and classes to simplify the process of styling web applications. However, the default set of colors provided by Tailwind may not always meet the requirements of your project. In such cases, you may need to add new colors to the Tailwind configurati
4 min read
How to add a Background Image using Tailwind CSS ?
To add a background image using Tailwind CSS, you can use the bg-[image-class] utility class, where [image-class] corresponds to a predefined set of background image classes. You also need to set the actual image URL using inline or external styles. Syntax&lt;div class="bg-cover bg-center h-screen" style="background-image: url('your-image-url.jpg')
1 min read
How to add Custom Fonts in a Tailwind CSS Project ?
Adding custom fonts to a Tailwind CSS project involves configuring the fontFamily property within the theme section of the tailwind.config.js file. You can then apply the custom font to specific elements using Tailwind utility classes. Syntax// tailwind.config.jsmodule.exports = { theme: { extend: { fontFamily: { custom: ['CustomFont', 'sans-serif'
1 min read
How to Add Padding to All Sides in Tailwind CSS ?
Tailwind CSS is a utility-first CSS framework that provides a vast array of classes to design your website directly in your markup. It emphasizes speed, efficiency, and reduces the time you spend styling in CSS files. One of the most common styling tasks in web development is adding padding to elements, which creates space around an element's conte
3 min read
How to Add Rounded Corners to an Image in Tailwind CSS ?
Tailwind CSS, a utility-first CSS framework, provides a highly efficient way to style web pages without having to leave your HTML. Among its many utilities, Tailwind offers a basic and powerful way to apply rounded corners to elements, including images. This feature enhances the visual appeal of images by softening their sharp edges, making them bl
3 min read
How to add @keyframes to a Tailwind CSS config file ?
Tailwind CSS is a utility framework in which we cannot add @keyframes through any built-in method directly to the configuration file. @Keyframes are used to invoke animation effects to our element. There are various ways to add @keyframes in a Tailwind CSS config file. Table of Content Using External CSS FileUsing Custom Animation in config FileUsi
3 min read
How to Add a Clip Path to Image in Tailwind CSS ?
Web application designers can create creative and eye-catching designs with Tailwind CSS's flexibility in applying clip paths to images. Developers can create complex shapes by using clip paths, which define an element's visible region. Custom Clip Paths using TailwindTailwind CSS's custom clip paths allow developers to define unique shapes with CS
2 min read
How to Add Dark Mode in ReactJS using Tailwind CSS ?
Tailwind CSS is a CSS framework that helps in building rapid custom UI. It is a highly customizable, low-level CSS framework that gives you all of the building blocks that you need. Tailwind CSS creates small utilities with a defined set of options enabling easy integration of existing classes directly into the HTML code. Prerequisites:React JSTail
3 min read
How to Add Tailwind CSS to HTML ?
Tailwind CSS is a utility-first CSS framework that provides a set of pre-defined classes to help you quickly style your HTML elements. It's known for its flexibility and ease of customization. In this article, we will explore different approaches to adding Tailwind CSS to your HTML projects. Table of Content Using a CDNUsing npm for a Custom BuildA
2 min read
How to Add Both !important & selector Strategy for Tailwind CSS Configuration ?
Tailwind CSS is a popular utility-first CSS framework that provides a set of pre-defined classes for common styles such as margins, padding, colors, and more. While Tailwind aims to provide a simple and easy-to-use solution for styling websites and applications, sometimes we need to override the default styles or provide custom styles that are not
3 min read
How to add scale animation on hover using Tailwind CSS in React ?
In this article, we'll see how to add scale animation on hover using tailwind CSS in a ReactJS app. The hover effect appears when a user positions the cursor over an element. In tailwind CSS, the scale utility class helps in getting the zoom effect over an element.  PrerequisitesReact JSTailwind CSSApproach to Add Scale AnimationTo add scale animat
3 min read
How to add a style on a condition in Tailwind CSS ?
In this article, we will see how to integrate conditional styling into our web projects using Tailwind CSS. There are various Tailwind CSS classes are available, although, it usually involves dynamically adding or removing classes using JavaScript. There are different approaches to implementing the styling properties conditionally: By Implementing
3 min read
How to add Full Screen Background Video using Tailwind CSS ?
Tailwind CSS is a CSS framework that helps in building rapid custom UI. It is a highly customizable, low-level CSS framework that gives you all of the building blocks that you need. Tailwind CSS creates small utilities with a defined set of options enabling easy integration of existing classes directly into the HTML code. In this article, we will s
3 min read
How to Add Top Space Between Rows in Twitter Bootstrap ?
Twitter Bootstrap is a popular front-end development framework that provides a set of tools and features to help developers create responsive and mobile-first websites quickly and easily. One of the features offered by Bootstrap is the ability to add top space between rows. This allows developers to create better layouts and improve the overall app
2 min read
Which CSS framework is better Tailwind CSS or Bootstrap ?
In this article, we will see which CSS framework is better tailwind CSS or bootstrap. Both are widely used CSS frameworks for styling the front end. We will understand each one of them in detail. Both frameworks have their own pros and cons so it depends upon the type of application you are going to build and accordingly you can choose the best fra
5 min read
How to use CSS Animations with Tailwind CSS ?
Tailwind CSS classes are used to style elements and apply animations effortlessly. Utilize Tailwind's animation utility classes to add dynamic visual effects. Combine Tailwind CSS with custom CSS animations for versatile and engaging web designs. Table of Content Tailwind CSS Animation Utility ClassesCSS Animations with Tailwind CSS and JavaScriptT
3 min read
How to align form elements to center using Tailwind CSS ?
You can easily align form elements in the center using flex property in Tailwind CSS. Tailwind uses justify-center and items-center property which is an alternative to the flex-property in CSS. Syntax: &lt;div class="flex flex-col justify-center items-center"&gt; .... &lt;/div&gt; flex property: flex-col: This property stacks the flex items column-
3 min read