css.TailwindCSS
- Syntax
- css.TailwindCSS [OPTIONS] RESOURCE
- Returns
- resource.Resource
Usage
The css.TailwindCSS function returns a resource created by processing the given resource with the Tailwind CSS CLI. The CLI scans your templates for Tailwind CSS utility classes, then generates a stylesheet that contains only the classes you use. To optimize the output, set the optimize or minify option.
Use this function with Tailwind CSS v4.0 or later. These versions require a modern browser to render correctly.
Requirements
To use this function, install Node.js, then install Tailwind CSS v4.0 or later and its command-line interface in the root of your project:
npm install --save-dev tailwindcss @tailwindcss/cliAs of v0.161.0, Hugo no longer supports the Tailwind CSS standalone binary. Install the Tailwind CSS CLI with npm as shown above.
Inject CSS variables
To inject CSS variables into your stylesheet, process the stylesheet with the css.Build function before you pass the result to the css.TailwindCSS function. Use the vars option of the css.Build function to set the variables, for example from your project configuration. When you do this:
- Add
tailwindcssto theexternalsoption of thecss.Buildfunction. Thecss.Buildfunction then leaves the@import "tailwindcss"statement unchanged, so the Tailwind CSS CLI can process it. - Set the
disableInlineImportsoption of thecss.TailwindCSSfunction totrue, because thecss.Buildfunction already replaced the other@importstatements.
File system access
Hugo runs the Node.js tool used by this function with a permission model that limits the files the tool can read to the paths in security.node.permissions.allowRead. By default, this is the project directory. If the tool needs to read files from other locations, such as configuration files, plugins, or imported files in the node_modules directory of a parent directory in a monorepo, add those paths to the list.
Options
The css.TailwindCSS function accepts an options map.
disableInlineImports- New in v0.147.4
- (
bool) Whether to disable the replacement of@importstatements with the content of the imported files. Hugo replaces the statements recursively, but imports each file only once. The import routine does not follow the CSS specification, so you can place@importstatements anywhere in the file. Default isfalse.Hugo does not replace imports that use the
url()functional notation, and the Tailwind CSS CLI leaves them unchanged in the generated CSS code.Hugo does not replace imports with media queries. Hugo passes these statements to the Tailwind CSS CLI, which resolves local paths relative to the project root, not relative to the importing file.
Hugo treats an import of an external URL without the
url()functional notation, such as@import "https://example.org/styles.css", as an import of a local file. The build fails unless you set theskipInlineImportsNotFoundoption totrue. importContext- New in v0.165.0
- (
resource.ResourceGetter) A resource getter to use when resolving@importstatements. Hugo searches this context first, before falling back to the file system. Hugo matches a resource in the context by the path relative to theassetsdirectory. For a path that begins with./or../, Hugo resolves the path relative to the importing file. For example, when theassets/css/main.cssfile contains@import "./sub/theme.css", a resource namedcss/sub/theme.cssmatches. For a bare path, Hugo also tries the path as written, so@import "sub/theme.css"matches a resource namedsub/theme.cssorcss/sub/theme.css. Has no effect whendisableInlineImportsistrue. minify- (
bool) Whether to optimize and minify the output. Default isfalse. optimize- (
bool) Whether to optimize the output without minifying. Default isfalse. skipInlineImportsNotFound- (
bool) Whether to continue the build when Hugo cannot resolve an@importstatement, preserving the original statement. The Tailwind CSS CLI then processes the statement. It leaves an import of an external URL unchanged in the generated CSS code, but the build fails if the statement references a local file that the CLI cannot find. Default isfalse.
Examples
These examples show how to set up Tailwind CSS in your project and how to inject CSS variables into a stylesheet.
Project setup
This example generates a stylesheet that contains only the Tailwind CSS utility classes that your site uses. Hugo records the classes in a build stats file, and Tailwind CSS reads that file.
- Step 1
- Install the typography plugin, which this example uses to style rendered Markdown:
npm install --save-dev @tailwindcss/typography - Step 2
- Add this to your project configuration:
build: buildStats: enable: true cachebusters: - source: 'assets/notwatching/hugo_stats\.json' target: css - source: '(postcss|tailwind)\.config\.js' target: css module: mounts: - source: assets target: assets - disableWatch: true source: hugo_stats.json target: assets/notwatching/hugo_stats.json security: exec: allow: - ^(dart-)?sass$ - ^go$ - ^git$ - ^node$ - ^postcss$ - ^tailwindcss$[build] [build.buildStats] enable = true [[build.cachebusters]] source = 'assets/notwatching/hugo_stats\.json' target = 'css' [[build.cachebusters]] source = '(postcss|tailwind)\.config\.js' target = 'css' [module] [[module.mounts]] source = 'assets' target = 'assets' [[module.mounts]] disableWatch = true source = 'hugo_stats.json' target = 'assets/notwatching/hugo_stats.json' [security] [security.exec] allow = ['^(dart-)?sass$', '^go$', '^git$', '^node$', '^postcss$', '^tailwindcss$']{ "build": { "buildStats": { "enable": true }, "cachebusters": [ { "source": "assets/notwatching/hugo_stats\\.json", "target": "css" }, { "source": "(postcss|tailwind)\\.config\\.js", "target": "css" } ] }, "module": { "mounts": [ { "source": "assets", "target": "assets" }, { "disableWatch": true, "source": "hugo_stats.json", "target": "assets/notwatching/hugo_stats.json" } ] }, "security": { "exec": { "allow": [ "^(dart-)?sass$", "^go$", "^git$", "^node$", "^postcss$", "^tailwindcss$" ] } } } - Step 3
- Create a CSS entry file:assets/css/main.css
@import "tailwindcss"; @plugin "@tailwindcss/typography"; @source "hugo_stats.json";Tailwind CSS ignores the files listed in your
.gitignorefile. If your.gitignorefile lists thehugo_stats.jsonfile, use the@sourcedirective as shown above to make the file available to Tailwind CSS. - Step 4
- Create a partial template to process the CSS with the Tailwind CSS CLI:layouts/_partials/css.html
{{ with resources.Get "css/main.css" }} {{ $opts := dict "minify" (not hugo.IsDevelopment) }} {{ with . | css.TailwindCSS $opts }} {{ if hugo.IsDevelopment }} <link rel="stylesheet" href="{{ .RelPermalink }}"> {{ else }} {{ with . | fingerprint }} <link rel="stylesheet" href="{{ .RelPermalink }}" integrity="{{ .Data.Integrity }}" crossorigin="anonymous"> {{ end }} {{ end }} {{ end }} {{ end }} - Step 5
- Call the partial template from your base template. Use the
templates.Deferfunction to defer execution until Hugo renders all sites and output formats:layouts/baseof.html<head> {{ with (templates.Defer (dict "key" "global")) }} {{ partial "css.html" . }} {{ end }} </head>
CSS variables
This example processes a stylesheet with the css.Build function to inject CSS variables, then passes the result to the css.TailwindCSS function:
{{ with resources.Get "css/main.css" }}
{{ $cssOpts := dict
"externals" (slice "tailwindcss")
"vars" (dict "favorite-color" "#7f93c9")
}}
{{ $tailwindOpts := dict "disableInlineImports" true }}
{{ with . | css.Build $cssOpts | css.TailwindCSS $tailwindOpts }}
<link rel="stylesheet" href="{{ .RelPermalink }}">
{{ end }}
{{ end }}Import the variables in the CSS entry file:
@import "tailwindcss";
@import "hugo:vars";
.highlight {
color: var(--favorite-color);
}