| 1 |
--- |
| 2 |
title: css.Sass |
| 3 |
description: Returns a resource created by transpiling the given Sass resource to CSS. |
| 4 |
categories: [] |
| 5 |
keywords: [] |
| 6 |
params: |
| 7 |
functions_and_methods: |
| 8 |
aliases: [toCSS] |
| 9 |
returnType: resource.Resource |
| 10 |
signatures: ['css.Sass [OPTIONS] RESOURCE'] |
| 11 |
aliases: [/functions/resources/tocss/] |
| 12 |
--- |
| 13 |
|
| 14 |
Transpile Sass to CSS using the LibSass transpiler included in Hugo's extended and extended/deploy editions, or [install Dart Sass](#dart-sass) to use the latest features of the Sass language. |
| 15 |
|
| 16 |
<!-- TODO |
| 17 |
Remove the admonition below somewhere after v0.168.0, 15 minor releases |
| 18 |
after deprecation. |
| 19 |
--> |
| 20 |
|
| 21 |
> [!WARNING] |
| 22 |
> The embedded LibSass transpiler was deprecated in [v0.153.0][] and will be removed in a future release. Use the Dart Sass transpiler instead by setting the `transpiler` option to `dartsass` as shown in the examples below. |
| 23 |
|
| 24 |
Sass has two forms of syntax: [SCSS][] and [indented][]. Hugo supports both. |
| 25 |
|
| 26 |
## Options |
| 27 |
|
| 28 |
The `css.Sass` function accepts an options map. |
| 29 |
|
| 30 |
`enableSourceMap` |
| 31 |
: (`bool`) Whether to generate a source map. Default is `false`. |
| 32 |
|
| 33 |
```go-html-template |
| 34 |
{{ $opts := dict |
| 35 |
"transpiler" "dartsass" |
| 36 |
"enableSourceMap" true |
| 37 |
}} |
| 38 |
{{ $r := resources.Get "sass/main.scss" | css.Sass $opts }} |
| 39 |
``` |
| 40 |
|
| 41 |
`importContext` |
| 42 |
: {{< new-in 0.165.0 />}} |
| 43 |
: (`resource.ResourceGetter`) A [resource getter](g) to use when resolving `@use` and `@import` statements. Hugo searches this context first, matching by the path as written in the statement, before falling back to the file system. Applicable to Dart Sass. |
| 44 |
|
| 45 |
`includePaths` |
| 46 |
: (`slice`) A slice of paths, relative to the project root, that the transpiler will use when resolving `@use` and `@import` statements. |
| 47 |
|
| 48 |
```go-html-template |
| 49 |
{{ $opts := dict |
| 50 |
"transpiler" "dartsass" |
| 51 |
"includePaths" (slice "node_modules/bootstrap/scss") |
| 52 |
}} |
| 53 |
{{ $r := resources.Get "sass/main.scss" | css.Sass $opts }} |
| 54 |
``` |
| 55 |
|
| 56 |
`outputStyle` |
| 57 |
: (`string`) The output style of the resulting CSS. With LibSass, one of `nested` (default), `expanded`, `compact`, or `compressed`. With Dart Sass, either `expanded` (default) or `compressed`. |
| 58 |
|
| 59 |
```go-html-template |
| 60 |
{{ $opts := dict |
| 61 |
"transpiler" "dartsass" |
| 62 |
"outputStyle" "compressed" |
| 63 |
}} |
| 64 |
{{ $r := resources.Get "sass/main.scss" | css.Sass $opts }} |
| 65 |
``` |
| 66 |
|
| 67 |
`precision` |
| 68 |
: (`int`) The precision of floating point math. Applicable to LibSass. Default is `8`. |
| 69 |
|
| 70 |
```go-html-template |
| 71 |
{{ $opts := dict |
| 72 |
"transpiler" "dartsass" |
| 73 |
"precision" 10 |
| 74 |
}} |
| 75 |
{{ $r := resources.Get "sass/main.scss" | css.Sass $opts }} |
| 76 |
``` |
| 77 |
|
| 78 |
`silenceDeprecations` |
| 79 |
: {{< new-in 0.139.0 />}} |
| 80 |
: (`slice`) A slice of deprecation IDs to silence. IDs are enclosed in brackets within Dart Sass warning messages (e.g., `import` in `WARN Dart Sass: DEPRECATED [import]`). Applicable to Dart Sass. |
| 81 |
|
| 82 |
```go-html-template |
| 83 |
{{ $opts := dict |
| 84 |
"transpiler" "dartsass" |
| 85 |
"silenceDeprecations" (slice "import") |
| 86 |
}} |
| 87 |
{{ $r := resources.Get "sass/main.scss" | css.Sass $opts }} |
| 88 |
``` |
| 89 |
|
| 90 |
`silenceDependencyDeprecations` |
| 91 |
: {{< new-in 0.146.0 />}} |
| 92 |
: (`bool`) Whether to silence deprecation warnings from dependencies, where a dependency is considered any file transitively imported through a load path. This does not apply to `@warn` or `@debug` rules. Default is `false`. |
| 93 |
|
| 94 |
```go-html-template |
| 95 |
{{ $opts := dict |
| 96 |
"transpiler" "dartsass" |
| 97 |
"silenceDependencyDeprecations" true |
| 98 |
}} |
| 99 |
{{ $r := resources.Get "sass/main.scss" | css.Sass $opts }} |
| 100 |
``` |
| 101 |
|
| 102 |
`sourceMapIncludeSources` |
| 103 |
: (`bool`) Whether to embed sources in the generated source map. Applicable to Dart Sass. Default is `false`. |
| 104 |
|
| 105 |
```go-html-template |
| 106 |
{{ $opts := dict |
| 107 |
"transpiler" "dartsass" |
| 108 |
"enableSourceMap" true "sourceMapIncludeSources" true |
| 109 |
}} |
| 110 |
{{ $r := resources.Get "sass/main.scss" | css.Sass $opts }} |
| 111 |
``` |
| 112 |
|
| 113 |
`targetPath` |
| 114 |
: (`string`) The target path of the resource, relative to the [`publishDir`][]. If unset, the target path defaults to the asset's original path with a `.css` extension. |
| 115 |
|
| 116 |
```go-html-template |
| 117 |
{{ $opts := dict |
| 118 |
"transpiler" "dartsass" |
| 119 |
"targetPath" "css/bundle.css" |
| 120 |
}} |
| 121 |
{{ $r := resources.Get "sass/main.scss" | css.Sass $opts }} |
| 122 |
``` |
| 123 |
|
| 124 |
`transpiler` |
| 125 |
: (`string`) The transpiler to use, either `libsass` or `dartsass`. Hugo's extended and extended/deploy editions include the LibSass transpiler. To use the Dart Sass transpiler, see the [installation instructions](#dart-sass). Default is `libsass`. |
| 126 |
|
| 127 |
```go-html-template |
| 128 |
{{ $opts := dict "transpiler" "dartsass" }} |
| 129 |
{{ $r := resources.Get "sass/main.scss" | css.Sass $opts }} |
| 130 |
``` |
| 131 |
|
| 132 |
`vars` |
| 133 |
: (`map`) A map of key-value pairs used to generate Sass variables. The `css.Sass` function injects these variables into the stylesheet when it encounters the `hugo:vars` internal identifier within a `@use` or `@import` statement. |
| 134 |
|
| 135 |
```go-html-template |
| 136 |
{{ $vars := dict |
| 137 |
"font-family" "\"Times New Roman\", Times, serif" |
| 138 |
"font-size" "24px" |
| 139 |
"primary-color" "blue" |
| 140 |
}} |
| 141 |
{{ $opts := dict |
| 142 |
"transpiler" "dartsass" |
| 143 |
"vars" $vars |
| 144 |
}} |
| 145 |
{{ $r := resources.Get "sass/main.scss" | css.Sass $opts }} |
| 146 |
``` |
| 147 |
|
| 148 |
In the example above, using the identifier in your stylesheet allows you to access the values as Sass variables in the `hugo:vars` namespace: |
| 149 |
|
| 150 |
```scss |
| 151 |
@use 'hugo:vars' as v; |
| 152 |
|
| 153 |
.element { |
| 154 |
color: v.$primary-color; |
| 155 |
font-family: v.$font-family; |
| 156 |
font-size: v.$font-size; |
| 157 |
} |
| 158 |
``` |
| 159 |
|
| 160 |
The above produces output equivalent to: |
| 161 |
|
| 162 |
```css |
| 163 |
.element { |
| 164 |
color: blue; |
| 165 |
font-family: "Times New Roman", Times, serif; |
| 166 |
font-size: 24px; |
| 167 |
} |
| 168 |
``` |
| 169 |
|
| 170 |
{{< new-in 0.161.0 />}} |
| 171 |
|
| 172 |
The map may optionally contain nested maps. Each nested map is exposed as a separate `hugo:vars/<name>` namespace, where `<name>` is the key of the nested map (lowercased). Top-level scalar values and nested maps are independent. A top-level `@use 'hugo:vars'` only includes scalar values, while `@use 'hugo:vars/<name>'` only includes the scalars from the named nested map. |
| 173 |
|
| 174 |
```go-html-template |
| 175 |
{{ $vars := dict |
| 176 |
"font-family" "\"Times New Roman\", Times, serif" |
| 177 |
"font-size" "24px" |
| 178 |
"primary-color" "blue" |
| 179 |
"mobile" (dict |
| 180 |
"font-size" "12px" |
| 181 |
"primary-color" "red" |
| 182 |
) |
| 183 |
}} |
| 184 |
{{ $opts := dict |
| 185 |
"transpiler" "dartsass" |
| 186 |
"vars" $vars |
| 187 |
}} |
| 188 |
{{ $r := resources.Get "sass/main.scss" | css.Sass $opts }} |
| 189 |
``` |
| 190 |
|
| 191 |
In the stylesheet, reference each nested namespace with a separate `@use` statement. Assign an alias to access the variables from that namespace: |
| 192 |
|
| 193 |
```scss |
| 194 |
@use 'hugo:vars' as v; |
| 195 |
@use 'hugo:vars/mobile' as mobile; |
| 196 |
|
| 197 |
body { |
| 198 |
color: v.$primary-color; |
| 199 |
font-family: v.$font-family; |
| 200 |
font-size: v.$font-size; |
| 201 |
} |
| 202 |
|
| 203 |
@media (max-width: 650px) { |
| 204 |
body { |
| 205 |
color: mobile.$primary-color; |
| 206 |
font-size: mobile.$font-size; |
| 207 |
} |
| 208 |
} |
| 209 |
``` |
| 210 |
|
| 211 |
The above produces output equivalent to: |
| 212 |
|
| 213 |
```css |
| 214 |
body { |
| 215 |
color: blue; |
| 216 |
font-family: "Times New Roman", Times, serif; |
| 217 |
font-size: 24px; |
| 218 |
} |
| 219 |
|
| 220 |
@media (max-width: 650px) { |
| 221 |
body { |
| 222 |
color: red; |
| 223 |
font-size: 12px; |
| 224 |
} |
| 225 |
} |
| 226 |
``` |
| 227 |
|
| 228 |
The `vars` option is useful for setting Sass variables within your project configuration. |
| 229 |
|
| 230 |
{{< code-toggle file=hugo >}} |
| 231 |
[params.theme.style] |
| 232 |
font-family = '"Times New Roman", Times, serif' |
| 233 |
font-size = '24px' |
| 234 |
primary-color = 'blue' |
| 235 |
|
| 236 |
[params.theme.style.mobile] |
| 237 |
font-size = '12px' |
| 238 |
primary-color = 'red' |
| 239 |
{{< /code-toggle >}} |
| 240 |
|
| 241 |
```go-html-template |
| 242 |
{{ $opts := dict |
| 243 |
"transpiler" "dartsass" |
| 244 |
"vars" site.Params.theme.style }} |
| 245 |
{{ $r := resources.Get "sass/main.scss" | css.Sass $opts }} |
| 246 |
``` |
| 247 |
|
| 248 |
When passing a `vars` map to the `css.Sass` function, Hugo detects common typed CSS values such as `24px` or `#FF0000` using regular expression matching. If necessary, you can bypass automatic type inference by using the [`css.Quoted`][] or [`css.Unquoted`][] function to explicitly indicate a value's type. |
| 249 |
|
| 250 |
## Example |
| 251 |
|
| 252 |
```go-html-template {copy=true} |
| 253 |
{{ with resources.Get "sass/main.scss" }} |
| 254 |
{{ $opts := dict |
| 255 |
"enableSourceMap" hugo.IsDevelopment |
| 256 |
"outputStyle" (cond hugo.IsDevelopment "expanded" "compressed") |
| 257 |
"targetPath" "css/main.css" |
| 258 |
"transpiler" "dartsass" |
| 259 |
"vars" site.Params.styles |
| 260 |
"includePaths" (slice "node_modules/bootstrap/scss") |
| 261 |
}} |
| 262 |
{{ with . | css.Sass $opts }} |
| 263 |
{{ if hugo.IsDevelopment }} |
| 264 |
<link rel="stylesheet" href="{{ .RelPermalink }}"> |
| 265 |
{{ else }} |
| 266 |
{{ with . | fingerprint }} |
| 267 |
<link rel="stylesheet" href="{{ .RelPermalink }}" integrity="{{ .Data.Integrity }}" crossorigin="anonymous"> |
| 268 |
{{ end }} |
| 269 |
{{ end }} |
| 270 |
{{ end }} |
| 271 |
{{ end }} |
| 272 |
``` |
| 273 |
|
| 274 |
## Dart Sass |
| 275 |
|
| 276 |
<!-- TODO |
| 277 |
Revise the paragraphs below somewhere after v0.168.0, 15 minor releases |
| 278 |
after the deprecation of the LibSass transpiler. |
| 279 |
--> |
| 280 |
|
| 281 |
Hugo's extended and extended/deploy editions include [LibSass][] to transpile Sass to CSS. In 2020, the Sass team deprecated LibSass in favor of [Dart Sass][]. |
| 282 |
|
| 283 |
Use the latest features of the Sass language by installing Dart Sass in your development and production environments. |
| 284 |
|
| 285 |
### Installation overview |
| 286 |
|
| 287 |
Dart Sass is compatible with Hugo v0.114.0 and later. |
| 288 |
|
| 289 |
If you have been using Embedded Dart Sass[^1] with Hugo v0.113.0 and earlier, uninstall Embedded Dart Sass, then install Dart Sass. If you have installed both, Hugo will use Dart Sass. |
| 290 |
|
| 291 |
If you install Hugo as a [Snap package][] there is no need to install Dart Sass. The Hugo Snap package includes Dart Sass. |
| 292 |
|
| 293 |
### Development environment |
| 294 |
|
| 295 |
When you install Dart Sass somewhere in your PATH, Hugo will find it. |
| 296 |
|
| 297 |
OS | Package manager | Site | Installation |
| 298 |
:-------|:----------------|:-------------------|:----------------------------- |
| 299 |
Linux | Homebrew | [brew.sh][] | `brew install sass/sass/sass` |
| 300 |
Linux | Snap | [snapcraft.io][] | `sudo snap install dart-sass` |
| 301 |
macOS | Homebrew | [brew.sh][] | `brew install sass/sass/sass` |
| 302 |
Windows | Chocolatey | [chocolatey.org][] | `choco install sass` |
| 303 |
Windows | Scoop | [scoop.sh][] | `scoop install sass` |
| 304 |
|
| 305 |
You may also install [prebuilt binaries][] for Linux, macOS, and Windows. You must install the prebuilt binary outside of your project directory and ensure its path is included in your system's PATH environment variable. |
| 306 |
|
| 307 |
Run `hugo env` to list the active transpilers. |
| 308 |
|
| 309 |
> [!NOTE] |
| 310 |
> If you build Hugo from source and run `mage test -v`, the test will fail if you install Dart Sass as a Snap package. This is due to the Snap package's strict confinement model. |
| 311 |
|
| 312 |
### Production environment |
| 313 |
|
| 314 |
To use Dart Sass with Hugo on a [CI/CD](g) platform, you typically must modify your build workflow to install Dart Sass before the Hugo site build begins. This is because these platforms don't have Dart Sass pre-installed, and Hugo needs it to process your Sass files. |
| 315 |
|
| 316 |
There's one key exception where you can skip this step: you have committed your `resources` directory to your repository. This is only possible if: |
| 317 |
|
| 318 |
- You have not changed Hugo's default asset cache location. |
| 319 |
- You have not set [`useResourceCacheWhen`][] to never in your project configuration. |
| 320 |
|
| 321 |
By committing the `resources` directory, you're providing the pre-built CSS files directly to your CI/CD platform, so it doesn't need to run the Sass compilation itself. |
| 322 |
|
| 323 |
For examples of how to install Dart Sass in a production environment, see these hosting guides: |
| 324 |
|
| 325 |
- [Cloudflare][] |
| 326 |
- [GitHub Pages][] |
| 327 |
- [GitLab Pages][] |
| 328 |
- [Netlify][] |
| 329 |
- [Render][] |
| 330 |
- [SourceHut][] |
| 331 |
- [Vercel][] |
| 332 |
|
| 333 |
[^1]: In 2023, the Sass team deprecated Embedded Dart Sass in favor of Dart Sass. |
| 334 |
|
| 335 |
[Cloudflare]: /host-and-deploy/host-on-cloudflare/ |
| 336 |
[Dart Sass]: https://sass-lang.com/dart-sass/ |
| 337 |
[GitHub Pages]: /host-and-deploy/host-on-github-pages/ |
| 338 |
[GitLab Pages]: /host-and-deploy/host-on-gitlab-pages/ |
| 339 |
[LibSass]: https://sass-lang.com/libsass |
| 340 |
[Netlify]: /host-and-deploy/host-on-netlify/ |
| 341 |
[Render]: /host-and-deploy/host-on-render/ |
| 342 |
[SCSS]: https://sass-lang.com/documentation/syntax#scss |
| 343 |
[Snap package]: https://snapcraft.io/hugo |
| 344 |
[SourceHut]: /host-and-deploy/host-on-sourcehut-pages/ |
| 345 |
[Vercel]: /host-and-deploy/host-on-vercel/ |
| 346 |
[`css.Quoted`]: /functions/css/quoted/ |
| 347 |
[`css.Unquoted`]: /functions/css/unquoted/ |
| 348 |
[`publishDir`]: /configuration/all/#publishdir |
| 349 |
[`useResourceCacheWhen`]: /configuration/build/#useresourcecachewhen |
| 350 |
[brew.sh]: https://brew.sh/ |
| 351 |
[chocolatey.org]: https://community.chocolatey.org/packages/sass |
| 352 |
[indented]: https://sass-lang.com/documentation/syntax#the-indented-syntax |
| 353 |
[prebuilt binaries]: https://github.com/sass/dart-sass/releases/latest |
| 354 |
[scoop.sh]: https://scoop.sh/#/apps?q=sass |
| 355 |
[snapcraft.io]: https://snapcraft.io/dart-sass |
| 356 |
[v0.153.0]: https://github.com/gohugoio/hugo/releases/tag/v0.153.0 |