Debian Codegraph @

docs/content/en/functions/css/TailwindCSS.md

raw
1 ---
2 title: css.TailwindCSS
3 description: Processes the given resource with the Tailwind CSS CLI.
4 categories: []
5 keywords: []
6 params:
7 functions_and_methods:
8 aliases: []
9 returnType: resource.Resource
10 signatures: ['css.TailwindCSS [OPTIONS] RESOURCE']
11 ---
12
13 Use the `css.TailwindCSS` function to process your Tailwind CSS files. This function uses the Tailwind CSS CLI to:
14
15 1. Scan your templates for Tailwind CSS utility class usage.
16 1. Compile those utility classes into standard CSS.
17 1. Generate an optimized CSS output file.
18
19 > [!NOTE]
20 > Use this function with Tailwind CSS v4.0 and later, which require a relatively [modern browser][] to render correctly.
21
22 ## Setup
23
24 Step 1
25 : Install Tailwind CSS v4.0 or later:
26
27 ```sh {copy=true}
28 npm install --save-dev tailwindcss @tailwindcss/cli @tailwindcss/typography
29 ```
30
31 <!-- TODO
32 Remove the admonition below somewhere after v0.176.0, 15 minor releases
33 after deprecation.
34 -->
35
36 > [!NOTE]
37 > As of v0.161.0, Hugo no longer supports the Tailwind [standalone binary][]. You must now install the Tailwind CSS CLI via `npm` as shown above.
38
39 Step 2
40 : Add this to your project configuration:
41
42 {{< code-toggle file=hugo copy=true >}}
43 [build]
44 [build.buildStats]
45 enable = true
46 [[build.cachebusters]]
47 source = 'assets/notwatching/hugo_stats\.json'
48 target = 'css'
49 [[build.cachebusters]]
50 source = '(postcss|tailwind)\.config\.js'
51 target = 'css'
52 [module]
53 [[module.mounts]]
54 source = 'assets'
55 target = 'assets'
56 [[module.mounts]]
57 disableWatch = true
58 source = 'hugo_stats.json'
59 target = 'assets/notwatching/hugo_stats.json'
60 {{< /code-toggle >}}
61
62 Step 3
63 : Create a CSS entry file:
64
65 ```css {file="assets/css/main.css" copy=true}
66 @import "tailwindcss";
67 @plugin "@tailwindcss/typography";
68 @source "hugo_stats.json";
69 ```
70
71 Tailwind CSS respects `.gitignore` files. This means that if `hugo_stats.json` is listed in your `.gitignore` file, Tailwind CSS will ignore it. To make `hugo_stats.json` available to Tailwind CSS you must explicitly source it as shown in the example above.
72
73 Step 4
74 : Create a _partial_ template to process the CSS with the Tailwind CSS CLI:
75
76 ```go-html-template {file="layouts/_partials/css.html" copy=true}
77 {{ with resources.Get "css/main.css" }}
78 {{ $opts := dict "minify" (not hugo.IsDevelopment) }}
79 {{ with . | css.TailwindCSS $opts }}
80 {{ if hugo.IsDevelopment }}
81 <link rel="stylesheet" href="{{ .RelPermalink }}">
82 {{ else }}
83 {{ with . | fingerprint }}
84 <link rel="stylesheet" href="{{ .RelPermalink }}" integrity="{{ .Data.Integrity }}" crossorigin="anonymous">
85 {{ end }}
86 {{ end }}
87 {{ end }}
88 {{ end }}
89 ```
90
91 Step 5
92 : Call the _partial_ template from your base template, deferring template execution until after all sites and output formats have been rendered:
93
94 ```go-html-template {file="layouts/baseof.html" copy=true}
95 <head>
96 {{ with (templates.Defer (dict "key" "global")) }}
97 {{ partial "css.html" . }}
98 {{ end }}
99 </head>
100 ```
101
102 ## Options
103
104 The `css.TailwindCSS` function accepts an options map.
105
106 `minify`
107 : (`bool`) Whether to optimize and minify the output. Default is `false`.
108
109 `optimize`
110 : (`bool`) Whether to optimize the output without minifying. Default is `false`.
111
112 `disableInlineImports`
113 : {{< new-in 0.147.4 />}}
114 : (`bool`) Whether to disable inlining of `@import` statements. Inlining is performed recursively, but currently once only per file. It is not possible to import the same file in different scopes (root, media query, etc.). Note that this import routine does not care about the CSS specification, so you can have `@import` statements anywhere in the file. Default is `false`.
115
116 `skipInlineImportsNotFound`
117 : (`bool`) Whether to allow the build process to continue despite unresolved import statements, preserving the original import declarations. It is important to note that the inline importer does not process URL-based imports or those with media queries, and these will remain unaltered even when this option is disabled. Default is `false`.
118
119 ## Inject CSS variables
120
121 The [`css.Build`][] function has a [`vars`][] option that can be used to inject CSS variables into your stylesheets. This is particularly useful for dynamically setting values based on your site's configuration or other data. To use this with Tailwind CSS, you can use `css.Build` as a preprocessor step before passing the result to `css.TailwindCSS`. Here's how you can do it:
122
123 ```go-html-template
124 {{ with resources.Get "css/styles.css" }}
125 {{ $cssOpts := dict
126 "vars" (dict "favourite-color" "#7f93c9")
127 "externals" (slice "tailwindcss")
128 }}
129 {{ $tailwindOpts := dict "disableInlineImports" true }}
130 {{ with . | css.Build $cssOpts | css.TailwindCSS $tailwindOpts }}
131 <link rel="stylesheet" href="{{ .RelPermalink }}">
132 {{ end }}
133 {{ end }}
134 ```
135
136 Some notes to the above:
137
138 - Marking `tailwindcss` as an external in the `css.Build` options prevents it from being processed by the build step, allowing it to be correctly handled by the Tailwind CSS CLI in the subsequent step.
139 - The `disableInlineImports` option is set to `true` for the Tailwind CSS step as imports are handled by the `css.Build`.
140
141 [modern browser]: https://tailwindcss.com/docs/compatibility#browser-support
142 [standalone binary]: https://github.com/tailwindlabs/tailwindcss/releases/latest
143 [`css.Build`]: /functions/css/build/
144 [`vars`]: /functions/css/build/#vars