Tailwind CSS Academy · บทเรียน

โทเค็นพื้นฐานเทียบกับโทเค็นเชิงความหมาย

กำหนดโทเค็นพื้นฐาน เช่น color.blue.500 และโทเค็นเชิงความหมาย เช่น color.primary แล้วแมปโทเค็นเหล่านั้นในคอนฟิกของ Tailwind เพื่อให้ปรับธีมได้อย่างยืดหยุ่น

บทเรียน 1 จาก 413 ขั้นตอน

โทเค็นพื้นฐานเทียบกับโทเค็นเชิงความหมาย เป็นบทเรียน Tailwind CSS Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Tailwind CSS Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Tailwind CSS Academy มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

What Are Design Tokens?

Design tokens are named values that store the visual decisions of a design system — colors, spacing, font sizes, and more. Instead of hardcoding #3b82f6 throughout your CSS, you give it a name and reference that name everywhere. This creates a single source of truth that is easy to update globally. Tailwind's config is a natural home for design tokens because it generates utility classes from those named values.

Primitive Tokens Defined

Primitive tokens are raw, descriptive values with no semantic meaning attached. They describe what a value is, not where it is used. Examples include color.blue.500, spacing.4, or fontSize.lg. Tailwind's default color palette is built entirely from primitives — blue-500, red-300, and so on. You reference primitives to build the higher-level semantic layer.

// Primitive tokens in tailwind.config.js
module.exports = {
  theme: {
    extend: {
      colors: {
        'blue-brand': '#3b82f6',
        'blue-brand-dark': '#1d4ed8',
        'red-danger': '#ef4444',
        'green-success': '#22c55e'
      }
    }
  }
}

Semantic Tokens Defined

Semantic tokens describe the purpose of a value, not its raw appearance. Instead of blue-500, you would define color.primary. Instead of red-500, you would define color.danger. Semantic tokens make your codebase intent-driven — a developer reading text-primary immediately understands it is the main brand color, regardless of what that color actually is. This abstraction is what enables easy theming and rebanding.

// Semantic tokens built on top of primitives
module.exports = {
  theme: {
    extend: {
      colors: {
        primary: '#3b82f6',      // maps to blue-brand primitive
        'primary-hover': '#1d4ed8',
        danger: '#ef4444',       // maps to red-danger primitive
        success: '#22c55e'       // maps to green-success primitive
      }
    }
  }
}

Two-Layer Token Architecture

A robust token system uses two layers: primitives at the bottom and semantics on top. The primitive layer stores every raw value your design might ever use. The semantic layer maps those primitives to purpose-driven names. When your brand color changes, you update the primitive once, and every semantic token that references it inherits the change automatically. This separation of concerns is central to scalable design systems.

// Two-layer architecture
const primitives = {
  blue500: '#3b82f6',
  blue700: '#1d4ed8',
  gray900: '#111827'
};

// tailwind.config.js semantic layer
module.exports = {
  theme: {
    extend: {
      colors: {
        primary: primitives.blue500,
        'primary-dark': primitives.blue700,
        'text-base': primitives.gray900
      }
    }
  }
}

Using Semantic Tokens in Markup

Once semantic tokens are registered in your Tailwind config, they become available as utility classes across all color-accepting utilities. You can use bg-primary, text-primary, border-primary, and ring-primary — all referring to the same semantic value. This means changing the primary color in your config instantly updates every element in your UI that uses these semantic utilities.

<button class="bg-primary hover:bg-primary-dark text-white px-4 py-2 rounded-lg">
  Save Changes
</button>

<span class="text-danger font-medium">Error: field required</span>

<div class="border border-success bg-success/10 p-3 rounded">
  Successfully saved!
</div>

Naming Conventions for Tokens

Good token naming follows a consistent pattern that communicates role and scale. A common convention uses category.role.modifier — for example color.action.default, color.action.hover, and color.action.disabled. Avoid overly specific names like submit-button-blue — they break the moment you want to reuse the token elsewhere. Choose names that describe intent and scale, not visual implementation.

// Good naming: intent-based
colors: {
  'action-default': '#3b82f6',
  'action-hover': '#1d4ed8',
  'action-disabled': '#93c5fd',
  'surface-base': '#ffffff',
  'surface-raised': '#f8fafc',
  'text-primary': '#111827',
  'text-muted': '#6b7280'
}

// Avoid: too specific or too abstract
// 'btn-blue', 'thing1', 'color7'

Mapping Primitives to Semantics

The cleanest way to maintain the two-layer architecture is to define your primitives as JavaScript constants in a separate file and import them into your Tailwind config. This keeps primitives readable and reusable while the config itself only contains semantic mappings. When a designer changes the brand palette, only the primitives file needs to be updated — the semantic mapping stays intact.

// tokens/primitives.js
module.exports = {
  blue50: '#eff6ff',
  blue500: '#3b82f6',
  blue700: '#1d4ed8',
  rose500: '#f43f5e',
  gray100: '#f3f4f6',
  gray900: '#111827'
};

// tailwind.config.js
const p = require('./tokens/primitives');
module.exports = {
  theme: {
    extend: {
      colors: {
        primary: p.blue500,
        'primary-hover': p.blue700,
        'primary-light': p.blue50,
        danger: p.rose500
      }
    }
  }
}

Semantic Spacing and Typography Tokens

Tokens are not limited to colors. You can define semantic spacing and typography tokens too. Instead of p-4, a semantic spacing token named card-padding communicates what the spacing is for. In Tailwind, you add these under theme.extend.spacing and theme.extend.fontSize. This ensures UI consistency: every card in the app uses the same p-card padding, and that padding only needs to be adjusted once.

module.exports = {
  theme: {
    extend: {
      spacing: {
        'card': '1.5rem',     // p-card = 24px
        'section': '5rem',    // py-section = 80px
        'nav-height': '4rem'  // h-nav-height
      },
      fontSize: {
        'hero': ['3.5rem', { lineHeight: '1.1' }],
        'body': ['1rem', { lineHeight: '1.6' }]
      }
    }
  }
}

Tokens for Interactive States

Defining tokens for interactive states is equally important. A primary button needs tokens for its default, hover, active, and disabled appearances. Without semantic tokens, each developer might pick slightly different shades for hover, creating visual inconsistency. With semantic tokens like action-hover and action-disabled, every button in the system responds identically to user interaction.

<button
  class="bg-action-default hover:bg-action-hover active:bg-action-active
         disabled:bg-action-disabled disabled:cursor-not-allowed
         text-white px-5 py-2.5 rounded-lg transition-colors"
>
  Submit
</button>

<!-- tailwind.config.js colors:
  action-default: '#3b82f6'
  action-hover:   '#1d4ed8'
  action-active:  '#1e40af'
  action-disabled:'#93c5fd'
-->

Tokens Across Light and Dark Mode

Semantic tokens shine brightest when combined with dark mode. A primitive token like blue-500 has a fixed value, but a semantic token like primary can map to different primitives in light and dark mode. Using CSS variables as the bridge allows runtime switching without regenerating CSS. This technique is explored further in the theming module, but understanding that semantic tokens enable it is a key architectural insight.

/* global.css */
:root {
  --color-primary: #3b82f6;
  --color-surface: #ffffff;
  --color-text: #111827;
}

.dark {
  --color-primary: #60a5fa;
  --color-surface: #1f2937;
  --color-text: #f9fafb;
}

/* tailwind.config.js */
colors: {
  primary: 'var(--color-primary)',
  surface: 'var(--color-surface)',
  'text-base': 'var(--color-text)'
}

Token Audit and Maintenance

As a design system grows, token audits become essential. An audit checks whether every token is actively used, whether any tokens have drifted from their documented purpose, and whether new patterns have emerged that need a new token. Tools like grep or dedicated design token managers can scan your codebase for unused tokens. Removing unused tokens keeps the config lean and reduces cognitive load for new developers joining the team.

// Find tokens in config not used in markup
// (pseudocode — run as a build-time lint check)
const definedTokens = Object.keys(theme.colors);
const usedTokens = scanHTMLFiles(srcDir)
  .flatMap(file => extractTailwindClasses(file))
  .map(cls => cls.replace(/^(bg|text|border|ring)-/, ''));

const unusedTokens = definedTokens
  .filter(t => !usedTokens.includes(t));

console.log('Unused tokens:', unusedTokens);

Quick Check

Test your understanding of Tailwind CSS Mastery concepts from this lesson.

Lesson Recap

In this lesson you learned: primitive tokens are raw named values like color.blue.500, semantic tokens map those primitives to intent-driven names like color.primary, and two-layer architecture separates what a value is from where it is used. Next up we explore how CSS variables bridge Tailwind tokens with runtime theme switching.

เริ่มต้นได้ฟรี

เรียนรู้ HTML ด้วย AI tutor — ฟรี

เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป

คอร์ส
30
บทเรียน
120

คำถามที่พบบ่อย

บทเรียน “โทเค็นพื้นฐานเทียบกับโทเค็นเชิงความหมาย” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “โทเค็นพื้นฐานเทียบกับโทเค็นเชิงความหมาย” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Tailwind CSS Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Tailwind CSS Academy มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “โทเค็นพื้นฐานเทียบกับโทเค็นเชิงความหมาย”

กำหนดโทเค็นพื้นฐาน เช่น color.blue.500 และโทเค็นเชิงความหมาย เช่น color.primary แล้วแมปโทเค็นเหล่านั้นในคอนฟิกของ Tailwind เพื่อให้ปรับธีมได้อย่างยืดหยุ่น คุณปฏิบัติ Tailwind CSS Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Tailwind CSS Academy หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Tailwind CSS Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน

บทเรียน “โทเค็นพื้นฐานเทียบกับโทเค็นเชิงความหมาย” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Tailwind CSS Academy นี้ได้ไหม

ได้ บทเรียน Tailwind CSS Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. โทเค็นพื้นฐานเทียบกับโทเค็นเชิงความหมาย
  2. การสร้างธีมด้วยตัวแปร CSS
  3. กลยุทธ์การสร้างธีมหลายแบรนด์
  4. เอกสารและการกำกับดูแลโทเค็น
← กลับไปที่ Tailwind CSS Academy