All Posts

How to use padding in Tailwind CSS

Padding is everywhere in web design. Every button, every card, every section needs proper spacing between its content and edges. Get it wrong and your interface looks cramped or weirdly stretched.

The old way meant writing custom CSS for every single element. You would end up with dozens of padding declarations scattered across your stylesheet, inconsistent values, and a maintenance headache.

Tailwind CSS changes this completely. With simple utility classes you can add padding to any element in seconds, keep your spacing consistent across the entire project, and make changes without touching a CSS file.

How padding classes work in Tailwind

Tailwind gives you a bunch of padding utilities that follow a simple pattern. The basic format is p- followed by a number, like p-4 or p-8.

These numbers map to Tailwind's spacing scale. By default, p-1 equals 0.25rem (4px), p-2 equals 0.5rem (8px), p-4 equals 1rem (16px), and so on. The scale goes all the way up to p-96 for really large spacing.

But you are not limited to padding all sides at once. Tailwind has utilities for every direction:

  • pt-4 adds padding to the top
  • pr-4 adds padding to the right
  • pb-4 adds padding to the bottom
  • pl-4 adds padding to the left

You can also use shortcuts for vertical and horizontal padding:

  • px-4 adds padding to left and right (horizontal)
  • py-4 adds padding to top and bottom (vertical)

Here is a visual example showing how these different utilities work:

<div class='max-w-3xl mx-auto space-y-6 p-4'>
  <div class='space-y-2'>
    <h3 class='text-sm font-semibold text-gray-700'>All sides: p-8</h3>
    <div class='p-8 bg-blue-100 border-2 border-blue-300 rounded-lg'>
      <p class='bg-white p-2 rounded text-sm'>Content with equal padding on all sides</p>
    </div>
  </div>

  <div class='space-y-2'>
    <h3 class='text-sm font-semibold text-gray-700'>Horizontal: px-8</h3>
    <div class='px-8 bg-green-100 border-2 border-green-300 rounded-lg'>
      <p class='bg-white p-2 rounded text-sm'>Content with left and right padding only</p>
    </div>
  </div>

  <div class='space-y-2'>
    <h3 class='text-sm font-semibold text-gray-700'>Vertical: py-8</h3>
    <div class='py-8 bg-purple-100 border-2 border-purple-300 rounded-lg'>
      <p class='bg-white p-2 rounded text-sm'>Content with top and bottom padding only</p>
    </div>
  </div>

  <div class='space-y-2'>
    <h3 class='text-sm font-semibold text-gray-700'>Individual: pt-8 pr-4 pb-2 pl-12</h3>
    <div class='pt-8 pr-4 pb-2 pl-12 bg-orange-100 border-2 border-orange-300 rounded-lg'>
      <p class='bg-white p-2 rounded text-sm'>Each side has different padding</p>
    </div>
  </div>
</div>

The colored backgrounds in this example make it easy to see exactly where the padding is applied. The white content box shows you the actual content area, while the colored area around it is the padding.

Picking the right padding utility for your needs

When you are working with padding in Tailwind, you have got choices. Do you pad all sides equally? Just horizontal or vertical? Or do you need fine control over each individual side?

Here is how I think about it.

Use p- for consistent spacing on all sides. This works great for buttons, badges, or any component where you want equal breathing room all around. A simple p-4 gives you uniform padding thats balanced and clean.

Use px- and py- when you need different spacing on different axes. Buttons are the perfect example – they usually need more horizontal padding than vertical. Something like px-6 py-3 creates that nice, wide button shape users expect.

Use individual side utilities when you need precise control. Sometimes you need extra padding on just one or two sides. Maybe you want more top padding to separate content from a header, or extra left padding to offset an icon. That is when pt-8 or pl-4 comes in handy.

Here is how this looks in practice with common UI elements:

<div class='max-w-2xl mx-auto space-y-8 p-4'>
  <div class='space-y-3'>
    <h3 class='text-sm font-semibold text-gray-700'>Button with px-6 py-3</h3>
    <button class='px-6 py-3 bg-blue-500 text-white rounded-lg hover:bg-blue-600 font-medium'>
      Click Me
    </button>
    <p class='text-xs text-gray-500'>More horizontal padding creates a wider button</p>
  </div>

  <div class='space-y-3'>
    <h3 class='text-sm font-semibold text-gray-700'>Card with p-6</h3>
    <div class='p-6 bg-white rounded-lg shadow-md border border-gray-200'>
      <h4 class='font-semibold text-lg text-gray-800'>Card Title</h4>
      <p class='text-gray-600 mt-2'>Equal padding on all sides keeps content balanced and clean</p>
    </div>
  </div>

  <div class='space-y-3'>
    <h3 class='text-sm font-semibold text-gray-700'>Notice with pl-4 pr-6 py-3</h3>
    <div class='pl-4 pr-6 py-3 border-l-4 border-blue-500 bg-blue-50 rounded-r-lg'>
      <p class='font-medium text-gray-800'>Important Notice</p>
      <p class='text-sm text-gray-600 mt-1'>Extra left padding makes room for the accent border</p>
    </div>
  </div>

  <div class='space-y-3'>
    <h3 class='text-sm font-semibold text-gray-700'>Form input with px-4 py-2</h3>
    <input type='text' placeholder='Enter your email' class='px-4 py-2 border border-gray-300 rounded-lg w-full focus:outline-none focus:ring-2 focus:ring-blue-500'>
    <p class='text-xs text-gray-500'>Form inputs typically need less vertical padding</p>
  </div>
</div>

The key is matching the padding type to your design needs. Most of the time px- and py- will cover 80% of your use cases. Save individual side padding for those special situations where you need asymmetric spacing.

Using custom values and responsive padding

Sometimes Tailwind's default spacing scale does not have exactly what you need. Maybe your design calls for 14px of padding, but the scale only offers 12px (p-3) or 16px (p-4).

No problem. Tailwind lets you use arbitrary values with square brackets. Just write p-[14px] and you are set. You can use any CSS unit – pixels, rems, percentages, even calc() expressions.

Here are some examples:

  • p-[14px] – exactly 14 pixels
  • p-[2.5rem] – 2.5 rem units
  • p-[5%] – 5 percent of the parent
  • px-[calc(100%-2rem)] – calculated value

But here is my advice: stick to the default scale when you can. Arbitrary values are super useful but if you are using them constantly, you lose the consistency that makes Tailwind so powerful. Use them for those edge cases where you really need a specific value.

Now let's talk responsive padding. This is where Tailwind really shines.

You can add different padding at different screen sizes using breakpoint prefixes. Want less padding on mobile and more on desktop? Just combine utilities like this: p-4 md:p-8 lg:p-12.

<div class='max-w-4xl mx-auto space-y-8 p-4'>
  <div class='space-y-3'>
    <h3 class='text-sm font-semibold text-gray-700'>Responsive: p-4 md:p-8 lg:p-12</h3>
    <div class='p-4 md:p-8 lg:p-12 bg-gradient-to-br from-blue-100 to-blue-200 rounded-lg border-2 border-blue-300'>
      <div class='bg-white p-3 rounded shadow-sm'>
        <p class='text-sm text-gray-700'>This container has more padding as the screen gets wider. Try resizing your browser window.</p>
      </div>
    </div>
    <p class='text-xs text-gray-500'>Mobile: 1rem, Tablet: 2rem, Desktop: 3rem</p>
  </div>

  <div class='space-y-3'>
    <h3 class='text-sm font-semibold text-gray-700'>Responsive axis: px-4 py-6 md:px-8 md:py-10</h3>
    <div class='px-4 py-6 md:px-8 md:py-10 bg-gradient-to-br from-purple-100 to-purple-200 rounded-lg border-2 border-purple-300'>
      <div class='bg-white p-3 rounded shadow-sm'>
        <h4 class='font-bold text-gray-800'>Hero Section</h4>
        <p class='text-sm text-gray-600 mt-2'>Different horizontal and vertical padding at each breakpoint</p>
      </div>
    </div>
  </div>

  <div class='space-y-3'>
    <h3 class='text-sm font-semibold text-gray-700'>Arbitrary value: p-[14px]</h3>
    <div class='p-[14px] bg-gradient-to-br from-orange-100 to-orange-200 rounded-lg border-2 border-orange-300'>
      <div class='bg-white p-3 rounded shadow-sm'>
        <p class='text-sm text-gray-700'>Exactly 14 pixels of padding using arbitrary value syntax</p>
      </div>
    </div>
  </div>

  <div class='space-y-3'>
    <h3 class='text-sm font-semibold text-gray-700'>Combining both approaches</h3>
    <button class='px-6 py-2 md:px-[30px] md:py-[10px] bg-green-500 hover:bg-green-600 text-white font-medium rounded-lg transition-colors'>
      Responsive Custom Button
    </button>
    <p class='text-xs text-gray-500'>Uses default scale on mobile, arbitrary values on tablet+</p>
  </div>
</div>

Mobile users get comfortable padding without wasting space. Desktop users get generous padding that takes advantage of the extra screen real estate. Everyone wins.

One thing to watch out for: don't go crazy with responsive padding. If you have padding changing at every single breakpoint, your HTML gets messy fast. Stick to 2-3 breakpoints max for most use cases.

Real-world padding patterns you can use

Alright, let's put everything together with some practical examples you can actually use in your projects. These are common UI patterns I see all the time, and the padding choices make a real difference in how professional they look.

Cards and panels: Cards need enough padding to let content breathe but not so much that they feel empty. I usually go with p-6 as a starting point. For larger cards or feature sections, bump it up to p-8 or even p-10.

Navigation bars: Horizontal navigation typically needs px-6 py-4 on the container and px-3 py-2 on individual nav links. This creates clickable areas that feel comfortable without making the navbar too tall.

Content sections: Page sections benefit from responsive padding. Start with py-12 px-4 on mobile and increase to py-20 px-8 on desktop. The generous vertical padding creates clear visual separation between sections.

Form elements: Input fields work best with compact padding like px-4 py-2. Too much padding makes them feel bloated. Buttons in forms usually want px-6 py-2.5 to stand out slightly more than inputs.

Here is a complete example showing all these patterns working together:

<div class='min-h-screen bg-gray-50'>
  <!-- Navigation -->
  <nav class='bg-white shadow-sm px-6 py-4'>
    <div class='flex items-center justify-between max-w-6xl mx-auto'>
      <span class='font-bold text-xl text-gray-800'>Brand</span>
      <div class='flex space-x-1'>
        <a href='#' class='px-3 py-2 rounded text-gray-700 hover:bg-gray-100 transition-colors'>Home</a>
        <a href='#' class='px-3 py-2 rounded text-gray-700 hover:bg-gray-100 transition-colors'>About</a>
        <a href='#' class='px-3 py-2 rounded text-gray-700 hover:bg-gray-100 transition-colors'>Contact</a>
      </div>
    </div>
  </nav>

  <!-- Content Section -->
  <section class='py-12 px-4 md:py-16 md:px-8'>
    <div class='max-w-4xl mx-auto'>
      <h1 class='text-3xl font-bold text-gray-900 mb-8'>Dashboard Overview</h1>

      <!-- Card Grid -->
      <div class='grid md:grid-cols-2 gap-6 mb-8'>
        <div class='p-6 bg-white rounded-lg shadow-md border border-gray-100'>
          <div class='flex items-center justify-between mb-4'>
            <h3 class='text-lg font-semibold text-gray-800'>Total Users</h3>
            <span class='text-2xl'>👥</span>
          </div>
          <p class='text-3xl font-bold text-blue-600'>12,345</p>
          <p class='text-sm text-gray-500 mt-2'>+12% from last month</p>
        </div>

        <div class='p-6 bg-white rounded-lg shadow-md border border-gray-100'>
          <div class='flex items-center justify-between mb-4'>
            <h3 class='text-lg font-semibold text-gray-800'>Revenue</h3>
            <span class='text-2xl'>💰</span>
          </div>
          <p class='text-3xl font-bold text-green-600'>$54,320</p>
          <p class='text-sm text-gray-500 mt-2'>+8% from last month</p>
        </div>
      </div>

      <!-- Form -->
      <div class='p-6 bg-white rounded-lg shadow-md border border-gray-100'>
        <h3 class='text-lg font-semibold text-gray-800 mb-4'>Newsletter Signup</h3>
        <form class='space-y-4'>
          <input type='email' placeholder='Enter your email address' class='w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent'>
          <button type='submit' class='w-full px-6 py-2.5 bg-blue-500 text-white font-medium rounded-lg hover:bg-blue-600 transition-colors'>
            Subscribe Now
          </button>
        </form>
      </div>
    </div>
  </section>
</div>

These patterns work because the padding serves a purpose. Navigation needs enough padding for easy clicking. Cards need padding to separate content from edges. Forms need padding that makes inputs comfortable to use.

The best part? Once you establish these patterns in your project, you can reuse them consistently. Your p-6 cards always look the same. Your px-6 py-4 navigation feels familiar. Users get a cohesive experience and you get faster development.

That is everything you need to know about using padding in Tailwind CSS. Start with the default spacing scale, use axis utilities for most cases, add responsive padding where it makes sense, and save arbitrary values for special situations.

The patterns I showed you will cover 90% of what you build. Master these basics and you will create cleaner layouts with way less CSS.

Now go build something with better spacing!

Other posts you might like...

See more

Subscribe to our newsletter

Get notified when we add new templates, components, and more!

Thanks for subscribing!
Please check your email to confirm your subscription.

WindyBase Make development a breeze

WindyBase is a weekly curated Tailwind CSS template and tool directory built for the modern developer.

© 2026 WindyBase. All rights reserved.