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.
m-{size}: Set the margin on all four sides.mt-{size}: Set the top margin (margin-top).mr-{size}: Set the right margin (margin-right).mb-{size}: Set the bottom margin (margin-bottom).ml-{size}: Set the left margin (margin-left).mx-{size}: Set the horizontal margins (left and right margins).my-{size}: Set the vertical margins (top and bottom margins).
Common margin values:
m-0: Set the margin to 0.m-1tom-64: Set margins of different sizes, by default based on theremunit.m-px: Set the margin to 1px.m-auto: Set the margin to auto, commonly used for centering.
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.
-
p-{size}: Set the padding on all four sides. -
pt-{size}: Set the top padding (padding-top). -
pr-{size}: Set the right padding (padding-right). -
pb-{size}: Set the bottom padding (padding-bottom). -
pl-{size}: Set the left padding (padding-left). -
px-{size}: Set the horizontal padding (left and right padding). -
py-{size}: Set the vertical padding (top and bottom padding). -
p-0: Set the padding to 0. -
p-1top-64: Set padding of different sizes. -
p-px: Set the padding to 1px.
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:
leading-none: Set the line height to 1 (no spacing).leading-tight: Set the line height to a more compact value.leading-snug,leading-normal,leading-loose: Provide different line-height values.leading-{size}: Set precise line height using a specified unit.
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:
tracking-tight: Reduce letter spacing.tracking-normal: Set normal letter spacing.tracking-wide: Increase letter spacing.tracking-{size}: Set letter spacing using a specified unit.
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:
gap-0: Set the gap to 0.gap-1togap-64: Set different gap values.
Example
01
02
03
04
Try it »