0Pricing
Tailwind CSS Academy · 课时

安装与配置 Tailwind

通过 CDN 和使用 CLI 的 npm 设置 Tailwind CSS,并查看您的第一个带样式的 HTML 页面呈现出来。

安装与配置 Tailwind 是 CoddyKit 上的免费 Tailwind CSS Academy 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 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 导师)并解锁 Tailwind CSS Academy 课程的其余内容,请升级到 CoddyKit PRO。 Tailwind CSS Academy 课程共包含 4 节课。

「安装与配置 Tailwind」这节课中我会学到什么?

通过 CDN 和使用 CLI 的 npm 设置 Tailwind CSS,并查看您的第一个带样式的 HTML 页面呈现出来。 你通过在浏览器中直接运行的动手代码来练习 Tailwind CSS Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Tailwind CSS Academy 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Tailwind CSS Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 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