Skip to content

Instantly share code, notes, and snippets.

@shafayeatsumit
Last active August 19, 2023 10:47
Show Gist options
  • Save shafayeatsumit/520f274dd641f4735076f8eaa1f21fb8 to your computer and use it in GitHub Desktop.
Save shafayeatsumit/520f274dd641f4735076f8eaa1f21fb8 to your computer and use it in GitHub Desktop.
Tailwind CSS Setup Guide

Tailwind CSS Setup Guide

Prerequisites

Before you begin, make sure you have Node.js and npm (Node Package Manager) installed on your system.

Getting Started

  1. Create an Empty Folder: Begin by creating a new empty folder for your project.

  2. Initialize npm Package: Open your terminal/command prompt and navigate to your project folder. Run the following command to initialize a new npm package:

    npm init -y
  3. Install Tailwind CSS: Install Tailwind CSS and save it as a development dependency using the following command:

    npm install -D tailwindcss
  4. Initialize Tailwind Configuration: Generate the default Tailwind configuration file using the following command:

    npx tailwindcss init

Project Setup

  1. Create Files: Inside the source folder (often named src), create two files: index.html and input.css.

  2. Add Tailwind Directives to Main CSS File: Open your main CSS file (input.css inside the src folder) and add the following Tailwind directives at the beginning. These directives include Tailwind's base, components, and utilities layers:

    /* src/input.css */
    @tailwind base;
    @tailwind components;
    @tailwind utilities;
    
    /* Your custom styles go here */
  3. Update Tailwind Configuration: Open the tailwind.config.js file and locate the content property. Update it as follows to include HTML and JavaScript files from the src folder:

    module.exports = {
      // Other configurations...
      content: ["./src/*.{html,js}"],
    };
  4. Update package.json: Add the following scripts to your package.json file. These scripts will be used for building and watching changes in your Tailwind CSS:

    "scripts": {
      "build": "tailwindcss -i ./src/input.css -o ./dist/output.css",
      "watch": "tailwindcss -i ./src/input.css -o ./dist/output.css --watch"
    }
  5. Link CSS in HTML: Open the index.html file and add the following line inside the <head> section. This links your compiled Tailwind CSS to the HTML file:

    <link rel="stylesheet" href="../dist/output.css" />

Building and Watching

To build your Tailwind CSS styles and generate the output file, run the following command in your terminal:

npm run build

To watch for changes in your CSS and automatically update the output file as you make edits, use the following command:

npm run watch

Remember to include the generated output.css file in your HTML files to apply the Tailwind CSS styles to your project.

Now you're all set to start working with Tailwind CSS in your project! Enjoy the benefits of its utility classes and streamlined styling approach.

Folder Structure Example:

project-folder/
├── src/
│   ├── index.html
│   └── input.css
├── tailwind.config.js
├── dist/
│   └── output.css
├── node_modules/
├── package.json
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment