How to Add Padding to All Sides in Tailwind CSS ?
Last Updated :
14 Feb, 2024
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 content, inside of any defined borders. Tailwind CSS offers a basic and flexible approach to add padding to all sides of an element or each side individually.
Uniform Padding to All Sides
To add padding to all sides of an element equally in Tailwind CSS, you can use the p-{size} syntax, where {size} represents the scale of the padding you wish to apply. Tailwind's default configuration offers a scale from 0 to 7, with additional sizes available if configured in tailwind.config.js.
Example: This example adds uniform padding to the element.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content=
"width=device-width, initial-scale=1.0">
<title>Uniform Padding</title>
<link href=
"https://cdn.jsdelivr.net/npm/[email protected]/dist/tailwind.min.css"
rel="stylesheet">
</head>
<body class="m-4">
<div class="p-8 w-48 h-24 bg-green-200">
GeeksforGeeks
</div>
</body>
</html>
Output

In this example, p-4 applies a padding of 1rem (16px) around the entire content of the <div> element. The size of the padding increases as the number after p- increases.
Individual Side Padding
Tailwind also allows you to specify padding for individual sides of an element using pt-, pr-, pb-, and pl- for padding-top, padding-right, padding-bottom, and padding-left, respectively.
Example: This example adds Individual Side Padding to the element.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content=
"width=device-width, initial-scale=1.0">
<title>Individual Side Padding Example</title>
<link href=
"https://cdn.jsdelivr.net/npm/[email protected]/dist/tailwind.min.css"
rel="stylesheet">
</head>
<body class="m-4">
<div class="pt-2 pr-4 pb-2 pl-4 w-48 h-24 bg-green-200">
This div has different padding on each side.
</div>
</body>
</html>
Output

This example applies 0.5rem (8px) of padding to the top and bottom (pt-2 and pb-2) and 1rem (16px) of padding to the right and left sides (pr-4 and pl-4).
Responsive Padding
Tailwind CSS excels in creating responsive designs with minimal effort. You can apply different paddings at various breakpoints (e.g., sm:, md:, lg:, xl:, 2xl:) to ensure your layout adapts to different screen sizes.
Example: This example adds the Responsive Padding.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content=
"width=device-width, initial-scale=1.0">
<title>Responsive Padding Example</title>
<link href=
"https://cdn.jsdelivr.net/npm/[email protected]/dist/tailwind.min.css"
rel="stylesheet">
</head>
<body class="m-4">
<div class="p-2 md:p-4 lg:p-8 w-48 h-24 bg-green-200">
This div's padding changes with the screen size.
</div>
</body>
</html>
Output

Here, the <div> starts with a padding of 0.5rem (8px) on all sides. As the screen width reaches the md breakpoint, the padding increases to 1rem (16px), and at the lg breakpoint, it further increases to 2rem (32px).
Padding X and Y
For scenarios where you want to apply the same padding value to the top and bottom (Y-axis) or right and left (X-axis), Tailwind offers px- and py- utilities.
Example: This example adds the padding on X and Y axis.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content=
"width=device-width, initial-scale=1.0">
<title>X and Y Axis Padding</title>
<link href=
"https://cdn.jsdelivr.net/npm/[email protected]/dist/tailwind.min.css"
rel="stylesheet">
</head>
<body class="m-4">
<div class="px-6 py-4 w-48 h-24 bg-green-200">
This div has uniform padding
on the X-axis and Y-axis.
</div>
</body>
</html>
Output

The px-6 class applies a padding of 1.5rem (24px) to both the right and left sides, while py-4 applies a padding of 1rem (16px) to both the top and bottom sides.
Similar Reads
How to Add Text Shadow in Tailwind CSS?
Adding a text shadow in Tailwind CSS is useful for making text for creating visual effects like glows and embossed or debossed effects. Tailwind CSS does not provide text shadow utilities out of the box but it allows you to easily extend its functionality to add custom utilities. In this article, we
3 min read
How to Apply Inset to Box Shadow in Tailwind CSS?
In web development, shadows play a significant role in attracting user attention. By applying shadows to elements on our web pages, we can enhance their visual appearance and attract the user's focus. In Tailwind CSS, we can apply an insert box shadow by using the "shadow-inner" utility class. Appro
2 min read
How to Set Text Color in RGBA in Tailwind CSS ?
In this article, we will change the color of text to rgba(0, 0, 0, 0.54) which is an RGB color format with an added parameter alpha that tells the opacity of the color. In this article, we are going to learn how we can achieve the text color of rgba(0, 0, 0, 0.54) in Tailwind CSS and hence, change t
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 path
2 min read
How to Add Rounded Corners to an Image in Tailwind CSS ?
Adding rounded corners to an image in Tailwind CSS is an easy way to make your pictures look nicer. Instead of having sharp corners, you can give them smooth, rounded edges. Tailwind CSS has simple classes that let you choose how rounded the corners are, whether just a bit or completely round. Appro
3 min read
How to Create a Sidebar in NextJS & Tailwind CSS?
In web development, sidebars are commonly used to organize navigation elements and provide quick access to different sections of a website. A sidebar is a vertical menu or panel that is typically positioned on the left or right side of the main content area. Prerequisites:Next.jsTailwindJavaScriptSt
5 min read
How to Add Tailwind CSS to HTML ?
Tailwind CSS is a utility-first framework offering pre-defined classes for rapid styling of HTML elements. It simplifies customization and accelerates web development workflows. Integration Options: CDN Integration: Quickly add Tailwind CSS via a CDN link in the <head> of your HTML.Using npm:
3 min read
How to use Tailwind @apply in CSS Modules in Next.js ?
In Next.js, you can enhance CSS Modules by integrating Tailwind's @apply directive. This approach combines Tailwind's utility classes with modular CSS, ensuring scoped styling and efficient management across your application. We can integrate Tailwind's @apply directive in CSS Modules of Next.js by
3 min read
How to make a Split Navigation Bar in Tailwind CSS ?
Split Navigation Bars in Tailwind CSS implement the different classes that help to distribute the content across the header of a webpage, providing a visually balanced layout. By separating navigation items into distinct sections, it becomes easier for users to identify and access different parts of
3 min read
How to Control the Background Size in Tailwind CSS ?
To control background-size in Tailwind CSS, use the bg-{size} utility classes. These classes allow you to adjust the background size, providing options like bg-cover for covering the entire element, bg-contain for fitting the content within, and custom sizes such as bg-auto or bg-50%. Tailwind simpl
2 min read