|
1 |
| -# Tailwind CSS |
| 1 | +# Tailwind CSS V4 |
2 | 2 |
|
3 |
| -This guide will help you install and config `Tailwind v3` |
4 |
| - |
5 |
| -## Install Dependency |
6 |
| - |
7 |
| -In directory which contains the `package.json`, install `tailwindcss` and `postcss-import` |
| 3 | +From `python-webpack-boilerplate>=1.0.4`, we can choose `tailwind` as the style solution when creating the frontend project. |
8 | 4 |
|
9 | 5 | ```bash
|
10 |
| -$ npm install -D tailwindcss@latest postcss-import |
| 6 | +(venv) python manage.py webpack_init |
| 7 | + [1/3] project_slug (frontend): |
| 8 | + [2/3] run_npm_command_at_root (y): |
| 9 | + [3/3] Select style_solution |
| 10 | + 1 - tailwind |
| 11 | + 2 - bootstrap |
| 12 | + 3 - scss |
| 13 | + Choose from [1/2/3] (1): 1 |
| 14 | + [SUCCESS]: Frontend app 'frontend' has been created. To know more, check https://python-webpack-boilerplate.rtfd.io/en/latest/frontend/ |
11 | 15 | ```
|
12 | 16 |
|
13 |
| -Once done, update `postcss.config.js` |
| 17 | +Just choose `tailwind` as the style_solution, the Tailwind V4 will be ready for you. |
14 | 18 |
|
15 |
| -``` |
16 |
| -// https://tailwindcss.com/docs/using-with-preprocessors |
17 |
| -
|
18 |
| -module.exports = { |
19 |
| - plugins: [ |
20 |
| - require('postcss-import'), |
21 |
| - require('tailwindcss/nesting')(require('postcss-nesting')), |
22 |
| - require('tailwindcss'), |
23 |
| - require('postcss-preset-env')({ |
24 |
| - features: { 'nesting-rules': false } |
25 |
| - }), |
26 |
| - ] |
27 |
| -}; |
28 |
| -``` |
| 19 | +## Install Dependency |
29 | 20 |
|
30 |
| -Next, generate a config file for your project using the Tailwind CLI utility included when you install the `tailwindcss` npm package |
| 21 | +In directory which contains the `package.json` |
31 | 22 |
|
32 | 23 | ```bash
|
33 |
| -$ npx tailwindcss init |
| 24 | +# install dependency packages |
| 25 | +$ npm install |
| 26 | +# run webpack in watch mode |
| 27 | +$ npm run watch |
34 | 28 | ```
|
35 | 29 |
|
36 |
| -Now `tailwind.config.js` is generated |
37 |
| - |
38 |
| -```js |
39 |
| -module.exports = { |
40 |
| - content: [], |
41 |
| - theme: { |
42 |
| - extend: {}, |
43 |
| - }, |
44 |
| - plugins: [], |
45 |
| -} |
46 |
| -``` |
47 |
| - |
48 |
| -!!! note |
49 |
| - |
50 |
| - Please make sure `tailwind.config.js` exists in the same directory as `postcss.config.js` |
51 |
| - |
52 |
| -## JIT |
53 |
| - |
54 |
| -From Tailwind V3, it enabled `JIT` (Just-in-Time) all the time. |
55 |
| - |
56 |
| -> Tailwind CSS works by scanning all of your HTML, JavaScript components, and any other template files for class names, then generating all of the corresponding CSS for those styles. |
57 |
| -
|
58 |
| -> In order for Tailwind to generate all of the CSS you need, it needs to know about every single file in your project that contains any Tailwind class names. |
59 |
| -
|
60 |
| -So we should config `content` section of the `tailwind.config.js`, then Tailwind will know which css classes are used. |
61 |
| - |
62 |
| -Let's update *frontend/tailwind.config.js* |
63 |
| - |
64 |
| -```js |
65 |
| -const Path = require("path"); |
66 |
| -const pwd = process.env.PWD; |
67 |
| - |
68 |
| -// We can add current project paths here |
69 |
| -const projectPaths = [ |
70 |
| - Path.join(pwd, "../example/templates/**/*.html"), |
71 |
| - // add js file paths if you need |
72 |
| -]; |
73 |
| - |
74 |
| -const contentPaths = [...projectPaths]; |
75 |
| -console.log(`tailwindcss will scan ${contentPaths}`); |
76 |
| - |
77 |
| -module.exports = { |
78 |
| - content: contentPaths, |
79 |
| - theme: { |
80 |
| - extend: {}, |
81 |
| - }, |
82 |
| - plugins: [], |
83 |
| -} |
84 |
| -``` |
| 30 | +## Explicitly Specify Source Files |
85 | 31 |
|
86 |
| -Notes: |
| 32 | +Even Tailwind 4 can AUTO scan all project files in the project directory, we still recommend to explicitly specify the source files to improve performance. |
87 | 33 |
|
88 |
| -1. Here we add Django templates path to the `projectPaths` |
89 |
| -1. And then we pass the `contentPaths` to the `content` |
90 |
| -1. The final built css file will contain css classes used in the Django templates |
| 34 | +Below is an example |
91 | 35 |
|
92 |
| -## Import Tailwind CSS |
| 36 | +```css |
| 37 | +/*import tailwindcss and disable automatic source detection*/ |
| 38 | +@import "tailwindcss" source(none); |
93 | 39 |
|
94 |
| -Update *src/styles/index.scss* |
| 40 | +/*register frontend directory*/ |
| 41 | +@source "../"; |
95 | 42 |
|
96 |
| -```scss |
97 |
| -@import "tailwindcss/base"; |
98 |
| -@import "tailwindcss/components"; |
99 |
| -@import "tailwindcss/utilities"; |
| 43 | +/*register django templates*/ |
| 44 | +@source "../../../django_tailwind_app/**/*.html"; |
100 | 45 |
|
101 | 46 | .jumbotron {
|
102 |
| - // should be relative path of the entry scss file |
103 |
| - background-image: url("../../vendors/images/sample.jpg"); |
104 |
| - background-size: cover; |
| 47 | + /*should be relative path of the entry css file*/ |
| 48 | + background-image: url("../../vendors/images/sample.jpg"); |
| 49 | + background-size: cover; |
105 | 50 | }
|
106 | 51 |
|
107 |
| -.btn-blue { |
108 |
| - @apply inline-block px-4 py-2; |
109 |
| - @apply font-semibold rounded-lg shadow-md; |
110 |
| - @apply bg-blue-500 text-white; |
111 |
| - @apply hover:bg-blue-700 focus:outline-none; |
112 |
| - @apply focus:ring-2 focus:ring-blue-400 focus:ring-opacity-75; |
| 52 | +@layer components{ |
| 53 | + .btn-blue { |
| 54 | + @apply inline-flex items-center; |
| 55 | + @apply px-4 py-2; |
| 56 | + @apply font-semibold rounded-lg shadow-md; |
| 57 | + @apply text-white bg-blue-500; |
| 58 | + @apply hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-blue-400/50; |
| 59 | + } |
113 | 60 | }
|
114 | 61 | ```
|
115 | 62 |
|
116 |
| -``` |
117 |
| -$ npm run start |
118 |
| -
|
119 |
| -tailwindcss will scan django_basic/example/templates/**/*.html |
120 |
| -``` |
121 |
| - |
122 |
| -Edit Django template `templates/index.html` |
123 |
| - |
124 |
| -```html hl_lines="8 28" |
125 |
| -{% load webpack_loader static %} |
126 |
| - |
127 |
| -<!DOCTYPE html> |
128 |
| -<html> |
129 |
| -<head> |
130 |
| - <meta charset="utf-8" /> |
131 |
| - <title>Index</title> |
132 |
| - {% stylesheet_pack 'app' %} |
133 |
| -</head> |
134 |
| -<body> |
135 |
| - |
136 |
| -<div class="jumbotron"> |
137 |
| - <div class="w-full max-w-7xl p-4 mx-auto"> |
138 |
| - <h1 class="text-4xl mb-4">Hello, world!</h1> |
139 |
| - |
140 |
| - <p class="mb-2">This is a template for a simple marketing or informational website. It includes a large callout called a |
141 |
| - jumbotron and three supporting pieces of content. Use it as a starting point to create something more unique.</p> |
142 |
| - |
143 |
| - <p class="mb-2"><a class="btn-blue" href="#" role="button">Learn more »</a></p> |
144 |
| - |
145 |
| - <div class="flex justify-center"> |
146 |
| - <img src="{% static 'vendors/images/webpack.png' %}"/> |
147 |
| - </div> |
148 |
| - |
149 |
| - </div> |
150 |
| -</div> |
151 |
| - |
152 |
| -{% javascript_pack 'app' %} |
153 |
| - |
154 |
| -</body> |
155 |
| -</html> |
156 |
| -``` |
157 |
| - |
158 |
| -```bash |
159 |
| -$ python manage.py runserver |
160 |
| -``` |
161 |
| - |
162 |
| - |
163 |
| - |
164 | 63 | ## Live Reload
|
165 | 64 |
|
166 |
| -When you add Tailwind css class in Django template, it would be cool if the page can `auto live realod`, please check link below |
167 |
| - |
168 | 65 | [Live Reload Support](live_reload.md)
|
169 | 66 |
|
170 |
| -## Tutorials |
171 |
| - |
172 |
| -To learn more about Tailwind and Django, you can check |
| 67 | +## Tailwind V3 |
173 | 68 |
|
174 |
| -1. [Django Tailwind CSS Alpine.js Tutorial](https://www.accordbox.com/blog/django-tailwind-css-alpinejs-tutorial/) |
175 |
| -2. [wagtail-tailwind-blog](https://github.com/AccordBox/wagtail-tailwind-blog) |
| 69 | +If you still want to install Tailwind V3, please check [Tailwind CSS V3](setup_with_tailwind3.md) |
0 commit comments