Home Web Front-end CSS Tutorial Setting Up a Tailwind CSS Project from Scratch

Setting Up a Tailwind CSS Project from Scratch

Aug 08, 2024 pm 03:03 PM

Setting Up a Tailwind CSS Project from Scratch

Setting Up a Tailwind CSS Project from Scratch

Tailwind CSS has become a popular choice among developers for its utility-first approach to styling. It offers a highly customizable and efficient way to design web applications without writing custom CSS. In this guide, we'll walk you through setting up a Tailwind CSS project from scratch.

Table of Contents

  1. Introduction to Tailwind CSS
  2. Prerequisites
  3. Setting Up a New Project
  4. Installing Tailwind CSS
  5. Configuring Tailwind CSS
  6. Creating Your First Tailwind CSS File
  7. Building and Watching CSS
  8. Optimizing for Production
  9. Conclusion

Introduction to Tailwind CSS

Tailwind CSS is a utility-first CSS framework that provides low-level utility classes to build custom designs directly in your markup. Unlike traditional CSS frameworks like Bootstrap or Foundation, Tailwind doesn't come with pre-designed components. Instead, it provides a set of utility classes that let you design your components without leaving your HTML.

Why Use Tailwind CSS?

  • Utility-First Approach: Allows you to apply styles directly in your HTML, reducing the need for custom CSS.
  • Customization: Tailwind is highly customizable, allowing you to override default settings and create your unique design system.
  • Responsive Design: Tailwind offers built-in responsive design utilities, making it easy to create mobile-first designs.
  • Consistency: Ensures consistent styling across your application.

Prerequisites

Before we begin, ensure you have the following installed:

  • Node.js (v12 or higher)
  • npm (Node Package Manager)

You can download and install Node.js and npm from the official website.

Setting Up a New Project

First, create a new directory for your project and navigate into it:

mkdir tailwind-project
cd tailwind-project
Copy after login

Next, initialize a new npm project:

npm init -y
Copy after login

This command creates a package.json file, which will keep track of your project dependencies and scripts.

Installing Tailwind CSS

To install Tailwind CSS, you need to add it as a dependency to your project. Run the following command:

npm install tailwindcss
Copy after login

After installing Tailwind CSS, you'll need to create a configuration file. This file will allow you to customize the default settings of Tailwind CSS. To generate this file, run:

npx tailwindcss init
Copy after login

This command creates a tailwind.config.js file in your project root. This file is where you can customize your Tailwind setup.

Configuring Tailwind CSS

Open the tailwind.config.js file. You should see a basic configuration like this:

module.exports = {
  content: [],
  theme: {
    extend: {},
  },
  plugins: [],
}
Copy after login

The content array is used to specify the paths to all of your template files. This allows Tailwind to tree-shake unused styles in production. Add the paths to your HTML and JavaScript files:

module.exports = {
  content: [
    './src/**/*.{html,js}',
  ],
  theme: {
    extend: {},
  },
  plugins: [],
}
Copy after login
Copy after login

This configuration tells Tailwind to look for classes in any .html or .js file inside the src directory.

Creating Your First Tailwind CSS File

Next, create a new CSS file where you will import Tailwind's base, components, and utilities styles. Create a src directory and inside it, create a file named styles.css:

mkdir src
touch src/styles.css
Copy after login

Open the styles.css file and add the following imports:

@tailwind base;
@tailwind components;
@tailwind utilities;
Copy after login

These directives tell Tailwind to include its base, components, and utilities styles in your CSS file.

Building and Watching CSS

To compile your CSS, you'll need to use the Tailwind CLI. Add a build script to your package.json file:

"scripts": {
  "build": "tailwindcss -i ./src/styles.css -o ./dist/styles.css",
  "watch": "tailwindcss -i ./src/styles.css -o ./dist/styles.css --watch"
}
Copy after login

The build script compiles your src/styles.css file and outputs the result to dist/styles.css. The watch script does the same but continues to watch for changes and recompiles automatically.

To compile your CSS for the first time, run:

npm run build
Copy after login
Copy after login

This command creates a dist directory with the compiled styles.css file.

Creating Your First HTML File

Now, create an index.html file in the src directory:

touch src/index.html
Copy after login

Open the index.html file and add the following boilerplate HTML:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Tailwind CSS Project</title>
  <link href="../dist/styles.css" rel="stylesheet">
</head>
<body>
  <h1 class="text-4xl font-bold text-center mt-10">Hello, Tailwind CSS!</h1>
</body>
</html>
Copy after login

This simple HTML file includes the compiled Tailwind CSS file and adds a styled heading.

To see your changes, open the index.html file in a web browser.

Optimizing for Production

When you're ready to deploy your project, you'll want to optimize your CSS for production. Tailwind provides a built-in tool for purging unused styles and minifying your CSS.

To enable this, update your tailwind.config.js file to include the purge option:

module.exports = {
  content: [
    './src/**/*.{html,js}',
  ],
  theme: {
    extend: {},
  },
  plugins: [],
}
Copy after login
Copy after login

Next, install @fullhuman/postcss-purgecss and cssnano:

npm install @fullhuman/postcss-purgecss cssnano
Copy after login

Create a postcss.config.js file in your project root and add the following configuration:

const purgecss = require('@fullhuman/postcss-purgecss');
const cssnano = require('cssnano');

module.exports = {
  plugins: [
    purgecss({
      content: ['./src/**/*.{html,js}'],
      defaultExtractor: content => content.match(/[\w-/:]+(?<!:)/g) || [],
    }),
    cssnano({
      preset: 'default',
    }),
  ],
}
Copy after login

This configuration sets up PostCSS with PurgeCSS and CSSNano to remove unused styles and minify your CSS.

Finally, update your build script in package.json:

"scripts": {
  "build": "NODE_ENV=production tailwindcss -i ./src/styles.css -o ./dist/styles.css"
}
Copy after login

Run the build script to generate your optimized CSS:

npm run build
Copy after login
Copy after login

Your dist/styles.css file is now optimized for production.

Conclusion

Setting up a Tailwind CSS project from scratch is straightforward and provides a powerful toolkit for building custom designs. By following this guide, you've learned how to install Tailwind CSS, configure it, and optimize it for production. Tailwind's utility-first approach streamlines the styling process, allowing you to focus on building your application.

Happy coding!

The above is the detailed content of Setting Up a Tailwind CSS Project from Scratch. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Vue 3 Vue 3 Apr 02, 2025 pm 06:32 PM

It&#039;s out! Congrats to the Vue team for getting it done, I know it was a massive effort and a long time coming. All new docs, as well.

Can you get valid CSS property values from the browser? Can you get valid CSS property values from the browser? Apr 02, 2025 pm 06:17 PM

I had someone write in with this very legit question. Lea just blogged about how you can get valid CSS properties themselves from the browser. That&#039;s like this.

A bit on ci/cd A bit on ci/cd Apr 02, 2025 pm 06:21 PM

I&#039;d say "website" fits better than "mobile app" but I like this framing from Max Lynch:

Using Markdown and Localization in the WordPress Block Editor Using Markdown and Localization in the WordPress Block Editor Apr 02, 2025 am 04:27 AM

If we need to show documentation to the user directly in the WordPress editor, what is the best way to do it?

Stacked Cards with Sticky Positioning and a Dash of Sass Stacked Cards with Sticky Positioning and a Dash of Sass Apr 03, 2025 am 10:30 AM

The other day, I spotted this particularly lovely bit from Corey Ginnivan’s website where a collection of cards stack on top of one another as you scroll.

Comparing Browsers for Responsive Design Comparing Browsers for Responsive Design Apr 02, 2025 pm 06:25 PM

There are a number of these desktop apps where the goal is showing your site at different dimensions all at the same time. So you can, for example, be writing

How to Use CSS Grid for Sticky Headers and Footers How to Use CSS Grid for Sticky Headers and Footers Apr 02, 2025 pm 06:29 PM

CSS Grid is a collection of properties designed to make layout easier than it’s ever been. Like anything, there&#039;s a bit of a learning curve, but Grid is

Google Fonts   Variable Fonts Google Fonts Variable Fonts Apr 09, 2025 am 10:42 AM

I see Google Fonts rolled out a new design (Tweet). Compared to the last big redesign, this feels much more iterative. I can barely tell the difference

See all articles