การผสานรวม Tailwind CSS
ตั้งค่าและใช้งาน Tailwind CSS เพื่อพัฒนาส่วนติดต่อผู้ใช้อย่างรวดเร็วด้วยคลาสที่เน้นยูทิลิตี
การผสานรวม Tailwind CSS เป็นบทเรียน Next.js 15 Fullstack Web Apps ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Next.js 15 Fullstack Web Apps และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Next.js 15 Fullstack Web Apps มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Meet Tailwind CSS
Welcome to styling with Tailwind CSS! It's a popular utility-first CSS framework that helps you build custom designs quickly.
Instead of writing custom CSS for every element, you apply pre-defined utility classes directly in your HTML or JSX.
Utility-First Explained
What does 'utility-first' mean?
- Small, Single-Purpose Classes: Each class does one thing, like
text-blue-500for text color orp-4for padding. - Build Complex UIs: You combine many small utility classes to create unique designs.
- No Custom CSS Needed: Often, you won't need to write any custom CSS files, speeding up development.
Tailwind & Next.js: A Perfect Match
Tailwind CSS pairs wonderfully with Next.js:
- Component-Based: Easily apply classes directly to your React components.
- Zero Runtime: Tailwind generates static CSS at build time, meaning no extra JavaScript is shipped to the browser for styling.
- Purging: It automatically removes unused CSS, keeping your final bundle size small.
Step 1: Install Core Dependencies
First, let's add Tailwind CSS and its peer dependencies to your Next.js project. We install them as development dependencies.
tailwindcss: The core framework.postcss: A tool for transforming CSS.autoprefixer: A PostCSS plugin to add vendor prefixes to CSS rules.
npm install -D tailwindcss postcss autoprefixerStep 2: Initialize Tailwind Config
Next, generate your Tailwind configuration files. The -p flag creates both tailwind.config.js and postcss.config.js for you.
npx tailwindcss init -pStep 3: Configure Content Paths
Open tailwind.config.js. You need to tell Tailwind where to find your files (like pages, components) that will use Tailwind classes. This allows it to scan and generate only the necessary CSS.
/** @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}',
],
theme: {
extend: {},
},
plugins: [],
}Step 4: Add Tailwind Directives to CSS
Now, open your global CSS file (usually ./app/globals.css in the App Router). Add the @tailwind directives at the top. These inject Tailwind's base styles, components, and utility classes into your project.
@tailwind base;
@tailwind components;
@tailwind utilities;
/* Your custom global styles can go here */Using Utility Classes in JSX
With Tailwind set up, you can start applying classes directly to your elements. Notice how multiple classes combine to style the h1 element in this example.
// app/page.js or a component file
export default function HomePage() {
return (
<div className="min-h-screen flex items-center justify-center bg-gray-100">
<h1 className="text-4xl font-bold text-indigo-600 p-6 rounded-lg shadow-md bg-white">
Hello Tailwind!
</h1>
</div>
);
}Responsive Design Made Easy
Tailwind uses a mobile-first approach for responsive design. You can apply different styles at various breakpoints using prefixes like sm:, md:, and lg:.
p-4: Default padding (small screens).md:p-8: Padding for medium screens and up.lg:p-12: Padding for large screens and up.
// app/page.js or a component file
export default function ResponsivePage() {
return (
<div className="p-4 md:p-8 lg:p-12 bg-blue-100 md:bg-green-100 lg:bg-purple-100">
<p className="text-base md:text-lg lg:text-xl font-medium">
Watch me change on different screen sizes!
</p>
</div>
);
}Tailwind Integration Check
You've learned the essential steps to integrate Tailwind CSS into your Next.js project. Let's test your understanding!
Recap: Styling with Tailwind
Congratulations! You've successfully learned how to integrate Tailwind CSS into your Next.js application.
- Installation: Added Tailwind and PostCSS dependencies.
- Configuration: Initialized and configured
tailwind.config.jsto scan your files. - Integration: Included Tailwind's base styles in your global CSS.
- Usage: Started styling components with utility classes and understood responsive design.
Tailwind CSS empowers you to build beautiful, responsive UIs rapidly with a consistent design system.
คำถามที่พบบ่อย
บทเรียน “การผสานรวม Tailwind CSS” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การผสานรวม Tailwind CSS” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Next.js 15 Fullstack Web Apps ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Next.js 15 Fullstack Web Apps มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การผสานรวม Tailwind CSS”
ตั้งค่าและใช้งาน Tailwind CSS เพื่อพัฒนาส่วนติดต่อผู้ใช้อย่างรวดเร็วด้วยคลาสที่เน้นยูทิลิตี คุณปฏิบัติ Next.js 15 Fullstack Web Apps ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Next.js 15 Fullstack Web Apps หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Next.js 15 Fullstack Web Apps บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน
บทเรียน “การผสานรวม Tailwind CSS” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Next.js 15 Fullstack Web Apps นี้ได้ไหม
ได้ บทเรียน Next.js 15 Fullstack Web Apps ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- โมดูล CSS และสไตล์ส่วนกลาง
- การผสานรวม Tailwind CSS
- ไลบรารีคอมโพเนนต์และการกำหนดธีม
- การออกแบบแบบตอบสนองและโหมดมืด