Fri. Feb 28th, 2025
คู่มือการใช้งาน Tailwind CSS เบื้องต้นคู่มือการใช้งาน Tailwind CSS เบื้องต้น
Spread the love

Tailwind CSS เป็น CSS framework แบบ utility-first ที่ช่วยให้เราสามารถสร้าง UI ได้อย่างรวดเร็วโดยใช้ class ที่มีอยู่แล้ว ไม่จำเป็นต้องเขียน CSS เองมากนัก ในบทความนี้เราจะสอนการใช้งานเบื้องต้นพร้อมตัวอย่างโค้ด

1. การติดตั้ง Tailwind CSS

1.1 ติดตั้งผ่าน CDN (วิธีง่ายที่สุด)

เพิ่มโค้ดนี้ใน <head> ของไฟล์ HTML:

<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/tailwind.min.css" rel="stylesheet">

1.2 ติดตั้งผ่าน npm (สำหรับโปรเจกต์ที่ใช้ Node.js)

npm install -D tailwindcss
npx tailwindcss init

จากนั้นจะได้ไฟล์ tailwind.config.js สำหรับตั้งค่าการใช้งานเพิ่มเติม

2. โครงสร้าง HTML พร้อมใช้งาน Tailwind CSS

ตัวอย่างไฟล์ index.html แบบง่าย ๆ

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/tailwind.min.css" rel="stylesheet">
    <title>Tailwind CSS Example</title>
</head>
<body class="bg-gray-100 min-h-screen flex items-center justify-center">
    <div class="bg-white p-8 rounded-xl shadow-lg w-full max-w-md">
        <h1 class="text-2xl font-bold text-center mb-4">Welcome to Tailwind CSS</h1>
        <p class="text-gray-600 text-center">นี่คือหน้าตัวอย่างที่สร้างด้วย Tailwind CSS</p>
        <button class="mt-6 w-full bg-blue-500 text-white py-2 rounded hover:bg-blue-600">คลิกที่นี่</button>
    </div>
</body>
</html>

3. อธิบาย Class ที่ใช้

  • bg-gray-100 : พื้นหลังสีเทาอ่อน
  • min-h-screen : ความสูงอย่างน้อยเท่ากับหน้าจอทั้งหมด
  • flex items-center justify-center : จัดให้อยู่ตรงกลางแนวดิ่งและแนวนอน
  • bg-white p-8 rounded-xl shadow-lg : กล่องสีขาว มีขอบโค้งและเงา
  • text-2xl font-bold : ขนาดตัวอักษรใหญ่และหนา
  • hover:bg-blue-600 : เปลี่ยนสีปุ่มเมื่อชี้เมาส์

4. การใช้งาน Layout ด้วย Flexbox และ Grid

Flexbox Example

<div class="flex justify-between items-center p-4 bg-gray-200">
    <div>Logo</div>
    <div class="flex gap-4">
        <a href="#" class="text-blue-500">หน้าแรก</a>
        <a href="#" class="text-blue-500">เกี่ยวกับ</a>
        <a href="#" class="text-blue-500">ติดต่อ</a>
    </div>
</div>

Grid Example

<div class="grid grid-cols-3 gap-4 p-4">
    <div class="bg-red-300 p-4">Item 1</div>
    <div class="bg-green-300 p-4">Item 2</div>
    <div class="bg-blue-300 p-4">Item 3</div>
</div>

5. การปรับแต่งเพิ่มเติมใน tailwind.config.js

หากต้องการเพิ่มสีหรือขนาดใหม่ ให้แก้ไขไฟล์ tailwind.config.js

module.exports = {
    content: ['./*.html'],
    theme: {
        extend: {
            colors: {
                primary: '#1DA1F2',
                secondary: '#FFAD1F',
            },
        },
    },
    plugins: [],
}

6. สรุป

Tailwind CSS ช่วยให้การเขียน UI ง่ายและเร็วขึ้น โดยใช้ class ที่เตรียมไว้แล้ว แนะนำให้ฝึกใช้กับโปรเจกต์เล็ก ๆ เพื่อความคุ้นเคย หากต้องการเรียนรู้เพิ่มเติมสามารถดูเอกสารอย่างเป็นทางการได้ที่ https://tailwindcss.com/docs

Loading

By tikky

Related Post

Leave a Reply

Your email address will not be published. Required fields are marked *