Tailwind CSS

Padding and Margin


Padding and margins are handled using a set of utility classes that provide responsive control over the spacing of elements within your web pages.

 

Padding:

Padding refers to the space between an element's content and its border. Tailwind provides a set of utility classes with the prefix p- to allow you to easily add padding to your elements.

 

Padding Utility Classes:

  • p-{size}: Adds padding to all sides of the element equally.
  • px-{size}: Adds padding horizontally (left and right).
  • py-{size}: Adds padding vertically (top and bottom).
  • pt-{size}: Adds padding to the top.
  • pr-{size}: Adds padding to the right.
  • pb-{size}: Adds padding to the bottom.
  • pl-{size}: Adds padding to the left.

 

Example:

<div class="p-4">Padding on all sides</div>
<div class="px-8">Horizontal padding</div>
<div class="py-6">Vertical padding</div>
<div class="pt-10">Top padding</div>
<div class="pr-3">Right padding</div>
<div class="pb-2">Bottom padding</div>
<div class="pl-5">Left padding</div>

 

Margin:

Margin refers to the space outside of an element's border. It creates space between the element and its surrounding elements. Tailwind provides utility classes to control margin in a flexible and intuitive way.

 

Margin Utility Classes:

  • m-{size}: Adds margin to all sides of the element equally.
  • mx-{size}: Adds margin horizontally (left and right).
  • my-{size}: Adds margin vertically (top and bottom).
  • mt-{size}: Adds margin to the top.
  • mr-{size}: Adds margin to the right.
  • mb-{size}: Adds margin to the bottom.
  • ml-{size}: Adds margin to the left.

 

Example:

<div class="m-4">Margin on all sides</div>
<div class="mx-8">Horizontal margin</div>
<div class="my-6">Vertical margin</div>
<div class="mt-10">Top margin</div>
<div class="mr-3">Right margin</div>
<div class="mb-2">Bottom margin</div>
<div class="ml-5">Left margin</div>

 

Note:

Margin creates space outside of an element.
Padding creates space inside of an element.
Tailwind's utility classes allow precise control over margin and padding, making it easy to create layouts without writing custom CSS.