มาตราส่วนระยะห่างของ Tailwind
ทำความเข้าใจมาตราส่วนระยะห่างเริ่มต้นของ Tailwind (0, 0.5, 1, 2, ... 96) และการจับคู่กับค่า rem เพื่อให้กำหนดระยะห่างได้อย่างคาดเดาได้
มาตราส่วนระยะห่างของ Tailwind เป็นบทเรียน Tailwind CSS Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Tailwind CSS Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Tailwind CSS Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
What Is a Spacing Scale?
A spacing scale is a set of predefined size values used consistently across a design system for margins, padding, gaps, widths, and heights. Instead of using arbitrary pixel values like 13px or 17px, you choose from a curated set of sizes. This constraint produces visually harmonious designs because all spacing values are multiples of a base unit, creating natural alignment and rhythm throughout the UI.
Tailwind's Default Scale
Tailwind's spacing scale is based on a 4px (0.25rem) base unit. Each step number multiplies by 0.25rem: spacing-1 = 4px, spacing-2 = 8px, spacing-4 = 16px, spacing-8 = 32px, spacing-16 = 64px. The scale runs from 0 to 96 (384px), with fractional steps at 0.5 (2px), 1.5 (6px), 2.5 (10px), and 3.5 (14px) for fine-grained control at small sizes.
/* Tailwind spacing reference:
0 = 0px
0.5 = 2px
1 = 4px
2 = 8px
3 = 12px
4 = 16px
6 = 24px
8 = 32px
12 = 48px
16 = 64px
24 = 96px
32 = 128px
48 = 192px
64 = 256px
*/Why 4px Increments?
The 4px base unit is the de facto standard in UI design systems, adopted by Google's Material Design, Apple's HIG, and most major design tools. Designers and developers working on the same system naturally align: a designer specifying 16px maps directly to Tailwind's 4 (4 × 4px = 16px). This shared language eliminates the translation error between design files and code.
<!-- Common spacing values and their Tailwind class -->
<!-- 4px → p-1 or m-1 -->
<!-- 8px → p-2 or m-2 -->
<!-- 12px → p-3 or m-3 -->
<!-- 16px → p-4 or m-4 -->
<!-- 24px → p-6 or m-6 -->
<!-- 32px → p-8 or m-8 -->Using the Scale for Padding
The most common use of the spacing scale is padding. p-4 adds 16px of padding on all four sides. This is the standard padding for compact UI elements like badges and small cards. For comfortable cards, p-6 (24px) works well. For spacious section containers, p-8 to p-12 gives generous breathing room.
<span class="bg-gray-100 p-1 rounded text-xs">Dense (p-1)</span>
<div class="bg-white border p-4 rounded-lg mt-2">Standard card (p-4)</div>
<section class="bg-gray-50 p-8 mt-2">Spacious section (p-8)</section>Applying Scale Values for Layout
The same spacing scale applies to many CSS properties beyond margin and padding. gap-4 adds a 16px gap between flex or grid children. w-8 sets a width of 32px. h-16 sets a height of 64px. top-4 positions an absolutely positioned element 16px from the top. The unified scale means all these values automatically align with each other visually.
<!-- Icon with consistent sizing from the scale -->
<div class="flex items-center gap-3">
<div class="w-10 h-10 bg-indigo-100 rounded-lg flex items-center justify-center">
<!-- Icon: 40px x 40px -->
</div>
<span class="font-medium text-gray-900">Feature Name</span>
</div>Fractional Steps for Fine Control
When you need smaller adjustments than 4px, Tailwind's fractional steps provide precision without arbitrary values. p-0.5 (2px) is useful for tight inner padding on small tags and badges. p-1.5 (6px) fits between compact and standard padding. p-2.5 (10px) gives a slightly larger compact feel. These fractional sizes keep spacing within the design system.
<span class="bg-indigo-100 text-indigo-700 text-xs font-medium px-2.5 py-0.5 rounded-full">
Badge with fractional padding
</span>Visualizing the Scale
A useful mental model: small interactions (icons, badges, input padding) use 1–4 (4–16px). Component-level spacing (card padding, button padding, form field gaps) uses 4–8 (16–32px). Section-level spacing (section padding, hero margins) uses 12–24 (48–96px). Page-level spacing (top-level container padding) uses 24–32+ (96–128px).
Extending the Scale
If you need a value not in the default scale, extend it in tailwind.config.js under theme.extend.spacing. Adding spacing['18'] creates a new 72px step available as p-18, m-18, w-18, etc. This keeps your additions within the design system rather than using arbitrary values scattered throughout your HTML.
// tailwind.config.js
module.exports = {
theme: {
extend: {
spacing: {
'18': '72px', // adds p-18, m-18, w-18, h-18, gap-18...
'128': '512px', // adds p-128, m-128, w-128, h-128...
},
},
},
};Arbitrary Spacing Values
For one-off spacing needs that do not warrant a config entry, use arbitrary values: p-[13px] or mt-[22px]. Arbitrary values accept any valid CSS length unit: px, rem, em, %, vw, vh. Use these sparingly for pixel-perfect edge cases — too many arbitrary values defeat the purpose of having a design system. If you use the same arbitrary value more than twice, add it to the config instead.
<!-- One-off spacing that doesn't fit the scale -->
<div class="pt-[68px]">
Accounts for a fixed 68px header
</div>Scale Consistency in Teams
One of the biggest benefits of using a predefined spacing scale is team consistency. When all developers on a project use Tailwind's spacing classes, every element naturally aligns with every other element. There are no more debates about whether to use 14px or 16px — you use the scale. Code reviews become easier because any value outside the standard steps is immediately visible and questionable.
The Scale in Responsive Design
The spacing scale works seamlessly with responsive prefixes. You can apply different spacing at different breakpoints: px-4 md:px-8 lg:px-16 gives a page container padding that doubles at each major breakpoint. This scaling pattern mirrors how professional designs usually handle whitespace — mobile is compact, desktop is generous.
<main class="px-4 md:px-8 lg:px-16 py-8 md:py-12 lg:py-24">
Page content scales gracefully across all devices.
</main>Quick Check
Test your understanding of Tailwind CSS Mastery concepts from this lesson.
Lesson Recap
In this lesson you learned: Tailwind's spacing scale is based on 4px (0.25rem) increments numbered 0 to 96, fractional steps like 0.5, 1.5, and 2.5 provide fine-grained control at small sizes, and the same scale applies uniformly to padding, margin, gap, width, and height utilities. Next up we master padding utilities and all their directional variants.
คำถามที่พบบ่อย
บทเรียน “มาตราส่วนระยะห่างของ Tailwind” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “มาตราส่วนระยะห่างของ Tailwind” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Tailwind CSS Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Tailwind CSS Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “มาตราส่วนระยะห่างของ Tailwind”
ทำความเข้าใจมาตราส่วนระยะห่างเริ่มต้นของ Tailwind (0, 0.5, 1, 2, ... 96) และการจับคู่กับค่า rem เพื่อให้กำหนดระยะห่างได้อย่างคาดเดาได้ คุณปฏิบัติ Tailwind CSS Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Tailwind CSS Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Tailwind CSS Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน
บทเรียน “มาตราส่วนระยะห่างของ Tailwind” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Tailwind CSS Academy นี้ได้ไหม
ได้ บทเรียน Tailwind CSS Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- มาตราส่วนระยะห่างของ Tailwind
- ยูทิลิตีระยะขอบด้านใน
- ยูทิลิตีระยะขอบด้านนอกและระยะขอบอัตโนมัติ
- ระยะขอบด้านนอกติดลบและช่องว่างระหว่างองค์ประกอบ