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.
Before you get started, ensure you have the following:
pip install django
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
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
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.
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.
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;
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.
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>
Start the Django development server:
python manage.py runserver
Visit http://127.0.0.1:8000
to see Tailwind CSS in action.
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.
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!
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!