DEV Community

Cover image for How to integrate Tailwind CSS into Django
PyMeister
PyMeister

Posted on

How to integrate Tailwind CSS into Django

Django-Tailwind is a library that allows you to easily integrate the Tailwind CSS framework into your Django project. Tailwind is a popular, utility-first CSS framework that provides a fast and efficient way to build custom user interfaces.

Here's a step-by-step guide to using Django-Tailwind in your Django project:

Install Django-Tailwind: To use Django-Tailwind, you first need to install it using pip. Open a terminal and run the following command:

pip install django-tailwind
Enter fullscreen mode Exit fullscreen mode

Add Django-Tailwind to Your INSTALLED_APPS: Next, you need to add Django-Tailwind to your INSTALLED_APPS setting in your Django settings.py file.

INSTALLED_APPS = [
    # ...
    'tailwind',
]
Enter fullscreen mode Exit fullscreen mode

Include Django-Tailwind in Your Template: To include the Tailwind CSS in your Django templates, you need to use the

{% load tailwind %}
Enter fullscreen mode Exit fullscreen mode

tag at the top of your template file, and then include the

{% tailwind %}
Enter fullscreen mode Exit fullscreen mode

tag where you want the CSS to be loaded.

{% load tailwind %}
<!DOCTYPE html>
<html>
<head>
  <title>My Django Project</title>
  {% tailwind %}
</head>
<body>
  <!-- Your HTML goes here -->
</body>
</html>

Enter fullscreen mode Exit fullscreen mode

Use Tailwind Classes in Your HTML: Now that you have included the Tailwind CSS in your Django templates, you can start using the Tailwind classes in your HTML. Here's an example of a simple button:

<button class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded">
  Click me
</button>
Enter fullscreen mode Exit fullscreen mode

Customize Your Tailwind Configuration: If you want to customize your Tailwind configuration, you can create a tailwind.config.js file in the root of your project. The file should contain your custom configuration, which will be used instead of the default Tailwind configuration.

module.exports = {
  theme: {
    extend: {
      colors: {
        'my-custom-color': '#ff0000',
      },
    },
  },
  variants: {},
  plugins: [],
}
Enter fullscreen mode Exit fullscreen mode

In conclusion, Django-Tailwind is a great tool for integrating the Tailwind CSS framework into your Django project. With its easy setup and integration, you can quickly and easily start using Tailwind to build custom user interfaces that look and feel great.

Top comments (0)