0Pricing
Next.js 15 Fullstack Web Apps · 课时

集成 Tailwind CSS

设置并使用 Tailwind CSS,通过实用优先的类快速开发用户界面。

集成 Tailwind CSS 是 CoddyKit 上的免费 Next.js 15 Fullstack Web Apps 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 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-500 for text color or p-4 for 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 autoprefixer

Step 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 -p

Step 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.js to 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 导师)并解锁 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 导师会在你学习这节课的过程中回答你的问题。

学习 Next.js 15 Fullstack Web Apps 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Next.js 15 Fullstack Web Apps 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。

「集成 Tailwind CSS」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Next.js 15 Fullstack Web Apps 课中编写并运行代码吗?

能。每节 Next.js 15 Fullstack Web Apps 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. CSS 模块与全局样式
  2. 集成 Tailwind CSS
  3. 组件库与主题
  4. 响应式设计与深色模式
← 返回 Next.js 15 Fullstack Web Apps