Adding Tailwind CSS to Django

Add TailwindCSS to Django heyylateef | Jan 28 2025

Adding Tailwind CSS to Your Django Project

Tailwind CSS is a utility-first CSS framework that allows developers to build custom designs without leaving their HTML. It’s a great choice for Django projects where you want to maintain flexibility and control over your styles. In this guide, we’ll walk through the process of adding Tailwind CSS to a Django project.


Prerequisites

Before you get started, ensure you have the following:

  1. Django installed: You can install Django via pip if you don’t already have it:
    pip install django
    
  2. Node.js and npm/yarn: Tailwind CSS requires Node.js to manage its dependencies.

Steps to Add Tailwind CSS to Your Django Project

1. Create a Django Project (if you don’t already have one)

If you don’t have a Django project, create one:

django-admin startproject myproject
cd myproject

Create a new Django app within your project:

python manage.py startapp myapp

2. Install Tailwind CSS Dependencies

Navigate to your project directory and initialize a new Node.js project:

npm init -y

Next, install Tailwind CSS and its peer dependencies:

npm install tailwindcss postcss autoprefixer

3. Set Up Tailwind CSS Configuration

Generate the tailwind.config.js file by running:

npx tailwindcss init

This will create a basic configuration file. You can customize it later based on your project needs.


4. Configure Tailwind CSS to Process Templates

Update your tailwind.config.js file to include Django template files. By default, Django templates are stored in templates/ or app-specific directories.

Example configuration:

/** @type {import('tailwindcss').Config} */
module.exports = {
  content: [
    './templates/**/*.html',
    './myapp/templates/**/*.html',
  ],
  theme: {
    extend: {},
  },
  plugins: [],
};

This ensures that Tailwind scans your templates for class names and generates only the CSS you use.


5. Create a Tailwind Input CSS File

Create a static/css/ directory in your Django project if it doesn’t already exist, and add a tailwind.css file:

@tailwind base;
@tailwind components;
@tailwind utilities;

6. Build the Tailwind CSS Output File

You need to process the tailwind.css file into a production-ready CSS file. Set up a script in your package.json to make this easier:

"scripts": {
  "build:css": "npx tailwindcss -i ./static/css/tailwind.css -o ./static/css/output.css --watch"
}

Run the following command to build the CSS file:

npm run build:css

This will generate output.css in the static/css/ directory.


7. Add the CSS to Your Django Template

In your base template, include the output.css file:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Django with Tailwind CSS</title>
  <link rel="stylesheet" href="{% static 'css/output.css' %}">
</head>
<body>
  <h1 class="text-3xl font-bold underline text-center">Hello, Tailwind CSS!</h1>
</body>
</html>

8. Run Your Django Server

Start the Django development server:

python manage.py runserver

Visit http://127.0.0.1:8000 to see Tailwind CSS in action.


Optional: Optimize for Production

To optimize your CSS for production, run:

npx tailwindcss -i ./static/css/tailwind.css -o ./static/css/output.css --minify

This removes unused CSS classes and reduces the file size.


Conclusion

Adding Tailwind CSS to your Django project is a straightforward process that enhances your ability to create beautiful, responsive designs with minimal effort. With the steps outlined above, you’re ready to start building custom, Tailwind-powered interfaces in your Django app.

Happy coding!

About The Lab

Like the content posted on this site? Need help with another web project? Want to give some feedback? Feel free to contact me via email or social media!

Know more!
DigitalOcean Referral Badge