Mastering Spacing and Sizing in Tailwind CSS
Master spacing and sizing in Tailwind CSS. Learn to apply padding, margin, and width/height utilities using the default spacing scale for consistent layouts.

Previously in this course, we explored applying text and color utilities to bring visual life to our elements. Now that we can control the look of our content, we need to master the layout—specifically, how we create breathing room and define the physical dimensions of our interface components.
In a utility-first workflow, you stop writing custom CSS for every margin-left or width property. Instead, you rely on a predictable, design-token-based system.
Understanding the Tailwind Spacing Scale
Tailwind includes a default spacing scale that works for padding, margin, width, height, and many other properties. This scale uses a unit-less system where 1 equals 0.25rem (which is typically 4px).
| Utility Value | Rem | Pixels |
|---|---|---|
0 | 0rem | 0px |
1 | 0.25rem | 4px |
2 | 0.5rem | 8px |
4 | 1rem | 16px |
8 | 2rem | 32px |
16 | 4rem | 64px |
Because this scale is shared, using p-4 (padding) and m-4 (margin) ensures your spacing remains visually consistent across your entire application.
Applying Padding and Margin Utilities

Padding adds space inside an element, while margin adds space outside the element border. Tailwind simplifies these with intuitive naming conventions:
p-{size}: Padding on all four sides.py-{size}: Padding on the Y-axis (top and bottom).px-{size}: Padding on the X-axis (left and right).m-{size}: Margin on all four sides.my-{size}: Margin on the Y-axis.mx-{size}: Margin on the X-axis.
You can also target specific sides using pt, pr, pb, and pl for padding, or mt, mr, mb, and ml for margins.
HTML<!-- Example: A card with internal padding and external margin --> style="color:#808080"><style="color:#4EC9B0">div class="m-8 p-4 bg-gray-100"> This card has 32px of margin (m-8) and 16px of internal padding (p-4). style="color:#808080"></style="color:#4EC9B0">div>
Controlling Sizing: Width and Height
When you need to constrain the dimensions of an element, you use width (w-*) and height (h-*) utilities. While you can use the spacing scale for fixed values (like w-64), Tailwind also provides percentage-based utilities for responsive layouts.
Width Utilities
w-full: Sets width to100%.w-1/2: Sets width to50%.w-screen: Sets width to100vw(full viewport width).
Height Utilities
h-full: Sets height to100%of the parent.h-screen: Sets height to100vh(full viewport height).
Worked Example: A Responsive Information Box
Let's combine these concepts into a simple component. We want a box that is constrained to a specific width, has generous padding, and sits away from other elements.
HTMLstyle="color:#808080"><style="color:#4EC9B0">div class="w-full max-w-sm mx-auto p-6 bg-white shadow-md border border-gray-200"> style="color:#808080"><style="color:#4EC9B0">h2 class="text-xl font-bold mb-4">Mastering Layoutsstyle="color:#808080"></style="color:#4EC9B0">h2> style="color:#808080"><style="color:#4EC9B0">p class="text-gray-700"> By using p-6 and mb-4, we ensure our content breathes while maintaining a consistent rhythm with the rest of the page. style="color:#808080"></style="color:#4EC9B0">p> style="color:#808080"></style="color:#4EC9B0">div>
In this example, max-w-sm ensures the box doesn't grow too wide on large screens, while mx-auto centers it horizontally within the parent container.
Hands-on Exercise

Open your project's index.html. Add a div element with a background color of bg-blue-50.
- Give it a width of
w-64. - Add a margin of
m-4to separate it from the edges of the page. - Apply
p-8inside the box to push the text away from the borders. - Try changing the padding to
p-2and notice how the text crowds the edges.
Common Pitfalls
- Over-nesting: Don't wrap elements in extra
divs just to apply margin. Use the parent container to control the spacing of children whenever possible. - Ignoring the Scale: Avoid hardcoding arbitrary values (like
w-[133px]) until you have exhausted the default spacing scale. Consistency is the primary benefit of a utility-first approach. - Confusing Margin and Padding: Remember: Padding is Personal (inside the border); Margin is Meaningless to the element itself (it only affects neighbors).
FAQ
Can I use values not in the default scale?
Yes, Tailwind supports "Arbitrary Values" (e.g., w-[37%]), but use these sparingly to maintain design system integrity.
Does w-full include padding?
By default, Tailwind resets box-sizing to border-box. This means padding and borders are included in the element's width, preventing layout breakage.
Recap

We've covered the power of the spacing scale, the distinction between padding and margin, and how to define element dimensions. These tools provide the skeleton for every component you will build in this course.
Up next: Project Setup and Hero Structure
Work with me

Next.js Full-Stack Web App Development
A fast, SEO-ready full-stack web app built with Next.js 16 — from idea to deployed product, by an engineer who ships to production.

Next.js Website & Landing Page Development
A blazing-fast, SEO-optimized website or landing page in Next.js — the kind that loads instantly and ranks. Design-to-code, done right.
