Tailwind CSS 3 with Django templates
2022-02-05
Usage
mkdir jstoolchain
cd jstoolchain
npm init -y
npm install -D tailwindcss
npx tailwindcss initjstoolchain/tailwind.config.js
module.exports = {
content: ["../templates/**/*.{html,js}"],
theme: {
extend: {},
},
plugins: [],
}mkdir css
touch css/tailwind.cssjstoolchain/css/tailwind.css
@tailwind base;
@tailwind components;
@tailwind utilities;jstoolchain/package.json
"scripts": {
"build": "npx tailwindcss -i css/tailwind.css -o ../static/css/tailwind-output.css"
},jstoolchain/
npm run buildtemplates/base.html
{% load static %}
<head>
<link rel="stylesheet" href="{% static 'css/tailwind-output.css' %}" />
</head>Now you can use tailwind css inside django templates, but you need to go through the build process (npm run build from above) each time new tailwind css was introduced.
In order to automate this build process, add following dev script to the package.json
jstoolchain/package.json
"scripts": {
"build": "npx tailwindcss -i css/tailwind.css -o ../static/css/tailwind-output.css",
"dev": "nodemon --watch '../templates/**/*' -e html -x 'npm run build'"
},jstoolchain/
npm run devAny changes in *.html files in django templates directory can trigger build script and changes will be applied without manually typing npm run build