Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Tailwind CSS Spacing

Tailwind CSS Spacing

In Tailwind CSS, spacing is an important part of layout.

Spacing refers to the distance between elements, including padding, margin, line-height, letter-spacing, etc.

Tailwind CSS provides some common utility classes, making the setting of these spacings simpler and more intuitive.


Margin

Margin is used to control the distance between an element and other elements. Tailwind provides concise utility classes to set margins, with the common format being m-{size}, where m represents the margin,{size} is the specific value.

Common margin values:

Use mt-*、mr-*、mb-* and ml-* utility classes to control the margin on one side of an element.

For example,mt-6 adds a 1.5rem margin to the top of an element,mr-4 adds a 1rem margin to the right of an element,mb-8 adds a 2rem margin to the bottom of an element, while ml-2 adds a 0.5rem margin to the left of an element.

Example

mt-6

mr-4

mb-8

ml-2

Try it »

In this example,mt-6 sets the top margin of an element,mr-4 sets the right margin,mb-8 sets the bottom margin,ml-2 sets the left margin.

Use the mx-* utility to control the horizontal margins of an element.



mx-8

Use the my-* utility to control the vertical margins of an element.



my-8

Use the m-* utility to control the margins on all sides of an element.



m-8

Padding

Padding is used to control the distance between an element's content and its border.

Like margins, Tailwind also provides a rich set of utility classes for padding.

Example

pt-6

pr-4

pb-8

pl-2

Try it »

Use the px-* utility to control the horizontal padding of an element.



px-8

Use the py-* utility to control the vertical padding of an element.



py-8

Use the p-* utility to control the padding on all sides of an element.



p-8

Use the ps-* and pe-* utilities to set the padding-inline-start and padding-inline-end logical properties, which map to the left or right side depending on text direction.

Example

 

ps-8

 

pe-8

 

ps-8

 

pe-8

Try it »


Line Height

Line height controls the vertical spacing between lines of text.

Tailwind provides leading-{size} utility classes to adjust line height.

Common line-height values:

Example

This is compact text.

This is looser text.

Try it »


Letter Spacing

Letter spacing controls the horizontal spacing between text characters.

Tailwind uses tracking-{size} utility classes to adjust letter spacing.

Common letter-spacing values:

Example

Compact text

Loose text

Try it »


Gap

gap is used to set the spacing between elements in Grid or Flexbox layouts.

It adds spacing between all child elements, especially useful for creating consistent spacing in layouts.

Common gap values:

Example

 

01

 

02

 

03

 

04

Try it »