Work & Development · Frontend Frameworks & TypeScript
Free preview
Set up a Tailwind CSS 4 project
Lesson 1 of Tailwind CSS 4: Utility-First Interfaces. Anyone can read this lesson with no account. Every lesson after this one is for students.
Utilities and CSS concepts
Install Tailwind, build a stylesheet, and explain how HTML classes become CSS.
Your goal: Install Tailwind, build a stylesheet, and explain how HTML classes become CSS.
Complete Responsive Websites with CSS first. A utility class is a short name for a small CSS rule. Think of labeled building blocks: p-6 adds padding and text-3xl sets a text size. Tailwind finds the class names written in your project and builds a stylesheet containing the needed rules. You still write HTML and use the CSS ideas you already learned. This course uses Tailwind 4 and shows the underlying CSS so you can check each translation.
What you need
- Node.js 22 or newer. Node runs JavaScript tools on your computer, and npm (installed with it) downloads packages. Install the LTS version from nodejs.org, then open a terminal and run
node --version. You should seev22or a higher number. - A terminal. On Windows use PowerShell or the terminal built into VS Code (View, then Terminal). On a Mac use Terminal or the VS Code terminal. VS Code's terminal opens in the folder you have open, which saves typing.
These instructions follow the official Tailwind CLI guide at tailwindcss.com/docs/installation/tailwind-cli. Tailwind 4 changed the setup from version 3, so if you search the web, check that a tutorial says v4. A v3 tutorial will mention tailwind.config.js and @tailwind base, which this course does not use.
Worked example
Create a new folder called resource-directory and open it in VS Code. In the terminal, run:
npm init -y
npm install --save-dev --save-exact tailwindcss@4.3.3 @tailwindcss/cli@4.3.3
mkdir src
npm init -y creates package.json, the file that records your project's packages. The install line adds two packages: tailwindcss itself and @tailwindcss/cli, the command-line tool that builds your CSS. --save-exact records exactly 4.3.3 so every lesson behaves the same on every computer.
Create src/input.css containing one line:
@import "tailwindcss";
Create src/index.html:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Resource directory practice</title>
<link rel="stylesheet" href="./output.css">
</head>
<body class="bg-slate-50 p-6 text-slate-900">
<main class="mx-auto max-w-3xl rounded-xl bg-white p-6">
<h1 class="text-3xl font-bold">Community learning resources</h1>
<p class="mt-4 leading-7">Useful practice, organized clearly.</p>
</main>
</body>
</html>
Build the CSS:
npx @tailwindcss/cli -i ./src/input.css -o ./src/output.css
The tool prints Done in and a time. Open src/index.html in your browser. You should see an off-white page, a white panel with rounded corners, and a bold heading with a 30-pixel font size at the usual root size (text-3xl is 1.875rem).
How the build works
Tailwind scans the files in your project for class names, then writes CSS for only the classes it finds into output.css. It skips node_modules, files listed in .gitignore, images and CSS files. The browser loads output.css, never input.css.
output.css is generated, so never edit it by hand: the next build overwrites it. Change your HTML or input.css, then rebuild.
While you work, add --watch to the build command. The tool stays running and rebuilds every time you save. Stop it with Ctrl+C.
Practice
- Change
p-6on the<main>panel top-10and rebuild. Open developer tools, select<main>and read its padding in the box-model diagram: it should now be 40 pixels (2.5rem) on each side. - Rename the link to
./outputs.css. The page loses all styling. Open the Network panel, reload and find the 404 foroutputs.css. Fix the name. - Open
output.cssand search for.p-10. Find the rulepadding: calc(var(--spacing) * 10);. Tailwind 4 builds its spacing from one variable,--spacing, which is 0.25rem, sop-10is 10 × 0.25rem = 2.5rem.
Check yourself
- Which file does the browser load?
- Why does
output.cssnot contain every possible Tailwind class? - Where do you make changes:
output.cssorinput.cssand your HTML?
Answers
output.css, the file the build writes.- Tailwind generates CSS only for class names it finds in your scanned files.
- In
input.cssand your HTML.output.cssis rebuilt from them and any hand edits are lost.
Next, you will map common utilities back to the box model.
Continue with the full course
This free preview is lesson 1 only. Enroll to open the rest of Tailwind CSS 4: Utility-First Interfaces.