0Pricing
Tailwind CSS Academy · บทเรียน

การติดตั้งและกำหนดค่า Tailwind

ตั้งค่า Tailwind CSS ผ่าน CDN และผ่าน npm ด้วย CLI แล้วดูหน้า HTML ที่ตกแต่งรูปแบบหน้าแรกของคุณปรากฏขึ้นจริง

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

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

Two Ways to Add Tailwind

Two ways to add Tailwind: a CDN script tag for quick prototyping, or the npm CLI for production, which builds a tiny optimized CSS file.

<!-- CDN approach (prototyping only) -->
<script src="https://cdn.tailwindcss.com"></script>

Installing Tailwind via npm

For production, install via npm: tailwindcss, postcss, and autoprefixer. Then run npx tailwindcss init -p to create both config files at once.

npm install -D tailwindcss postcss autoprefixer
npx tailwindcss init -p

The tailwind.config.js File

The tailwind.config.js file has one key field to set first: content. List every file that uses Tailwind classes, or those classes get left out of your build.

// tailwind.config.js
module.exports = {
  content: [
    './index.html',
    './src/**/*.{html,js,jsx,ts,tsx,vue,svelte}',
  ],
  theme: {
    extend: {},
  },
  plugins: [],
};

The PostCSS Config File

Tailwind runs as a PostCSS plugin. The postcss.config.js file tells your bundler to run it. Most tools pick it up automatically — you rarely touch it again.

// postcss.config.js
module.exports = {
  plugins: {
    tailwindcss: {},
    autoprefixer: {},
  },
};

Creating Your Main CSS File

Make a CSS entry file and add the three @tailwind directives: base, components, and utilities. At build time they expand into Tailwind generated styles.

/* src/input.css */
@tailwind base;
@tailwind components;
@tailwind utilities;

Running the Tailwind CLI

The Tailwind CLI watches your files and rebuilds on save. Point --input and --output at your files, add --watch for dev, and --minify for production.

# Development (with watch)
npx tailwindcss -i ./src/input.css -o ./dist/output.css --watch

# Production (minified)
npx tailwindcss -i ./src/input.css -o ./dist/output.css --minify

Linking the CSS in HTML

Now link the generated CSS in your HTML like any stylesheet. Point href at the output file, not the input, and every utility class starts working.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <link href="./dist/output.css" rel="stylesheet" />
  <title>My Tailwind Site</title>
</head>
<body>
  <h1 class="text-3xl font-bold text-blue-600">Hello Tailwind!</h1>
</body>
</html>

Tailwind With Vite

Using Vite? Tailwind plugs in through PostCSS. Set up the configs, import your CSS in main.js, and Vite handles the rest with instant hot reloading.

// main.js (Vite entry point)
import './src/input.css';

// Then run:
npm run dev

Tailwind With Create React App

On Create React App, install the packages, init the config, and import index.css with the directives. CRA already runs PostCSS, so just restart the server.

# 1. Install dependencies
npm install -D tailwindcss postcss autoprefixer
npx tailwindcss init -p

# 2. Add directives to src/index.css
# @tailwind base;
# @tailwind components;
# @tailwind utilities;

# 3. Start dev server
npm start

Tailwind Preflight: The CSS Reset

The @tailwind base line injects Preflight, a gentle reset. It clears default margins, sets box-sizing everywhere, and gives you a clean, consistent baseline.

/* Preflight effects (auto-applied): */
/* - All elements: box-sizing: border-box */
/* - Body: margin: 0 */
/* - h1-h6: font-size/weight reset to body defaults */
/* - ul/ol: list-style: none; margin: 0; padding: 0 */

Verifying Your Installation

To verify it works, add text-3xl font-bold text-indigo-600 to a heading. Big, bold, indigo? You are set. If not, check your content paths in the config.

<!-- Verification test -->
<h1 class="text-3xl font-bold text-indigo-600">
  If this is big, bold, and purple — Tailwind is working!
</h1>

Quick Check

Quick check! Make sure setup clicked. 🛠️

Lesson Recap

Great! Add Tailwind via CDN or npm, set content paths so classes ship, and add the three @tailwind directives. Next up: building your first page.

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

บทเรียน “การติดตั้งและกำหนดค่า Tailwind” ฟรีหรือไม่

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

คุณจะเรียนรู้อะไรในบทเรียน “การติดตั้งและกำหนดค่า Tailwind”

ตั้งค่า Tailwind CSS ผ่าน CDN และผ่าน npm ด้วย CLI แล้วดูหน้า HTML ที่ตกแต่งรูปแบบหน้าแรกของคุณปรากฏขึ้นจริง คุณปฏิบัติ Tailwind CSS Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

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

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

บทเรียน “การติดตั้งและกำหนดค่า Tailwind” ใช้เวลานานแค่ไหน

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

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

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

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

  1. อธิบาย CSS แบบยูทิลิตีเป็นหลัก
  2. การติดตั้งและกำหนดค่า Tailwind
  3. หน้า Tailwind แรกของคุณ
  4. Tailwind เทียบกับ CSS แบบดั้งเดิม
← กลับไปที่ Tailwind CSS Academy