Tailwind CSS Academy · 강의

사용자 지정 간격 값

기본 척도에서 부족한 부분을 채우도록 spacing.18이나 spacing.128과 같은 프로젝트별 간격 값을 테마에 추가합니다.

레슨 3/413개 단계

사용자 지정 간격 값은(는) CoddyKit의 무료 Tailwind CSS Academy 강의입니다. 이것은 4개 중 3번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 Tailwind CSS Academy 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. Tailwind CSS Academy 강의에는 총 4개의 강의가 포함되어 있습니다.

이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.

Tailwind's Default Spacing Scale

Tailwind's default spacing scale covers values from 0 to 96 (0 to 24rem in 0.25rem increments for small values, then larger steps). This scale is used consistently across margin, padding, width, height, gap, and positioning utilities. The values are designed to cover the vast majority of UI spacing needs, but real-world designs sometimes require values that fall between or beyond the defaults.

/* Tailwind default spacing examples */
spacing.0   = 0px
spacing.0.5 = 2px
spacing.1   = 4px
spacing.2   = 8px
spacing.4   = 16px
spacing.8   = 32px
spacing.16  = 64px
spacing.32  = 128px
spacing.64  = 256px
spacing.96  = 384px

Why Custom Spacing Is Needed

Design systems often require spacing values outside the default scale — a 128 value (32rem) for full-page hero sections, a 4.5 value (18px) for a specific element that sits between the standard 4 (16px) and 5 (20px), or a 13 value for a specific grid column width. Rather than using arbitrary values with bracket notation every time, add these values once to the config and give them a clean utility class name.

Adding Custom Spacing in Config

Add custom spacing values inside theme.extend.spacing. The key becomes the suffix in all spacing utilities, and the value is the CSS measurement you want. Use string keys for decimal or large numbers to ensure they are interpreted correctly. Once added, your custom spacing works automatically across m-*, p-*, w-*, h-*, gap-*, inset-*, and all other spacing utilities.

// tailwind.config.js
module.exports = {
  theme: {
    extend: {
      spacing: {
        '4.5':  '1.125rem', // 18px — between default 4 and 5
        '13':   '3.25rem',  // 52px
        '128':  '32rem',    // 512px — for large hero sections
        '144':  '36rem',    // 576px
        '160':  '40rem',    // 640px
      },
    },
  },
};

Using Custom Spacing Utilities

After adding custom spacing values, use them exactly like built-in spacing utilities. The key name goes after the utility prefix and hyphen. Custom spacing works with responsive prefixes, dark mode, and state variants just like defaults. The cross-utility nature means you write the config once and get padding, margin, width, height, and gap utilities from that single definition.

<!-- Using custom spacing values -->
<div class="p-4.5 m-13">   <!-- 18px padding, 52px margin -->
  Custom spacing in padding and margin
</div>

<section class="min-h-128">    <!-- 512px minimum height -->
  Full hero section
</section>

<div class="grid grid-cols-3 gap-4.5">
  <!-- 18px gap between grid items -->
</div>

Overriding the Full Spacing Scale

For projects with strict design systems that must use only specific spacing values, override the entire spacing scale by placing your values directly in theme.spacing (outside extend). This ensures no developer accidentally uses a spacing value outside your approved set. Be conservative with this approach — eliminating Tailwind's defaults removes many commonly needed utility classes.

// Strict spacing system: only these values are available
module.exports = {
  theme: {
    spacing: {
      '0':  '0px',
      '1':  '4px',
      '2':  '8px',
      '3':  '12px',
      '4':  '16px',
      '6':  '24px',
      '8':  '32px',
      '12': '48px',
      '16': '64px',
      '24': '96px',
    },
  },
};

Fractional and Decimal Spacing Keys

Tailwind handles fractional keys like '4.5' by generating utility classes with a CSS-safe version of the decimal. The class name uses the fraction as written: p-4.5, m-4.5. For keys with slashes (like fractions for percentage-based spacing), the slash is encoded in the CSS selector. Always quote decimal and fraction keys as strings in the config object to prevent JavaScript from interpreting them as numeric literals.

// tailwind.config.js
spacing: {
  '0.5':  '2px',   // p-0.5, m-0.5 (already in defaults)
  '1.5':  '6px',   // p-1.5, m-1.5 (already in defaults)
  '4.5':  '1.125rem', // CUSTOM: p-4.5
  '13':   '3.25rem',  // CUSTOM: p-13
  '18':   '4.5rem',   // CUSTOM: p-18
  '22':   '5.5rem',   // CUSTOM: p-22
}

Negative Spacing Values

Tailwind automatically generates negative variants for all spacing values, both default and custom. If you add '128': '32rem' to your spacing config, you also get -m-128, -mt-128, -inset-128, etc. Negative values are generated automatically — no separate config entry is needed. They are especially useful for overlapping elements, pull quotes, and decorative positioning.

<!-- Negative spacing pulls elements outside their container -->
<div class="relative">
  <div class="bg-blue-100 p-8">
    Main content
    <!-- This element extends 4px outside the parent top -->
    <span class="absolute -top-1 -right-1 bg-red-500 text-white text-xs rounded-full px-1.5 py-0.5">
      NEW
    </span>
  </div>
</div>

Spacing Relative to Font Size

Sometimes you want spacing values that scale with text size rather than being fixed rem values. Add spacing values using em units to the config — these will be relative to the element's own font size. For example, a button with px-1.5em padding would have proportional horizontal padding regardless of whether the button text is small or large.

// tailwind.config.js
spacing: {
  // em-based spacing (relative to font-size)
  '0.25em': '0.25em',
  '0.5em':  '0.5em',
  '0.75em': '0.75em',
  '1em':    '1em',
  '1.25em': '1.25em',
  '1.5em':  '1.5em',
  '2em':    '2em',
}

Spacing for Design System Grids

When your design is based on an 8px grid system (or 4px base unit), map the entire grid to your spacing config. An 8px grid means all spacing is multiples of 8px: 8, 16, 24, 32, 40, 48, 64, 80, 96, 128. Tailwind's default spacing scale already aligns with this (1 unit = 4px, so 2 = 8px, 4 = 16px, 6 = 24px, 8 = 32px). You may only need to extend with values above 96.

// 8px grid — Tailwind already aligns, just extend for larger values
spacing: {
  // Already in defaults (4px base unit):
  // 2=8px, 4=16px, 6=24px, 8=32px, 10=40px, 12=48px, 16=64px, 20=80px, 24=96px

  // Extend for larger sections
  '112': '28rem',  // 448px (= 56 units of 8px)
  '128': '32rem',  // 512px
  '160': '40rem',  // 640px
  '192': '48rem',  // 768px
  '224': '56rem',  // 896px
  '256': '64rem',  // 1024px
}

Arbitrary Values as an Alternative

When you need a one-off spacing value and do not want to pollute your config, use Tailwind's arbitrary value syntax with square brackets: p-[22px], w-[347px], mt-[3.75rem]. This generates the exact CSS value without adding to the config. Arbitrary values are ideal for one-time overrides but should not become a habit — if you use the same value three or more times, add it to the config instead.

<!-- Arbitrary values for truly one-off sizes -->
<div class="p-[22px] w-[347px] mt-[3.75rem]">
  One-off dimensions from design spec
</div>

<!-- Prefer config-based values for repeated use -->
<!-- BAD: p-[22px] used in 15 components -->
<!-- GOOD: Add '22': '1.375rem' to spacing config, use p-22 everywhere -->

Documenting Your Spacing System

As your custom spacing grows, document it alongside your color and typography tokens. Create a simple visual spacing reference in your Storybook, design system docs, or even a static HTML page that shows each spacing value as a colored block. This helps designers and developers use the right value from the config rather than reaching for arbitrary values or inconsistent one-offs.

<!-- Simple spacing token preview -->
<div class="space-y-2">
  <div class="flex items-center gap-4">
    <div class="bg-blue-200 h-4" style="width: 1.125rem;"></div>
    <span class="text-sm font-mono">spacing.4.5 = 1.125rem (18px)</span>
  </div>
  <div class="flex items-center gap-4">
    <div class="bg-blue-400 h-4" style="width: 3.25rem;"></div>
    <span class="text-sm font-mono">spacing.13 = 3.25rem (52px)</span>
  </div>
</div>

Quick Check

Test your understanding of custom spacing values in Tailwind CSS.

Lesson Recap

In this lesson you learned: add custom spacing in theme.extend.spacing to extend the default scale without replacing it, custom spacing works automatically across all spacing utilities (padding, margin, width, height, gap), and arbitrary values with bracket notation are a good alternative for truly one-off measurements. Next up we explore CSS custom properties as design tokens in Tailwind.

무료로 시작

AI 튜터와 함께 HTML을(를) 배우세요 — 무료

브라우저에서 실제 코드를 작성하고 실행하며, 24/7 AI 튜터로부터 즉각적인 도움을 받고, 웹이나 앱에서 중단한 부분부터 계속 학습하세요.

코스
30
레슨
120

자주 묻는 질문

“사용자 지정 간격 값” 강의는 무료인가요?

네 — “사용자 지정 간격 값” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Tailwind CSS Academy 강의 전체를 잠금 해제할 수 있습니다. Tailwind CSS Academy 강의에는 총 4개의 강의가 포함되어 있습니다.

“사용자 지정 간격 값”에서 뭘 배우나요?

기본 척도에서 부족한 부분을 채우도록 spacing.18이나 spacing.128과 같은 프로젝트별 간격 값을 테마에 추가합니다. 브라우저에서 직접 실행하는 실습 코드로 Tailwind CSS Academy을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.

Tailwind CSS Academy을(를) 시작하는 데 경험이 필요한가요?

사전 경험은 필요하지 않습니다. CoddyKit의 Tailwind CSS Academy은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 3번째 강의입니다.

“사용자 지정 간격 값” 강의는 얼마나 걸리나요?

대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.

이 Tailwind CSS Academy 강의에서 코드를 작성하고 실행할 수 있나요?

네. 모든 Tailwind CSS Academy 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.

이 강의의 모든 강의

  1. 사용자 지정 색상 추가
  2. 사용자 지정 글꼴 모음
  3. 사용자 지정 간격 값
  4. Tailwind에서 CSS 변수 사용하기
← Tailwind CSS Academy(으)로 돌아가기