Tailwind CSS: Box model properties

The box model sets the sizing of the elements based on a few CSS properties.

We can define:

  1. The element width and height
  2. Padding and Margin
  3. Border

The element width and height

In Tailwind CSS, the element size is defined using w- (width) and h- (height), combined with the values.

Here’s a handy table of values you can use to set a fixed value:

ClassPixelsRem
w-00px
w-px1px
w-0.52px0.125rem
w-14px0.25rem
w-1.56px0.375rem
w-28px0.5rem
w-2.510px0.625rem
w-312px0.75rem
w-3.514px0.875rem
w-416px1rem
w-520px1.25rem
w-624px1.5rem
w-728px1.75rem
w-832px2rem
w-936px2.25rem
w-1040px2.5rem
w-1144px2.75rem
w-1248px3rem
w-1456px3.5rem
w-1664px4rem
w-2080px5rem
w-2496px6rem
w-28112px7rem
w-32128px8rem
w-36144px9rem
w-40160px10rem
w-44176px11rem
w-48192px12rem
w-52208px13rem
w-56224px14rem
w-60240px15rem
w-64256px16rem
w-72288px18rem
w-80320px20rem
w-96384px24rem

You can use a custom value, if you prefer, using this syntax: w-[200px] (use any unit you want, like em or rem)

You can set the width to auto with w-auto, so it’s calculated automatically by the browser (the default behavior)

You can set the width of an element as a percentage of the container’s width, so you can say for example w-1/2 to make it 50% of the container’s width:

ClassCSS
w-1/2width: 50%
w-1/3width: 33.33333%
w-2/3width: 66.66667%
w-1/4width: 25%
w-3/4width: 75%
w-1/5width: 20%
w-2/5width: 40%
w-3/5width: 60%
w-4/5width: 80%
w-1/6width: 16.66667%
w-5/6width: 83.33333%
w-fullwidth: 100%

All those values also work for h-* to set the height.

You can also use w-screen to set the width to be 100% of the viewport width, 100vw (which means the entire browser window) and height can be set using h-screen to set the height to be 100% the viewport height.

Sometimes you want to also set a maximum width. You do so using max-w-VALUE:

ClassCSS
max-w-xsmax-width: 320px
max-w-smmax-width: 384px
max-w-mdmax-width: 448px
max-w-lgmax-width: 512px
max-w-xlmax-width: 576px
max-w-2xlmax-width: 672px
max-w-3xlmax-width: 768px
max-w-4xlmax-width: 898px
max-w-5xlmax-width: 1024px
max-w-6xlmax-width: 1152px
max-w-7xlmax-width: 1280px
max-w-fullmax-width: 100%
max-w-screen-smmax-width: 640px
max-w-screen-mdmax-width: 768px
max-w-screen-lgmax-width: 1024px
max-w-screen-xlmax-width: 1280px
max-w-screen-2xlmax-width: 1536px

You have utility classes for min-width too, also height, min-height and max-height.

Margin and Padding

m and p lets you set margin and padding values.

Compose those 3 tables. Add a dash before the value (e.g. pt-2, m-auto)

SymbolDescription
pPadding
mMargin
-mNegative margin
SymbolDescription
tTop
rRight
bBottom
lLeft
xHorizontal
yVertical
ValuePixelsRem
00px
px1px
0.52px0.125rem
14px0.25rem
1.56px0.375rem
28px0.5rem
2.510px0.625rem
312px0.75rem
3.514px0.875rem
416px1rem
520px1.25rem
624px1.5rem
728px1.75rem
832px2rem
936px2.25rem
1040px2.5rem
1144px2.75rem
1248px3rem
1456px3.5rem
1664px4rem
2080px5rem
2496px6rem
28112px7rem
32128px8rem
36144px9rem
40160px10rem
44176px11rem
48192px12rem
52208px13rem
56224px14rem
60240px15rem
64256px16rem
72288px18rem
80320px20rem
96384px24rem
autoauto

Examples:

  • mt-40
  • -mr-2
  • m-20
  • p-10
  • py-10

I sometimes use margin: 0 auto to center things, the class mx-auto applies it.

Lessons in this unit:

0: Introduction
1: ▶︎ Box model properties
2: Colors
3: Typography
4: Flexbox and Grid in Tailwind
5: Modifiers
6: Responsive design in Tailwind
Are you intimidated by Git? Can’t figure out merge vs rebase? Are you afraid of screwing up something any time you have to do something in Git? Do you rely on ChatGPT or random people’s answer on StackOverflow to fix your problems? Your coworkers are tired of explaining Git to you all the time? Git is something we all need to use, but few of us really master it. I created this course to improve your Git (and GitHub) knowledge at a radical level. Launching May 21, 2024. Join the waiting list!