Skip to main content

เนื้อหาภาษาไทย / Thai content

System guide Content reviewed

การใช้ Tailwind CSS ร่วมกับ Next.js

บทความนี้อธิบายขั้นตอนการติดตั้งและใช้งาน Tailwind CSS ร่วมกับ Next.js เพื่อสร้าง UI ที่สวยงามและปรับแต่งได้ง่าย โดยเน้นที่การติดตั้ง, การ configure ไฟล์ต่างๆ,...

การใช้ Tailwind CSS ร่วมกับ Next.js

Next.js เป็น React framework ที่ได้รับความนิยมอย่างมาก ด้วยคุณสมบัติมากมาย เช่น Server-Side Rendering (SSR), Static Site Generation (SSG), และ API routes ทำให้เป็นตัวเลือกที่ยอดเยี่ยมสำหรับการสร้างเว็บแอปพลิเคชันที่รวดเร็วและมีประสิทธิภาพ การใช้ Tailwind CSS ร่วมกับ Next.js ช่วยให้คุณสร้าง UI ที่สวยงามและปรับแต่งได้อย่างง่ายดาย

การติดตั้ง Tailwind CSS ใน Next.js

การติดตั้ง Tailwind CSS ใน Next.js นั้นค่อนข้างตรงไปตรงมา โดยมีขั้นตอนหลักๆ ดังนี้:

  1. สร้าง Next.js project ใหม่ (ถ้ายังไม่มี):

    npx create-next-app my-tailwind-app
    cd my-tailwind-app
  2. ติดตั้ง Tailwind CSS และ PostCSS:

    npm install -D tailwindcss postcss autoprefixer

    หรือใช้ yarn:

    yarn add -D tailwindcss postcss autoprefixer
  3. สร้างไฟล์ tailwind.config.js และ postcss.config.js:

    npx tailwindcss init -p

    คำสั่งนี้จะสร้างไฟล์ tailwind.config.js และ postcss.config.js ใน root directory ของ project ของคุณ

การ Configure Tailwind CSS ใน Next.js

หลังจากติดตั้ง Tailwind CSS แล้ว เราต้อง configure ให้ Next.js รู้จัก Tailwind CSS และ PostCSS

  1. แก้ไข tailwind.config.js:

    ในไฟล์ tailwind.config.js ให้กำหนด paths ที่ Tailwind CSS จะใช้ในการค้นหา classes ที่ใช้งานในโปรเจกต์ของคุณ โดยทั่วไปจะรวมถึงไฟล์ JavaScript และ JSX ทั้งหมด

    /** @type {import('tailwindcss').Config} */
    module.exports = {
      content: [
        "./app/**/*.{js,ts,jsx,tsx,mdx}",
        "./pages/**/*.{js,ts,jsx,tsx,mdx}",
        "./components/**/*.{js,ts,jsx,tsx,mdx}",
        // Or if using `src` directory:
        "./src/**/*.{js,ts,jsx,tsx,mdx}",
      ],
      theme: {
        extend: {},
      },
      plugins: [],
    }
    
  2. แก้ไข postcss.config.js:

    ตรวจสอบให้แน่ใจว่า postcss.config.js มี Tailwind CSS และ autoprefixer เป็น plugins:

    module.exports = {
      plugins: {
        tailwindcss: {},
        autoprefixer: {},
      },
    }
    
  3. เพิ่ม Tailwind directives ใน Global CSS file:

    สร้างหรือแก้ไขไฟล์ CSS ส่วนกลาง (เช่น styles/globals.css หรือ app/globals.css) และเพิ่ม directives ของ Tailwind CSS:

    @tailwind base;
    @tailwind components;
    @tailwind utilities;
    

การใช้งาน Tailwind CSS ใน Components

เมื่อติดตั้งและ configure Tailwind CSS เรียบร้อยแล้ว คุณสามารถเริ่มใช้งาน Tailwind CSS classes ใน components ของคุณได้เลย

ตัวอย่าง:

สมมติว่าคุณมี component ชื่อ Button ในไฟล์ components/Button.js:

// components/Button.js
export default function Button({ children, onClick }) {
  return (
    <button
      onClick={onClick}
      className="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded"
    >
      {children}
    </button>
  );
}

ในตัวอย่างนี้ เราใช้ Tailwind CSS classes เช่น bg-blue-500 (background color), hover:bg-blue-700 (hover state background color), text-white (text color), font-bold (font weight), py-2 (padding top and bottom), px-4 (padding left and right), และ rounded (rounded corners) เพื่อจัดรูปแบบปุ่ม

การใช้งานใน Page Component:

คุณสามารถนำ component Button ไปใช้ใน page component ได้ ตัวอย่างเช่น ใน pages/index.js หรือ app/page.js:

// app/page.js (หรือ pages/index.js)
import Button from '../components/Button';

export default function Home() {
  return (
    <div className="flex items-center justify-center h-screen">
      <Button onClick={() => alert('Button clicked!')}>Click me</Button>
    </div>
  );
}

ในตัวอย่างนี้ เราใช้ Tailwind CSS classes flex, items-center, justify-center, และ h-screen เพื่อจัด layout ของหน้า

ข้อดีของการใช้ Tailwind CSS กับ Next.js

  • ความเร็วในการพัฒนา: Tailwind CSS ช่วยให้คุณสร้าง UI ได้อย่างรวดเร็วโดยไม่ต้องเขียน CSS เอง

  • ความสอดคล้อง: Tailwind CSS ช่วยให้มั่นใจได้ว่า UI ของคุณมีความสอดคล้องกันทั่วทั้งแอปพลิเคชัน

  • การปรับแต่ง: คุณสามารถปรับแต่ง Tailwind CSS ให้ตรงกับความต้องการของโปรเจกต์ของคุณได้

  • Performance: Next.js สามารถ optimize CSS ที่สร้างโดย Tailwind CSS เพื่อให้แอปพลิเคชันของคุณโหลดได้อย่างรวดเร็ว

ข้อควรระวัง

  • ไฟล์ CSS ขนาดใหญ่: ใน development environment ไฟล์ CSS อาจมีขนาดใหญ่ เนื่องจาก Tailwind CSS จะ generate classes ทั้งหมด อย่างไรก็ตาม ใน production build Next.js จะใช้ PurgeCSS เพื่อลบ classes ที่ไม่ได้ใช้ ทำให้ไฟล์ CSS มีขนาดเล็กลง

  • ความคุ้นเคยกับ Classes: การใช้งาน Tailwind CSS อย่างมีประสิทธิภาพต้องใช้ความคุ้นเคยกับ classes ต่างๆ แต่เมื่อคุณคุ้นเคยแล้ว จะช่วยให้คุณพัฒนา UI ได้อย่างรวดเร็ว

สรุป

การใช้ Tailwind CSS ร่วมกับ Next.js เป็นวิธีที่ยอดเยี่ยมในการสร้างเว็บแอปพลิเคชันที่สวยงาม รวดเร็ว และปรับแต่งได้ง่าย ด้วยขั้นตอนการติดตั้งและการ configure ที่ไม่ซับซ้อน คุณสามารถเริ่มใช้งาน Tailwind CSS ในโปรเจกต์ Next.js ของคุณได้ทันที

Office.in.th Custom Service

Want to build or adapt a system from this structure?

Office.in.th designs and develops enterprise web applications, API integrations, and public- and private-sector management systems, with long-term support.

See our software development service