|
| 1 | +# Using a custom font |
| 2 | + |
| 3 | +This example uses the Noto Serif and Noto Mono font families to show |
| 4 | +how custom fonts can be used. These families were chosen because the |
| 5 | +[Noto fonts are well documented] and they are available under the |
| 6 | +[Open Font License]. |
| 7 | + |
| 8 | +[Noto fonts are well documented]: https://notofonts.github.io/noto-docs/ |
| 9 | +[Open Font License]: https://scripts.sil.org/ofl |
| 10 | + |
| 11 | +In this example, the fonts are loaded from a CDN (that is not Google) |
| 12 | +to keep the example itself small and allow you to use it as a template |
| 13 | +for your own site, without assuming what fonts you might want to use. |
| 14 | +We also describe how to use custom fonts that are hosted on your own |
| 15 | +server, see below. |
| 16 | + |
| 17 | +Here we have some text in: |
| 18 | + |
| 19 | +- Noto Serif |
| 20 | +- **Noto Serif bold** |
| 21 | +- *Noto Serif italic* |
| 22 | +- __*Noto Serif bold italic*__ |
| 23 | +- `Noto Mono` |
| 24 | +- __`Noto Mono bold`__ |
| 25 | + |
| 26 | +!!! tip "Privacy Plugin" |
| 27 | + |
| 28 | + Before you consider the examples below, you should know that loading fonts |
| 29 | + or other assets from a CDN may bring you into non-compliance with data |
| 30 | + protection legislation such as the GDPR in the EU. You can read more about |
| 31 | + this in the [ensuring data privacy] section of the documentation. |
| 32 | + |
| 33 | + The [privacy plugin] provided by Material for MkDocs provides a |
| 34 | + best-of-both-worlds solution in that it allows you to specify fonts |
| 35 | + available on Google Fonts directly in your `mkdocs.yml`. It automatically |
| 36 | + downloads the ones used and includes them in your website so they are served |
| 37 | + up together with your documentation. No need for a custom stylesheet. |
| 38 | + |
| 39 | +[ensuring data privacy]: https://squidfunk.github.io/mkdocs-material/setup/ensuring-data-privacy |
| 40 | +[privacy plugin]: https://squidfunk.github.io/mkdocs-material/plugins/privacy/ |
| 41 | + |
| 42 | +## How it works |
| 43 | + |
| 44 | +In `mkdocs.yml`, turn off the auto-loading of fonts from Google Fonts |
| 45 | +and add an extra CSS stylesheet to configure your custom fonts. |
| 46 | + |
| 47 | +```yaml |
| 48 | +theme: |
| 49 | + font: false |
| 50 | + |
| 51 | +extra_css: |
| 52 | + - assets/stylesheets/extra.css |
| 53 | +``` |
| 54 | +
|
| 55 | +Then, [configure custom fonts] hosted either on a CDN or on your own |
| 56 | +server, depending on your needs. We demonstrate this here with the |
| 57 | +Noto Sans and Noto Mono font families. |
| 58 | +
|
| 59 | +[configure custom fonts]: https://squidfunk.github.io/mkdocs-material/setup/changing-the-fonts/#additional-fonts |
| 60 | +
|
| 61 | +### Using a CDN's CSS |
| 62 | +
|
| 63 | +If you are using a CDN to load your fonts, you may want to use CSS |
| 64 | +that the operators of the CDN are providing for defining the font |
| 65 | +faces, instead of writing your own. You still need to configure the |
| 66 | +fonts for Material to use, though: |
| 67 | +
|
| 68 | +=== "`mkdocs.yml`" |
| 69 | + |
| 70 | + ```yaml |
| 71 | + extra_css: |
| 72 | + - https://fonts.cdnfonts.com/css/noto-serif |
| 73 | + - https://fonts.cdnfonts.com/css/noto-mono |
| 74 | + - assets/stylesheets/extra.css |
| 75 | + ``` |
| 76 | + |
| 77 | +=== "`extra.css`" |
| 78 | + |
| 79 | + ```css |
| 80 | + :root { |
| 81 | + --md-text-font: "Noto Serif"; |
| 82 | + --md-code-font: "Noto Mono"; |
| 83 | + } |
| 84 | + ``` |
| 85 | + |
| 86 | +### Hosting on your own site |
| 87 | + |
| 88 | +The code for hosting the fonts yourself as part of your site, looks |
| 89 | +very similar to the previous example that used a CDN. You need to |
| 90 | +write your own font face definitions and point the browser |
| 91 | +to the files located on your own server, e.g., in |
| 92 | +`docs/assets/fonts`: |
| 93 | + |
| 94 | +=== "`mkdocs.yml`" |
| 95 | + |
| 96 | + ```yaml |
| 97 | + extra_css: |
| 98 | + - assets/stylesheets/extra.css |
| 99 | + ``` |
| 100 | + |
| 101 | +=== "`extra.css`" |
| 102 | + |
| 103 | + ```css |
| 104 | + @font-face { |
| 105 | + font-family: "Noto Serif"; |
| 106 | + font-weight: normal; |
| 107 | + font-style: normal; |
| 108 | + src: url("../fonts/NotoSerif-Regular.otf"); |
| 109 | + } |
| 110 | +
|
| 111 | + @font-face { |
| 112 | + font-family: "Noto Serif"; |
| 113 | + font-weight: bold; |
| 114 | + font-style: normal; |
| 115 | + src: url("../fonts/NotoSerif-Bold.otf"); |
| 116 | +
|
| 117 | + } |
| 118 | +
|
| 119 | + @font-face { |
| 120 | + font-family: "Noto Serif"; |
| 121 | + font-weight: normal; |
| 122 | + font-style: italic; |
| 123 | + src: url("../fonts/NotoSerif-Italic.otf"); |
| 124 | +
|
| 125 | + } |
| 126 | +
|
| 127 | + @font-face { |
| 128 | + font-family: "Noto Mono"; |
| 129 | + font-weight: normal; |
| 130 | + font-style: normal; |
| 131 | + src: url("../fonts/NotoSansMono-Regular.otf"); |
| 132 | +
|
| 133 | + } |
| 134 | +
|
| 135 | + @font-face { |
| 136 | + font-family: "Noto Mono"; |
| 137 | + font-weight: bold; |
| 138 | + font-style: normal; |
| 139 | + src: url("../fonts/NotoSansMono-Bold.otf"); |
| 140 | + } |
| 141 | +
|
| 142 | + :root { |
| 143 | + --md-text-font: "Noto Serif"; |
| 144 | + --md-code-font: "Noto Mono"; |
| 145 | + } |
| 146 | + ``` |
| 147 | + |
| 148 | + |
| 149 | +## (Dis-)Advantages |
| 150 | + |
| 151 | +If you are loading the fonts from a CDN as in this example, you are still not |
| 152 | +limiting traffic to only your website. To do that you would need to host the |
| 153 | +fonts as part of your site as described above. We include the CDN example here |
| 154 | +because you may have other reasons to use a CDN other than Google Fonts. |
| 155 | + |
| 156 | +Try not to use too many different fonts as that will slow down your page load |
| 157 | +and rendering times. |
| 158 | + |
| 159 | +Compared to using built-in browser fonts only, you have more control over the |
| 160 | +look of your site. |
| 161 | + |
| 162 | +## Alternatives |
| 163 | + |
| 164 | +Alternatively, you can simply [use built-in browser fonts] but that |
| 165 | +does mean giving up some control over the look of your site. |
| 166 | + |
| 167 | +[use built-in browser fonts]: ../fonts-builtin |
0 commit comments