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 [security.exec]
61 allow = ['^(dart-)?sass$', '^go$', '^git$', '^node$', '^postcss$', '^tailwindcss$']
62 {{< /code-toggle >}}
63
64 Step 3
65 : Create a CSS entry file:
66
67 ```css {file="assets/css/main.css" copy=true}
68 @import "tailwindcss";
69 @plugin "@tailwindcss/typography";
70 @source "hugo_stats.json";
71 ```
72
73 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.
74
75 Step 4
76 : Create a _partial_ template to process the CSS with the Tailwind CSS CLI:
77
78 ```go-html-template {file="layouts/_partials/css.html" copy=true}
79 {{ with resources.Get "css/main.css" }}
80 {{ $opts := dict "minify" (not hugo.IsDevelopment) }}
81 {{ with . | css.TailwindCSS $opts }}
82 {{ if hugo.IsDevelopment }}
83 <link rel="stylesheet" href="{{ .RelPermalink }}">
84 {{ else }}
85 {{ with . | fingerprint }}
86 <link rel="stylesheet" href="{{ .RelPermalink }}" integrity="{{ .Data.Integrity }}" crossorigin="anonymous">
87 {{ end }}
88 {{ end }}
89 {{ end }}
90 {{ end }}
91 ```
92
93 Step 5
94 : Call the _partial_ template from your _base_ template, deferring template execution until after all sites and output formats have been rendered:
95
96 ```go-html-template {file="layouts/baseof.html" copy=true}
97 <head>
98 {{ with (templates.Defer (dict "key" "global")) }}
99 {{ partial "css.html" . }}
100 {{ end }}
101 </head>
102 ```
103
104 ## Options
105
106 The `css.TailwindCSS` function accepts an options map.
107
108 `disableInlineImports`
109 : {{< new-in 0.147.4 />}}
110 : (`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`.
111
112 `importContext`
113 : {{< new-in 0.165.0 />}}
114 : (`resource.ResourceGetter`) A [resource getter](g) to use when resolving `@import` statements. Hugo searches this context first, matching by the path as written in the statement, before falling back to the file system. Has no effect when [`disableInlineImports`](#disableinlineimports) is `true`.
115
116 `minify`
117 : (`bool`) Whether to optimize and minify the output. Default is `false`.
118
119 `optimize`
120 : (`bool`) Whether to optimize the output without minifying. Default is `false`.
121
122 `skipInlineImportsNotFound`
123 : (`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`.
124
125 ## Inject CSS variables
126
127 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:
128
129 ```go-html-template
130 {{ with resources.Get "css/styles.css" }}
131 {{ $cssOpts := dict
132 "vars" (dict "favourite-color" "#7f93c9")
133 "externals" (slice "tailwindcss")
134 }}
135 {{ $tailwindOpts := dict "disableInlineImports" true }}
136 {{ with . | css.Build $cssOpts | css.TailwindCSS $tailwindOpts }}
137 <link rel="stylesheet" href="{{ .RelPermalink }}">
138 {{ end }}
139 {{ end }}
140 ```
141
142 Some notes to the above:
143
144 - 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.
145 - The `disableInlineImports` option is set to `true` for the Tailwind CSS step as imports are handled by the `css.Build`.
146
147 [modern browser]: https://tailwindcss.com/docs/compatibility#browser-support
148 [standalone binary]: https://github.com/tailwindlabs/tailwindcss/releases/latest
149 [`css.Build`]: /functions/css/build/
150 [`vars`]: /functions/css/build/#vars