| 1 |
--- |
| 2 |
title: css.ChromaStyles |
| 3 |
description: Returns a CSS stylesheet for the syntax highlighter. |
| 4 |
categories: [] |
| 5 |
keywords: [highlight] |
| 6 |
params: |
| 7 |
functions_and_methods: |
| 8 |
aliases: [] |
| 9 |
returnType: resource.Resource |
| 10 |
signatures: ['css.ChromaStyles OPTIONS'] |
| 11 |
--- |
| 12 |
|
| 13 |
{{< new-in 0.165.0 />}} |
| 14 |
|
| 15 |
The `css.ChromaStyles` function returns a CSS stylesheet for the syntax highlighter as a `Resource` object. This stylesheet is needed when the `noClasses` option is `false`, either as a [global default][] in your project configuration or as an override when using any of the following: |
| 16 |
|
| 17 |
- The [`highlight`][] shortcode |
| 18 |
- The [`transform.Highlight`][] function |
| 19 |
- The [`transform.HighlightCodeBlock`][] function |
| 20 |
- Highlight options in the [info string](g) of a fenced code block in Markdown |
| 21 |
|
| 22 |
Hugo caches the result, so calling the function multiple times with the same options incurs no additional overhead. |
| 23 |
|
| 24 |
## Options |
| 25 |
|
| 26 |
The `css.ChromaStyles` function requires an options map. The [`targetPath`](#targetpath) is the only required option. |
| 27 |
|
| 28 |
`classDark` |
| 29 |
: (`string`) When [`modeSelector`](#modeselector) is `true` and [`mode`](#mode) is `dark`, the CSS class name used to scope selectors. Default is `dark`. |
| 30 |
|
| 31 |
`classLight` |
| 32 |
: (`string`) When [`modeSelector`](#modeselector) is `true` and [`mode`](#mode) is `light`, the CSS class name used to scope selectors. Default is `light`. |
| 33 |
|
| 34 |
`highlightStyle` |
| 35 |
: (`string`) The foreground and background colors for highlighted lines, such as `#fff000 bg:#000fff`. Defaults to the colors defined by the selected [`style`](#style). |
| 36 |
|
| 37 |
`lineNumbersInlineStyle` |
| 38 |
: (`string`) The foreground and background colors for inline line numbers, such as `#fff000 bg:#000fff`. Defaults to the colors defined by the selected [`style`](#style). |
| 39 |
|
| 40 |
`lineNumbersTableStyle` |
| 41 |
: (`string`) The foreground and background colors for table line numbers, such as `#fff000 bg:#000fff`. Defaults to the colors defined by the selected [`style`](#style). |
| 42 |
|
| 43 |
`mode` |
| 44 |
: (`string`) The color [mode][mode], either `light` or `dark`. The specified style must support the given mode. If you omit this option, Hugo uses the style's default mode. |
| 45 |
|
| 46 |
`modeSelector` |
| 47 |
: (`bool`) Whether to scope CSS selectors under a top-level mode class. For example, a `light` mode stylesheet scopes selectors under `.light`, producing `.light .chroma` instead of `.chroma`. Set to `true` when generating a paired light/dark stylesheet. Default is `false`. |
| 48 |
|
| 49 |
`omitClassComments` |
| 50 |
: (`bool`) Whether to omit CSS class comment prefixes in the generated stylesheet. Default is `false`. |
| 51 |
|
| 52 |
`style` |
| 53 |
: (`string`) The syntax highlighting style. Defaults to the [`style`][] value in your project configuration. See [syntax highlighting styles][] for a list of available styles. |
| 54 |
|
| 55 |
`targetPath` |
| 56 |
: (`string`) The target path of the resource, relative to the [`publishDir`][]. Required. |
| 57 |
|
| 58 |
## Examples |
| 59 |
|
| 60 |
In the first two examples, call the _partial_ template from your _base_ template using the [`partials.IncludeCached`][] function. Both examples assume this project configuration: |
| 61 |
|
| 62 |
{{< code-toggle file=hugo copy=true >}} |
| 63 |
[markup.highlight] |
| 64 |
noClasses = false |
| 65 |
style = 'github' |
| 66 |
{{< /code-toggle >}} |
| 67 |
|
| 68 |
### Single stylesheet |
| 69 |
|
| 70 |
To generate and include a stylesheet using the [`style`][] value in your project configuration: |
| 71 |
|
| 72 |
```go-html-template {file="layouts/_partials/highlight.html" copy=true} |
| 73 |
{{ $opts := dict "targetPath" "css/highlight.css" }} |
| 74 |
{{ with css.ChromaStyles $opts }} |
| 75 |
<link rel="stylesheet" href="{{ .RelPermalink }}"> |
| 76 |
{{ end }} |
| 77 |
``` |
| 78 |
|
| 79 |
### Light and dark stylesheets |
| 80 |
|
| 81 |
To generate and include paired light and dark stylesheets for a style that supports both [modes][mode]: |
| 82 |
|
| 83 |
```go-html-template {file="layouts/_partials/highlight.html" copy=true} |
| 84 |
{{ $opts := dict |
| 85 |
"mode" "light" |
| 86 |
"targetPath" "css/highlight-light.css" |
| 87 |
}} |
| 88 |
{{ with css.ChromaStyles $opts }} |
| 89 |
<link rel="stylesheet" href="{{ .RelPermalink }}"> |
| 90 |
{{ end }} |
| 91 |
|
| 92 |
{{ $opts := dict |
| 93 |
"mode" "dark" |
| 94 |
"modeSelector" true |
| 95 |
"targetPath" "css/highlight-dark.css" |
| 96 |
}} |
| 97 |
{{ with css.ChromaStyles $opts }} |
| 98 |
<link rel="stylesheet" href="{{ .RelPermalink }}"> |
| 99 |
{{ end }} |
| 100 |
``` |
| 101 |
|
| 102 |
The light stylesheet is unscoped and acts as the default. The dark stylesheet's selectors are scoped under the `dark` class, so its rules take effect only when the root element has that class. |
| 103 |
|
| 104 |
### Complete example |
| 105 |
|
| 106 |
This example adds a light/dark/system theme switcher to your site, using the [`css.Build`][] function to bundle the generated stylesheets into your main CSS file. |
| 107 |
|
| 108 |
Step 1 |
| 109 |
: Add this to your project configuration: |
| 110 |
|
| 111 |
{{< code-toggle file=hugo copy=true >}} |
| 112 |
[markup.highlight] |
| 113 |
noClasses = false |
| 114 |
style = 'github' |
| 115 |
{{< /code-toggle >}} |
| 116 |
|
| 117 |
Step 2 |
| 118 |
: Create a CSS entry file with `@import` statements for the generated stylesheets, plus light and dark rules for the rest of the page: |
| 119 |
|
| 120 |
```css {file="assets/css/main.css" copy=true} |
| 121 |
@import "./highlight-light.css"; |
| 122 |
@import "./highlight-dark.css"; |
| 123 |
|
| 124 |
html { |
| 125 |
background-color: #fff; |
| 126 |
color: #000; |
| 127 |
color-scheme: light; |
| 128 |
} |
| 129 |
|
| 130 |
a { |
| 131 |
color: #00e; |
| 132 |
} |
| 133 |
|
| 134 |
html.dark { |
| 135 |
background-color: #000; |
| 136 |
color: #fff; |
| 137 |
color-scheme: dark; |
| 138 |
} |
| 139 |
|
| 140 |
html.dark a { |
| 141 |
color: #6af; |
| 142 |
} |
| 143 |
``` |
| 144 |
|
| 145 |
Step 3 |
| 146 |
: Create a _partial_ template to generate the stylesheets and bundle them with the CSS entry file: |
| 147 |
|
| 148 |
```go-html-template {file="layouts/_partials/css.html" copy=true} |
| 149 |
{{ $opts := dict |
| 150 |
"mode" "light" |
| 151 |
"targetPath" "css/highlight-light.css" |
| 152 |
}} |
| 153 |
{{ $highlightLight := css.ChromaStyles $opts }} |
| 154 |
|
| 155 |
{{ $opts := dict |
| 156 |
"mode" "dark" |
| 157 |
"modeSelector" true |
| 158 |
"targetPath" "css/highlight-dark.css" |
| 159 |
}} |
| 160 |
{{ $highlightDark := css.ChromaStyles $opts }} |
| 161 |
|
| 162 |
{{ with resources.Get "css/main.css" }} |
| 163 |
{{ $opts := dict |
| 164 |
"importContext" (slice $highlightLight $highlightDark) |
| 165 |
"minify" (cond hugo.IsDevelopment false true) |
| 166 |
"sourceMap" (cond hugo.IsDevelopment "linked" "none") |
| 167 |
}} |
| 168 |
{{ with . | css.Build $opts }} |
| 169 |
{{ if hugo.IsDevelopment }} |
| 170 |
<link rel="stylesheet" href="{{ .RelPermalink }}"> |
| 171 |
{{ else }} |
| 172 |
{{ with . | fingerprint }} |
| 173 |
<link rel="stylesheet" href="{{ .RelPermalink }}" integrity="{{ .Data.Integrity }}" crossorigin="anonymous"> |
| 174 |
{{ end }} |
| 175 |
{{ end }} |
| 176 |
{{ end }} |
| 177 |
{{ end }} |
| 178 |
``` |
| 179 |
|
| 180 |
The [`importContext`][] option makes the generated stylesheets available when `css.Build` resolves the `@import` statements in the CSS entry file. |
| 181 |
|
| 182 |
Step 4 |
| 183 |
: Create a JavaScript file to control the theme switcher, persisting manual selections in local storage and following the operating system's preference when the visitor selects the system option: |
| 184 |
|
| 185 |
```js {file="assets/js/main.js" copy=true} |
| 186 |
const root = document.documentElement; |
| 187 |
const fieldset = document.getElementById('theme-switcher'); |
| 188 |
const mq = window.matchMedia('(prefers-color-scheme: dark)'); |
| 189 |
|
| 190 |
const applyTheme = (theme) => { |
| 191 |
if (theme === 'dark') { |
| 192 |
root.classList.add('dark'); |
| 193 |
} else if (theme === 'light') { |
| 194 |
root.classList.remove('dark'); |
| 195 |
} else { |
| 196 |
root.classList.toggle('dark', mq.matches); |
| 197 |
} |
| 198 |
fieldset.querySelector(`input[value="${theme ?? 'system'}"]`).checked = true; |
| 199 |
}; |
| 200 |
|
| 201 |
// Keep class in sync with system preference when no manual override is set. |
| 202 |
mq.addEventListener('change', () => { |
| 203 |
if (!localStorage.getItem('theme')) { |
| 204 |
root.classList.toggle('dark', mq.matches); |
| 205 |
} |
| 206 |
}); |
| 207 |
|
| 208 |
applyTheme(localStorage.getItem('theme')); |
| 209 |
|
| 210 |
fieldset.addEventListener('change', (e) => { |
| 211 |
const next = e.target.value === 'system' ? null : e.target.value; |
| 212 |
if (next === null) { |
| 213 |
localStorage.removeItem('theme'); |
| 214 |
} else { |
| 215 |
localStorage.setItem('theme', next); |
| 216 |
} |
| 217 |
applyTheme(next); |
| 218 |
}); |
| 219 |
``` |
| 220 |
|
| 221 |
Step 5 |
| 222 |
: Create a _partial_ template to process the JavaScript: |
| 223 |
|
| 224 |
```go-html-template {file="layouts/_partials/js.html" copy=true} |
| 225 |
{{ with resources.Get "js/main.js" }} |
| 226 |
{{ $opts := dict |
| 227 |
"minify" (cond hugo.IsDevelopment false true) |
| 228 |
"sourceMap" (cond hugo.IsDevelopment "linked" "none") |
| 229 |
}} |
| 230 |
{{ with . | js.Build $opts }} |
| 231 |
{{ if hugo.IsDevelopment }} |
| 232 |
<script defer src="{{ .RelPermalink }}"></script> |
| 233 |
{{ else }} |
| 234 |
{{ with . | fingerprint }} |
| 235 |
<script defer src="{{ .RelPermalink }}" integrity="{{ .Data.Integrity }}" crossorigin="anonymous"></script> |
| 236 |
{{ end }} |
| 237 |
{{ end }} |
| 238 |
{{ end }} |
| 239 |
{{ end }} |
| 240 |
``` |
| 241 |
|
| 242 |
Step 6 |
| 243 |
: Call both _partial_ templates from your _base_ template and add the theme switcher markup. The inline script in the `head` element applies the theme before the first paint, preventing a flash of light-themed content when a dark-theme visitor loads a page: |
| 244 |
|
| 245 |
```go-html-template {file="layouts/baseof.html" copy=true} |
| 246 |
<!DOCTYPE html> |
| 247 |
<html lang="{{ site.Language.Locale }}"> |
| 248 |
<head> |
| 249 |
<meta charset="utf-8"> |
| 250 |
<meta name="viewport" content="width=device-width, initial-scale=1"> |
| 251 |
<title>{{ site.Title }}</title> |
| 252 |
<script> |
| 253 |
const theme = localStorage.getItem('theme'); |
| 254 |
document.documentElement.classList.toggle('dark', |
| 255 |
theme === 'dark' || |
| 256 |
(!theme && window.matchMedia('(prefers-color-scheme: dark)').matches)); |
| 257 |
</script> |
| 258 |
{{ partialCached "css.html" . }} |
| 259 |
{{ partialCached "js.html" . }} |
| 260 |
</head> |
| 261 |
<body> |
| 262 |
<header> |
| 263 |
<fieldset id="theme-switcher"> |
| 264 |
<legend>Color scheme</legend> |
| 265 |
<label><input type="radio" name="theme" value="light"> Light</label> |
| 266 |
<label><input type="radio" name="theme" value="dark"> Dark</label> |
| 267 |
<label><input type="radio" name="theme" value="system"> System</label> |
| 268 |
</fieldset> |
| 269 |
</header> |
| 270 |
<main> |
| 271 |
{{ block "main" . }}{{ end }} |
| 272 |
</main> |
| 273 |
</body> |
| 274 |
</html> |
| 275 |
``` |
| 276 |
|
| 277 |
Step 7 |
| 278 |
: To verify the setup, add a fenced code block to your home page: |
| 279 |
|
| 280 |
````md {file="content/_index.md" copy=true} |
| 281 |
```go |
| 282 |
func printGreeting(showGreeting bool) { |
| 283 |
if showGreeting { |
| 284 |
fmt.Println("Hello, World!") |
| 285 |
} |
| 286 |
} |
| 287 |
``` |
| 288 |
```` |
| 289 |
|
| 290 |
[`css.Build`]: /functions/css/build/ |
| 291 |
[`highlight`]: /shortcodes/highlight/ |
| 292 |
[`importContext`]: /functions/css/build/#importcontext |
| 293 |
[`partials.IncludeCached`]: /functions/partials/includecached/ |
| 294 |
[`publishDir`]: /configuration/all/#publishdir |
| 295 |
[`style`]: /configuration/markup/#style |
| 296 |
[`transform.HighlightCodeBlock`]: /functions/transform/highlightcodeblock/ |
| 297 |
[`transform.Highlight`]: /functions/transform/highlight/ |
| 298 |
[global default]: /configuration/markup/#noclasses |
| 299 |
[mode]: /quick-reference/syntax-highlighting-styles/#modes |
| 300 |
[syntax highlighting styles]: /quick-reference/syntax-highlighting-styles/ |