0Pricing
Tailwind CSS Academy · 강의

레이어를 사용한 사용자 지정 CSS 구성

@layer base, @layer components, @layer utilities를 사용해 우선순위를 조절하고 사용자 지정 스타일이 Tailwind의 출력에 올바르게 통합되도록 합니다.

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

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

The CSS Cascade Challenge

When mixing Tailwind utility classes with custom CSS, specificity conflicts can emerge. A custom CSS rule like .btn { color: red; } might override a Tailwind utility text-blue-500 due to source order, even though you intended the utility to win. Tailwind's @layer directive solves this by giving you explicit control over where in the CSS cascade your custom styles are inserted.

Tailwind's Three Layers

Tailwind organizes all generated CSS into three ordered layers: base comes first and contains element-level resets and defaults. components comes second and is designed for multi-utility class abstractions. utilities comes last and contains all single-purpose utility classes. This order ensures utilities always win over component styles, which always win over base styles — a predictable and intentional hierarchy.

/* Order in final CSS output: */
/* 1. @layer base (element defaults) */
/* 2. @layer components (component classes) */
/* 3. @layer utilities (utility classes) */

/* Your main CSS entry file */
@tailwind base;        /* injects base layer */
@tailwind components;  /* injects components layer */
@tailwind utilities;   /* injects utilities layer */

Using @layer base

The base layer is the right place for element-level resets, default HTML element styles, and global styles that apply unconditionally. Common uses include setting a default body font, establishing box-sizing rules, resetting heading margins, and defining link styles. Styles in the base layer can be overridden by anything in the components or utilities layers.

@layer base {
  *,
  *::before,
  *::after {
    box-sizing: border-box;
  }

  body {
    @apply font-sans antialiased text-gray-900 bg-white;
  }

  h1 { @apply text-3xl font-bold tracking-tight; }
  h2 { @apply text-2xl font-semibold tracking-tight; }
  h3 { @apply text-xl font-semibold; }

  a {
    @apply text-blue-600 hover:underline;
  }

  img {
    max-width: 100%;
    height: auto;
  }
}

Using @layer components

The components layer is for extracting repeated utility combinations into named classes using @apply. Component classes defined here can be overridden by utility classes applied in HTML because utilities live in the later utilities layer. This makes your component classes flexible — they set sensible defaults that can always be customized per-instance with utilities.

@layer components {
  .card {
    @apply bg-white rounded-xl shadow-sm border border-gray-200 overflow-hidden;
  }

  .btn {
    @apply inline-flex items-center px-4 py-2 rounded-lg font-medium text-sm
           transition-colors duration-150 focus:outline-none focus:ring-2;
  }

  .input {
    @apply w-full border border-gray-300 rounded-lg px-4 py-2 text-sm
           focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent;
  }
}

Using @layer utilities

The utilities layer is for single-purpose custom utility classes that Tailwind does not provide out of the box. These might be utilities for CSS properties not yet in Tailwind's core, browser-specific prefixed properties, or composited values too specific for the default scale. Utilities in this layer have the highest priority and override both base and component styles.

@layer utilities {
  /* CSS text-wrap utilities (not in Tailwind core) */
  .text-balance { text-wrap: balance; }
  .text-pretty  { text-wrap: pretty; }

  /* Content visibility for performance */
  .content-auto {
    content-visibility: auto;
    contain-intrinsic-size: auto 500px;
  }

  /* Custom scrollbar hiding */
  .scrollbar-hide {
    -ms-overflow-style: none;
    scrollbar-width: none;
  }
  .scrollbar-hide::-webkit-scrollbar {
    display: none;
  }
}

Why Layer Placement Matters for Specificity

CSS specificity normally depends on selector weight. But the CSS Cascade Layers specification (which Tailwind leverages) overrides specificity: a rule in a later layer always wins over a rule in an earlier layer, regardless of selector complexity. This means a .utilities-layer class like .p-4 (single class) wins over a .components-layer class like .card.padded.extra (three classes) automatically.

@layer components {
  /* Even with 3 classes, this loses to any utility */
  .card.padded.extra {
    padding: 100px;  /* will be overridden by p-4 utility */
  }
}

@layer utilities {
  /* One class in utilities layer always wins */
  .p-4 { padding: 1rem; }  /* wins! */
}

<!-- In HTML: p-4 correctly applies because utilities layer > components layer -->
<div class="card padded extra p-4">Correct padding: 1rem</div>

Custom Layers Beyond Tailwind's Three

You are not limited to Tailwind's three default layers. You can define entirely custom named layers using CSS @layer and control where they appear in the cascade by declaring them in the correct order at the top of your CSS file. This is useful for integrating third-party CSS libraries at specific points in the cascade or for creating a layered architecture within your own CSS.

/* Declare layer order at the top of your CSS */
@layer reset, base, vendors, components, utilities, overrides;

/* Then fill in each layer */
@layer reset {
  * { box-sizing: border-box; margin: 0; }
}

@layer vendors {
  /* Third-party library styles here */
}

@layer overrides {
  /* Emergency overrides — highest specificity */
  .force-hidden { display: none !important; }
}

Styles Outside Any Layer

CSS written outside any @layer block has higher specificity than any layered style. This means custom CSS you write without wrapping it in @layer will always override Tailwind's layered utilities, potentially causing unexpected layout issues. Always wrap your custom styles in the appropriate layer — or explicitly create an overrides layer — to avoid fighting Tailwind's cascade.

/* CAUTION: This is outside any layer and overrides ALL layered styles */
.btn {
  color: red;  /* This will override dark:text-white and hover:text-blue-600! */
}

/* BETTER: Inside the appropriate layer */
@layer components {
  .btn {
    color: red;  /* Can be overridden by utilities like text-white */
  }
}

Combining @layer With @apply

The @layer and @apply directives work together. You define a layer, place component or utility classes inside it, and use @apply within those classes to compose from Tailwind utilities. This combination gives you the organizational benefits of layers (predictable cascade) and the convenience of @apply (composing from existing utilities) in a single pattern.

@layer base {
  /* Element defaults using @apply */
  body { @apply font-sans antialiased text-base leading-relaxed; }
}

@layer components {
  /* Component classes using @apply */
  .alert { @apply flex items-start gap-3 p-4 rounded-lg border text-sm; }
  .alert-error { @apply alert bg-red-50 border-red-200 text-red-800; }
}

@layer utilities {
  /* Custom utilities using plain CSS */
  .touch-action-none { touch-action: none; }
  .will-change-transform { will-change: transform; }
}

Structuring CSS Files With Layers

As your project grows, split your CSS across multiple files organized by layer purpose. Import everything into a single entry file. Keep base styles minimal — only things that truly apply globally. Keep components files organized by component type. Keep utilities files for genuinely missing utilities. This structure makes onboarding new developers easier and prevents CSS from becoming a maintenance burden.

/* src/styles/main.css — entry point */
@tailwind base;
@tailwind components;
@tailwind utilities;

/* Base layer additions */
@layer base {
  @import './base/typography.css';
  @import './base/reset.css';
}

/* Component layer */
@layer components {
  @import './components/buttons.css';
  @import './components/cards.css';
  @import './components/forms.css';
}

/* Utility layer additions */
@layer utilities {
  @import './utilities/layout.css';
  @import './utilities/animations.css';
}

Debugging Layer Issues

When a style is not applying as expected, open Chrome DevTools and look at the Styles panel. Crossed-out declarations indicate styles being overridden. If a utility class is being crossed out by your own component CSS, you likely placed it outside a layer (giving it higher cascade priority) or in the wrong layer. Moving it to @layer components should restore the correct utility-wins behavior.

Quick Check

Test your understanding of organizing custom CSS with Tailwind's @layer directive.

Lesson Recap

In this lesson you learned: Tailwind's three layers — base, components, utilities — form a cascade hierarchy where each later layer overrides earlier ones, @layer base is for element defaults, @layer components for extracted component classes, and @layer utilities for custom utility classes. Next up we explore the pitfalls of @apply and when to use component extraction instead.

자주 묻는 질문

“레이어를 사용한 사용자 지정 CSS 구성” 강의는 무료인가요?

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

“레이어를 사용한 사용자 지정 CSS 구성”에서 뭘 배우나요?

@layer base, @layer components, @layer utilities를 사용해 우선순위를 조절하고 사용자 지정 스타일이 Tailwind의 출력에 올바르게 통합되도록 합니다. 브라우저에서 직접 실행하는 실습 코드로 Tailwind CSS Academy을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.

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

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

“레이어를 사용한 사용자 지정 CSS 구성” 강의는 얼마나 걸리나요?

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

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

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

이 강의의 모든 강의

  1. @apply의 기능과 사용 시점
  2. 재사용 가능한 컴포넌트 클래스 만들기
  3. 레이어를 사용한 사용자 지정 CSS 구성
  4. @apply의 주의점과 대안
← Tailwind CSS Academy(으)로 돌아가기