HUGO

  • News
  • Docs
  • Themes
  • Showcase
  • Community
  • GitHub
gohugoio Star
  • About Hugo
    • Overview
    • What is Hugo
    • Hugo features
    • Static site generators
    • Hugo's security model
    • Hugo and the GDPR
    • License
  • Installation
    • Overview
    • macOS
    • Linux
    • Windows
    • BSD
  • Getting started
    • Overview
    • Quick start
    • Basic usage
    • Directory structure
    • Configuration
    • Configure markup
    • Glossary of terms
    • External learning resources
  • Quick reference
    • Overview
    • Emojis
    • Functions
    • Methods
    • Page collections
  • Content management
    • Overview
    • Organization
    • Page bundles
    • Content formats
    • Front matter
    • Build options
    • Page resources
    • Image processing
    • Shortcodes
    • Related content
    • Sections
    • Content types
    • Archetypes
    • Taxonomies
    • Summaries
    • Links and cross references
    • URL management
    • Menus
    • Static files
    • Table of contents
    • Comments
    • Multilingual
    • Markdown attributes
    • Syntax highlighting
    • Diagrams
    • Mathematics
  • Templates
    • Overview
    • Templating
    • Template lookup order
    • Base templates and blocks
    • Single page templates
    • List templates
    • Homepage template
    • Section templates
    • Taxonomy templates
    • Pagination
    • Content view templates
    • Partial templates
    • Shortcode templates
    • Menu templates
    • Data templates
    • RSS templates
    • Sitemap templates
    • Internal templates
    • Custom output formats
    • 404 page
    • Robots.txt
  • Functions
    • Overview
    • cast
    • collections
    • compare
    • crypto
    • data
    • debug
    • diagrams
    • encoding
    • fmt
    • global
    • go template
    • hugo
    • images
    • inflect
    • js
    • lang
    • math
    • openapi3
    • os
    • partials
    • path
    • reflect
    • resources
    • safe
    • strings
    • templates
    • time
    • transform
    • urls
  • Methods
    • Overview
    • Duration
    • Menu
    • Menu entry
    • Page
    • Pages
    • Resource
    • Shortcode
    • Site
    • Taxonomy
    • Time
  • Render hooks
    • Overview
    • Introduction
    • Code blocks
    • Headings
    • Images
    • Links
  • Hugo Modules
    • Overview
    • Configure Hugo modules
    • Use Hugo Modules
    • Theme components
  • Hugo Pipes
    • Overview
    • Introduction
    • Transpile Sass to CSS
    • PostCSS
    • PostProcess
    • JavaScript building
    • Babel
    • Asset minification
    • Concatenating assets
    • Fingerprinting and SRI hashing
    • Resource from string
    • Resource from template
  • CLI
  • Troubleshooting
    • Overview
    • Audit
    • Logging
    • Inspection
    • Deprecation
    • Performance
    • FAQs
  • Developer tools
    • Overview
    • Editor plugins
    • Front-ends
    • Search
    • Migrations
    • Other projects
  • Hosting and deployment
    • Overview
    • Hugo Deploy
    • Deploy with Rclone
    • Deploy with Rsync
    • Host on 21YunBox
    • Host on AWS Amplify
    • Host on Azure Static Web Apps
    • Host on Cloudflare Pages
    • Host on Firebase
    • Host on GitHub Pages
    • Host on GitLab Pages
    • Host on KeyCDN
    • Host on Netlify
    • Host on Render
  • Contribute
    • Overview
    • Development
    • Documentation
    • Themes
  • Maintenance
FUNCTIONS RESOURCE FUNCTIONS

resources.PostCSS

Processes the given resource with PostCSS using any PostCSS plugin.

Syntax

resources.PostCSS [OPTIONS] RESOURCE

Returns

resource.Resource

Alias

postCSS
{{ with resources.Get "css/main.css" | postCSS }}
  <link rel="stylesheet" href="{{ .RelPermalink }}">
{{ end }}

Setup

Follow the steps below to transform CSS using any of the available PostCSS plugins.

Step 1
Install Node.js.
Step 2
Install the required Node.js packages in the root of your project. For example, to add vendor prefixes to your CSS rules:
npm i -D postcss postcss-cli autoprefixer
Step 3
Create a PostCSS configuration file in the root of your project. You must name this file postcss.config.js or another supported file name. For example:
module.exports = {
  plugins: [
    require('autoprefixer')
  ]
};

If you are a Windows user, and the path to your project contains a space, you must place the PostCSS configuration within the package.json file. See this example and issue #7333.

Step 4
Place your CSS file within the assets/css directory.
Step 5
Process the resource with PostCSS:
{{ with resources.Get "css/main.css" | postCSS }}
  <link rel="stylesheet" href="{{ .RelPermalink }}">
{{ end }}

Options

The resources.PostCSS method takes an optional map of options.

config
(string) The directory that contains the PostCSS configuration file. Default is the root of the project directory.
noMap
(bool) Default is false. If true, disables inline sourcemaps.
inlineImports
(bool) Default is false. Enable inlining of @import statements. It does so recursively, but will only import a file once. URL imports (e.g. @import url('https://fonts.googleapis.com/css?family=Open+Sans&display=swap');) and imports with media queries will be ignored. Note that this import routine does not care about the CSS spec, so you can have @import anywhere in the file. Hugo will look for imports relative to the module mount and will respect theme overrides.
skipInlineImportsNotFound
(bool) Default is false. Before Hugo 0.99.0 when inlineImports was enabled and we failed to resolve an import, we logged it as a warning. We now fail the build. If you have regular CSS imports in your CSS that you want to preserve, you can either use imports with URL or media queries (Hugo does not try to resolve those) or set skipInlineImportsNotFound to true.
{{ $opts := dict "config" "config-directory" "noMap" true }}
{{ with resources.Get "css/main.css" | postCSS $opts }}
  <link rel="stylesheet" href="{{ .RelPermalink }}">
{{ end }}

No configuration file

To avoid using a PostCSS configuration file, you can specify a minimal configuration using the options map.

use
(string) A space-delimited list of PostCSS plugins to use.
parser
(string) A custom PostCSS parser.
stringifier
(string) A custom PostCSS stringifier.
syntax
(string) Custom postcss syntax.
{{ $opts := dict "use" "autoprefixer postcss-color-alpha" }}
{{ with resources.Get "css/main.css" | postCSS $opts }}
  <link rel="stylesheet" href="{{ .RelPermalink }}">
{{ end }}

Check environment

The current Hugo environment name (set by --environment or in configuration or OS environment) is available in the Node context, which allows constructs like this:

const autoprefixer = require('autoprefixer');
const purgecss = require('@fullhuman/postcss-purgecss');
module.exports = {
  plugins: [
    autoprefixer,
    process.env.HUGO_ENVIRONMENT !== 'development' ? purgecss : null
  ]
}

See also

  • resources.Fingerprint
  • resources.Minify
  • resources.PostProcess
  • resources.ToCSS

On this page

  • Setup
  • Options
  • No configuration file
  • Check environment

In this section

  • resources.Babel
  • resources.ByType
  • resources.Concat
  • resources.Copy
  • resources.ExecuteAsTemplate
  • resources.Fingerprint
  • resources.FromString
  • resources.Get
  • resources.GetMatch
  • resources.GetRemote
  • resources.Match
  • resources.Minify
  • resources.PostCSS
  • resources.PostProcess
  • resources.ToCSS
Last updated: November 4, 2023: Namespace functions and methods (723a827f)
Improve this page
By the Hugo Authors
Hugo Logo
  • File an Issue
  • Get Help
  • @GoHugoIO
  • @spf13
  • @bepsays

Netlify badge

 

Hugo Sponsors

 

The Hugo logos are copyright © Steve Francia 2013–2024.

The Hugo Gopher is based on an original work by Renée French.

  • News
  • Docs
  • Themes
  • Showcase
  • Community
  • GitHub
  • About Hugo
  • Installation
  • Getting started
  • Quick reference
  • Content management
  • Templates
  • Functions
  • Methods
  • Render hooks
  • Hugo Modules
  • Hugo Pipes
  • CLI
  • Troubleshooting
  • Developer tools
  • Hosting and deployment
  • Contribute
  • Maintenance