Skip to content

Commit d587968

Browse files
authored
feat: add custom css properties to change fonts (#890)
1 parent 94175a3 commit d587968

File tree

7 files changed

+75
-14
lines changed

7 files changed

+75
-14
lines changed

exampleSite/content/en/features/theming/_index.md

+29
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,33 @@ If you want to add more Favicon formats you have to [overwrite](https://gohugo.i
5858
<meta name="theme-color" content="#efefef">
5959
```
6060

61+
## Fonts
62+
63+
To use a custom font, it needs to be specified first. While there are many ways to do this, we recommend to use `@font-face` as it supports local as well as remote fonts. If you want to serve the fonts from your own server, you have to place them in the `static/fonts` folder of your project.
64+
65+
The font registration is done in the `custom.css` file. There are also a few custom CSS properties available to simplify the usage of custom fonts.
66+
67+
**Example:**
68+
69+
<!-- prettier-ignore -->
70+
```css
71+
@font-face {
72+
font-family: "DancingScript";
73+
src:
74+
url("fonts/DancingScript.woff2") format("woff2"),
75+
url("fonts/DancingScript.woff") format("woff");
76+
font-weight: normal;
77+
font-style: normal;
78+
font-display: swap;
79+
}
80+
81+
:root {
82+
--code-max-height: 60rem;
83+
84+
--header-font-family: "DancingScript";
85+
--body-font-family: "DancingScript";
86+
--code-font-family: "DancingScript";
87+
}
88+
```
89+
6190
Happy customizing!

exampleSite/static/custom.css.example

+14
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
:root {
44
--code-max-height: 60rem;
5+
6+
--header-font-family: "DancingScript";
7+
--body-font-family: "DancingScript";
8+
--code-font-family: "DancingScript";
59
}
610

711
/* Light mode theming */
@@ -152,3 +156,13 @@
152156
--footer-link-color-visited: #ffcc5c;
153157
}
154158
}
159+
160+
@font-face {
161+
font-family: "DancingScript";
162+
src:
163+
url("fonts/DancingScript.woff2") format("woff2"),
164+
url("fonts/DancingScript.woff") format("woff");
165+
font-weight: normal;
166+
font-style: normal;
167+
font-display: swap;
168+
}

src/sass/_base.scss

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1-
:root,
2-
:root[color-theme="light"] {
1+
:root {
32
--code-max-height: none;
43

4+
--header-font-family: #{meta.inspect($header-font-family)};
5+
--body-font-family: #{meta.inspect($body-font-family)};
6+
--code-font-family: #{meta.inspect($code-font-family)};
7+
}
8+
9+
:root,
10+
:root[color-theme="light"] {
511
@include color_theme_light;
612
@include code_theme_light;
713
}

src/sass/_defaults.scss

+3
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,12 @@ $link-color-visited: rgba(119, 73, 191, 1) !default;
4646
$link-color-footer: rgba(246, 107, 14, 1) !default;
4747

4848
$body-background: white !default;
49+
$body-font-family: "Liberation Sans", sans-serif !default;
4950
$body-font-color: $gray-800 !default;
5051
$body-font-weight: normal !default;
5152
$body-min-width: 20rem !default;
5253

54+
$code-font-family: "Liberation Mono", monospace !default;
5355
$code-font-color: rgba(70, 70, 70, 1) !default;
5456
$code-font-color-dark: rgba(185, 185, 185, 1) !default;
5557

@@ -68,6 +70,7 @@ $link-color-visited-dark: rgba(186, 142, 240) !default;
6870
$code-background: $gray-100 !default;
6971
$code-background-dark: darken($body-background-dark, 3) !default;
7072

73+
$header-font-family: "Metropolis", sans-serif !default;
7174
$header-height: 3.5rem !default;
7275
$menu-width: 18rem !default;
7376

src/sass/_fonts.scss

+18-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
@font-face {
22
font-family: "Liberation Sans";
3-
src: url("fonts/LiberationSans-Bold.woff2") format("woff2"),
3+
src:
4+
url("fonts/LiberationSans-Bold.woff2") format("woff2"),
45
url("fonts/LiberationSans-Bold.woff") format("woff");
56
font-weight: bold;
67
font-style: normal;
@@ -9,7 +10,8 @@
910

1011
@font-face {
1112
font-family: "Liberation Sans";
12-
src: url("fonts/LiberationSans-BoldItalic.woff2") format("woff2"),
13+
src:
14+
url("fonts/LiberationSans-BoldItalic.woff2") format("woff2"),
1315
url("fonts/LiberationSans-BoldItalic.woff") format("woff");
1416
font-weight: bold;
1517
font-style: italic;
@@ -18,7 +20,8 @@
1820

1921
@font-face {
2022
font-family: "Liberation Sans";
21-
src: url("fonts/LiberationSans-Italic.woff2") format("woff2"),
23+
src:
24+
url("fonts/LiberationSans-Italic.woff2") format("woff2"),
2225
url("fonts/LiberationSans-Italic.woff") format("woff");
2326
font-weight: normal;
2427
font-style: italic;
@@ -27,7 +30,8 @@
2730

2831
@font-face {
2932
font-family: "Liberation Sans";
30-
src: url("fonts/LiberationSans.woff2") format("woff2"),
33+
src:
34+
url("fonts/LiberationSans.woff2") format("woff2"),
3135
url("fonts/LiberationSans.woff") format("woff");
3236
font-weight: normal;
3337
font-style: normal;
@@ -36,7 +40,8 @@
3640

3741
@font-face {
3842
font-family: "Liberation Mono";
39-
src: url("fonts/LiberationMono.woff2") format("woff2"),
43+
src:
44+
url("fonts/LiberationMono.woff2") format("woff2"),
4045
url("fonts/LiberationMono.woff") format("woff");
4146
font-weight: normal;
4247
font-style: normal;
@@ -45,30 +50,33 @@
4550

4651
@font-face {
4752
font-family: "Metropolis";
48-
src: url("fonts/Metropolis.woff2") format("woff2"), url("fonts/Metropolis.woff") format("woff");
53+
src:
54+
url("fonts/Metropolis.woff2") format("woff2"),
55+
url("fonts/Metropolis.woff") format("woff");
4956
font-weight: normal;
5057
font-style: normal;
5158
font-display: swap;
5259
}
5360

5461
@font-face {
5562
font-family: "GeekdocIcons";
56-
src: url("fonts/GeekdocIcons.woff2") format("woff2"),
63+
src:
64+
url("fonts/GeekdocIcons.woff2") format("woff2"),
5765
url("fonts/GeekdocIcons.woff") format("woff");
5866
font-weight: normal;
5967
font-style: normal;
6068
font-display: swap;
6169
}
6270

6371
body {
64-
font-family: "Liberation Sans", sans-serif;
72+
font-family: var(--body-font-family);
6573
}
6674

6775
code,
6876
.gdoc-error__title {
69-
font-family: "Liberation Mono", monospace;
77+
font-family: var(--code-font-family);
7078
}
7179

7280
.gdoc-header {
73-
font-family: "Metropolis", sans-serif;
81+
font-family: var(--header-font-family);
7482
}

src/sass/_shortcodes.scss

+2-2
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@
196196

197197
// {{< mermaid >}}
198198
.gdoc-mermaid {
199-
font-family: "Liberation Sans", sans-serif;
199+
font-family: var(--body-font-family);
200200

201201
// Fix height of mermaid SVG elements (see https://github.com/mermaid-js/mermaid/issues/2481)
202202
> svg {
@@ -211,7 +211,7 @@
211211
&__default {
212212
padding: 0;
213213
margin: 0;
214-
font-family: "Liberation Mono", monospace;
214+
font-family: var(--code-font-family);
215215
}
216216

217217
&__meta {

src/sass/main.scss

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
@use "sass:map";
2+
@use "sass:meta";
23

34
@import "_defaults";
45
@import "_color_mode";

0 commit comments

Comments
 (0)