Simple Analytics

Tailwind Padding: A Quick Overview

By: Erwin, 14 March 2023

Updated: 7 September 2023

Category: #quickoverview

< back to the Tailscan Blog

Padding is an essential aspect of any web design, and with the Tailwind CSS framework, customizing and adding padding can be done with ease. In this blog post, we will go through everything you need to know about Tailwind padding, including how to customize and add it to your projects using the existing classes.

What is Padding in Tailwind?

Padding is the space between an element and its content, and in Tailwind CSS, padding can be customized using classes with the format of p{t|r|b|l|x|y|s|e}-{size}.

The 'p' stands for padding, and {t|r|b|l|x|y|s|e} represents top, right, bottom, left, x-axis (horizontal), y-axis (vertical), inline-start or inline-end paddings, respectively. The padding different classes in Tailwind written out are as follows:

  • p-{size} (padding on all sides)
  • pt-{size} (padding-top)
  • pr-{size} (padding-right)
  • pb-{size} (padding-bottom)
  • pl-{size} (padding-left)
  • px-{size} (horizontal padding)
  • py-{size} (vertical padding)
  • ps-{size} (inline-start padding)
  • pe-{size} (inline-end padding)

The 'size' part of the class represents the size of the padding, where the default value is 0.25 rem (root em) per unit, which equals 4 pixels in Tailwind CSS with the following predefined sizes:

0, px (1 pixel), 0.5, 1 (eq: 4 pixels), 1.5, 2 (eq: 8 pixels), 2.5, 3, 3.5, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 60, 64, 72, 80, and 96.

For instance, to add padding to the top of an element with a size of 2rem (equal to 32 pixels), you can use the class pt-8:

<div class="pt-2">
    <!-- Your content here -->
</div>

Another example: the p-4 class will add a 1 rem (16-pixel) padding to all sides of an element. You can add 1-pixel padding by using the px value, for example: px-px, py-px, p-px, etc.

Conditional Padding in Tailwind CSS with variants

You can also use variant modifiers to customize padding further, like hover:, focus:, and responsive variants such as sm:, md:, and lg:. Tailwind CSS makes it easy to adapt padding to different screen sizes by allowing you to add responsive sizing.

The example below shows how to add padding to an element on hover:

<button class="p-4 hover:p-8">
  Button Text
</button>

Now, when you hover over the button, it will increase the padding from 1rem to 2rem in size. You can also use the responsive variant modifier to adjust the padding on different screens. This is a very common use case:

<div class="p-4 sm:p-8 md:p-16">
   <!-- Your content here -->
</div>

In the above example, the standard padding is 1rem. The sm:p-8 and md:p-16 classes will adjust the padding to have a size of 2rem on small screens and 4rem on medium (and larger than medium) screens.

Inline-start and inline-end padding

The inline-start and inline-end padding classes are used to add padding to the start and end of an inline element. The start and end of an element are determined by the text direction of the element. This will help make your website more accessible for people who use different languages and text directions.

For example, in English, the start of an element is the left side, and the end is the right side (LTR - left-to-right). In Arabic, the start of an element is the right side, and the end is the left side (RTL).

Below is an example using ps-8 and pe-8, both with LTR and RTL as text direction.

Left-to-right (LTR)

ps-8

pe-8

Right-to-left (RTL)

ps-8

pe-8

Using Arbitrary Values with Tailwind Padding

While Tailwind CSS provides many predefined padding sizes, there may be instances where you need to use a specific pixel value for your padding. For this, Tailwind CSS allows you to use square brackets around the pixel value, like so p-[90px] or px-[40px].

This way, if you need a padding of exactly 90 pixels, you can add the class p-[90px]:

<div class="p-[90px]">
  <!-- Your content here -->
</div>

Padding is crucial when building websites, and Tailwind CSS makes it effortless to customize and add padding to your website. By using the predefined padding classes with simple formatting, you can add padding efficiently to individual or multiple sides of an element. With the use of variant modifiers, you can adjust padding to your specific needs, whether it's responsive or on hover.

Check out all the Tailwind CSS padding classes here: Tailwind CSS Padding Classes.

Tailscan for Tailwind CSS

The absolute must-have tool for anyone using Tailwind CSS.

Build, design and debug your Tailwind website visually with Tailscan, right within the browser.

Get Tailscan now
video poster

Want to learn more? Join the Tailscan email list!

Get the latest on Tailwind CSS and Tailscan straight to your inbox with our monthly newsletter.

If you want to unsubscribe later, you can do so very easily, no hard feelings!