How to use tailwind css padding, margin and border in your project.
By Emmanuel Chinonso
As important as CSS margin, padding, and border are, most developers often struggle to understand this concept. New developers usually use these properties without realizing or acknowledging their importance when rendering items on our page. In today’s Tailwind CSS tutorial, we will explain what these properties are, when to use them, and how to use them when we build a project.
Table of content
- Box model
- Tailwind CSS padding
- Tailwind CSS margin
- Tailwind CSS border
- Project using these properties
We cannot explain these properties without explaining the box model.
It may not be obvious, especially to new developers, but every HTML element is rendered as a web page box.
You may create an image that is round and a text beside it. When you inspect it, you will see that HTML renders it as a box.
Therefore, a box model is a box that wraps every block-level HTML element on a webpage. This box model is composed of the content, padding, margin, and borders.
The content has to do with the value of the specified HTML element. This could be an
a tag, or even an
The next area after the content is the padding area. The padding area is the space between its content area and its border.
In Tailwind CSS, the padding size is usually set using the shorthand
padding and a class of
p. Other classes include
The Tailwind CSS padding controls the top, bottom, right, and left space between the content and the border.
However, the tailwind CSS framework offers more default classes for your web development. You can find all the default padding in Tailwind CSS documentations.
The next thing we will be wondering is when we can use the padding property. You can use it in the following ways.
- When you don’t want your content to touch the edges of the container
- When you want to increase a content block’s size without making the content itself bigger
- When you need space between an inner element and the parent box
- When you want the background of the element to display in the produced gap
Tailwind CSS margin
Tailwind CSS margin area is shown by a margin edge that is invisible around the border area. The margin separates elements from other elements.
Manipulating these areas gives the different elements the space their need to appear well on the webpage. If you don’t apply margins to your elements there will appear joined together.
In Tailwind CSS, the margin property is usually denoted with the shorthand
margin and class
ml. Other classes include
You can check other classes and their properties in the Tailwind CSS documentation. The margin property can be used in the following ways
- When you need space around elements, such as separating a photo and words describing it
- When you want to center an element horizontally
- When you want to overlap elements
- When you want to move an element up, down, or side to side.
Tailwind CSS border
The border acts as a shield or edge for the box wrapped around every block-level HTML element.
You can see the border of any element by adding the property
border to your style.
In Tailwind CSS, you add classes to make your border visible on an HTML block-level element. We all know that all HTML elements are block-level even if it doesn’t appear that way. The classes available in Tailwind CSS, along with their properties, are
The border property is usually used when you wish to control the border styles.
Project using these properties
At this point in our Tailwind CSS tutorial, we will build a frame box showing the properties of the border, margin, and padding in action.
The first step is to install Tailwind CSS in your project. We covered that aspect in our previous articles. You can check them out.
Padding, border and margin
We are going to build a text element. It will be represented in block form, and we can see the effect of padding and border.
1 <head> 2 <meta charset="UTF-8"> 3 <meta http-equiv="X-UA-Compatible" content="IE=edge"> 4 <meta name="viewport" content="width=device-width, initial-scale=1.0"> 5 <title>Margin, padding and border effect</title> 6 <link rel="stylesheet" href="style.css"> 7 </head> 8 9 <body class="text-center"> 10 <h1 class="text-center text-purple-500 text-5xl font-bold"> 11 DEVWARES 12 </h1> 13 <div class="flex justify-center"> 14 <div class="p-4 bg-purple-300 w-24 h-24"> 15 <div class=" border-2 border-purple-800 bg-purple-600 w-30 h-30"> padding and border</div> 16 </div> 17 </body> 18 19 </html>
The project we created will like the image below
The next one is building the effect of margin
1 <head> 2 <meta charset="UTF-8"> 3 <meta http-equiv="X-UA-Compatible" content="IE=edge"> 4 <meta name="viewport" content="width=device-width, initial-scale=1.0"> 5 <title>Margin, padding and border effect</title> 6 <link rel="stylesheet" href="style.css"> 7 </head> 8 9 <body class="text-center"> 10 <h1 class="text-center text-purple-500 text-5xl font-bold"> 11 DEVWARES 12 </h1> 13 <div class="flex justify-center"> 14 <div class="m-8 bg-purple-300 w-24 h-24"> 15 <div class=" m-4 border-2 border-purple-800 bg-purple-600 w-30 h-30">margin and border</div> 16 </div> 17 </div> 18 </body> 19 20 </html>
The project we created will look like the image below.
At the end of today’s Tailwind CSS, we showed you how important Tailwind CSS Margin, padding, and the border are to a developer.
We also explained what they are and showed the classes available in Tailwind CSS padding, margin, and border. I hope you can use the Tailwind CSS border, padding, and margin better now.
Create Stunning websites and web apps
Trying to build out all user interfaces and components for your website or web app from scratch can become a very tedious task. A huge reason why we created Contrast Bootstrap to help reduce the amount of time we spend doing that, so we can focus on building some other aspects of the project. Contrast Bootstrap PRO consists of a UI Kit featuring over 10000+ component variants. Together with a template of 5 admin dashboards and 23+ additional multipurpose pages template for building almost any type of website or web app. You can view a demo and learn more about Contrast by clicking here.Try contrast pro
Contrast Bootstrap PRO was built using the most popular CSS framework Bootstrap to help build your next landing, admin SAAS, prelaunch etc project with a clean, prebuilt and well documented template and UI components.Learn more about Contrast