Getting Started with Tailwind CSS

Frontend ~ 8 minutes

# Welcome to Tailwind CSS

Hello and welcome! Today we will learn Tailwind CSS. It is a very popular CSS tool. With Tailwind, you do not write many CSS files. Instead, you add small utility classes directly to your HTML.

For example, the class text-center centers text, and bg-blue-500 gives a blue background. You join many small classes to build a full design. This is called the utility-first way.

Why people like Tailwind

  • You build designs faster, without leaving your HTML file
  • You do not need to invent class names
  • Your final CSS file is small, because unused classes are removed
  • Responsive design is very easy

Do not worry if this sounds new. We will go step by step. Let us start!

Create a project folder
Code Copied to Clipboard

# Step 1: Make a project folder

First, we need a place for our files. The command above creates a new folder named tailwind-demo. Please enter this folder in your terminal with cd tailwind-demo.

You also need Node.js installed, because Tailwind uses npm. If you are not sure, run node --version. You can download Node.js from nodejs.org if you need it.

Create package.json
Code Copied to Clipboard

# Step 2: Start an npm project

This command creates a package.json file. It will remember the tools we install. Now, please install Tailwind. We will use the official Tailwind CLI, which is the easiest way to begin without any framework.

The next command installs two packages. One is tailwindcss, and the other is the CLI tool called @tailwindcss/cli.

Install Tailwind CSS
Code Copied to Clipboard

About Tailwind versions

Tailwind CSS version 4 changed the setup a little. In version 4, you only need one line of CSS to load Tailwind, and the old tailwind.config.js file is optional. If you follow an older video, some steps may look different. The latest steps are always at tailwindcss.com/docs.

# Step 3: Add Tailwind to your CSS

Now, let us create a folder named src, and inside it a file named input.css. This file needs only one line. The line tells Tailwind to add all its styles here.

src/input.css
Code Copied to Clipboard

# Step 4: Start the build process

Tailwind reads your HTML files and finds the classes you used. Then it writes a small CSS file with only those styles. We call this the build.

The command below starts a watch mode. This means Tailwind rebuilds the CSS every time you save a file. Please keep this terminal open while you work.

Build CSS in watch mode
Code Copied to Clipboard

# Step 5: Create your HTML page

Now, please create a file named index.html in the main folder. We link the built CSS file inside the head. Then we write a simple card using utility classes.

Let us look at the first part of the page. It has the basic HTML setup and the link to dist/output.css.

index.html (part 1)
Code Copied to Clipboard

# Adding a card

Next, we add a card inside the body. Each class does one small job. Let us read them one by one.

What the classes mean

  • max-w-sm limits the width of the card
  • mx-auto centers the card on the page
  • mt-10 adds space on the top
  • p-6 adds padding inside the card
  • rounded-xl makes the corners round
  • shadow-lg adds a soft shadow

Please type the second part of the page below, then close the body and html tags.

index.html (part 2)
Code Copied to Clipboard
Keep the watch command running

If your page has no style, the most common reason is that the build is not running. Please check the terminal where you started the watch command. It must stay open.

Also check that the link path in your HTML is correct. It must match the -o path from the build command, which is ./dist/output.css. Finally, remember to refresh your browser after you save your changes.

# Step 6: Open the page

Now, please open index.html in your browser. You should see a white card with a title, a short text and a blue button. When you move your mouse over the button, it becomes darker. This is the hover:bg-blue-700 class at work.

The hover: word is called a variant. Variants let you style special states, like hover, focus and active. You only add a small prefix in front of the class.

Do not memorize every class

Tailwind has hundreds of classes, but you do not need to remember them all. Most people learn the common ones in a few days. When you forget one, use the search box in the official docs at tailwindcss.com/docs. Also, the Tailwind plugin for VS Code gives you class suggestions while you type.

# Step 7: Make it responsive

Tailwind makes responsive design simple. You add a breakpoint prefix before a class. The class then works only on screens of that size or bigger.

Common breakpoint prefixes

  • sm: starts at 640px
  • md: starts at 768px
  • lg: starts at 1024px
  • xl: starts at 1280px

Tailwind is mobile-first. This means classes without a prefix work on all screens, and prefixed classes change the style on bigger screens. Let us see an example with a grid.

A responsive grid
Code Copied to Clipboard

# How the grid works

In the grid above, we use grid-cols-1 as the base. So on a phone, you see one column. On a medium screen, md:grid-cols-2 shows two columns. On a large screen, lg:grid-cols-3 shows three columns.

Try to make your browser window smaller and bigger. You will see the layout change smoothly. This is a very powerful feature, and you wrote no media query at all!

Now, let us prepare the final CSS for a real website. For production, we create a small, compressed file.

Build a minified file for production
Code Copied to Clipboard

Production build

The --minify flag removes spaces and makes the file as small as possible. Use this file when you publish your website. Because Tailwind includes only the classes you used, the final file is often very small.

One more tip: avoid building class names with code, like "bg-" + color. Tailwind cannot see such names in your files, so it will not create the style. Always write the full class name, like bg-red-500.

# Summary and next steps

Congratulations! You have built your first page with Tailwind CSS. Let us remember the main points.

What we learned

  • Tailwind gives small utility classes that you add in HTML
  • We installed it with npm and used the Tailwind CLI
  • We wrote @import "tailwindcss"; in our input CSS file
  • Variants like hover: and prefixes like md: change styles for states and screen sizes
  • --minify creates a small file for production

Where to go next

  • Read the full guide at tailwindcss.com/docs
  • Try flexbox classes like flex, items-center and justify-between
  • Build a navigation bar and a footer for your page
  • Learn about dark mode with the dark: variant

Please practice a little every day. You are doing great!