mkdocs-material/docs/setup/changing-the-fonts.md

122 lines
3.2 KiB
Markdown
Raw Normal View History

# Changing the fonts
2020-07-17 00:55:37 +03:00
Material for MkDocs makes it easy to change the typeface of your project
2021-10-10 18:39:53 +03:00
documentation, as it directly integrates with [Google Fonts]. Alternatively,
2020-07-20 20:28:13 +03:00
fonts can be custom-loaded if self-hosting is preferred for data privacy reasons
or another destination should be used.
2020-07-17 00:55:37 +03:00
2021-10-10 18:39:53 +03:00
[Google Fonts]: https://fonts.google.com
2020-07-17 00:55:37 +03:00
## Configuration
2020-07-17 00:55:37 +03:00
### Regular font
2022-09-11 20:25:40 +03:00
[:octicons-tag-24: 0.1.2][Font support] ·
2021-10-10 18:39:53 +03:00
:octicons-milestone-24: Default: [`Roboto`][Roboto]
2021-10-10 18:39:53 +03:00
The regular font is used for all body copy, headlines, and essentially
everything that does not need to be monospaced. It can be set to any
2021-10-10 18:39:53 +03:00
valid [Google Font][Google Fonts] via `mkdocs.yml`:
``` yaml
theme:
font:
2020-07-17 00:55:37 +03:00
text: Roboto
```
The typeface will be loaded in 300, 400, _400i_ and __700__.
2020-07-17 00:55:37 +03:00
2021-10-10 18:39:53 +03:00
[Roboto]: https://fonts.google.com/specimen/Roboto
2022-09-11 20:25:40 +03:00
[Font support]: https://github.com/squidfunk/mkdocs-material/releases/tag/0.1.2
2020-07-17 00:55:37 +03:00
### Monospaced font
2020-07-17 00:55:37 +03:00
2022-09-11 20:25:40 +03:00
[:octicons-tag-24: 0.1.2][Font support] ·
2021-10-10 18:39:53 +03:00
:octicons-milestone-24: Default: [`Roboto Mono`][Roboto Mono]
2020-07-17 00:55:37 +03:00
The _monospaced font_ is used for code blocks and can be configured separately.
2021-10-10 18:39:53 +03:00
Just like the regular font, it can be set to any valid [Google Font]
[Google Fonts] via `mkdocs.yml`:
``` yaml
theme:
2020-07-17 00:55:37 +03:00
font:
code: Roboto Mono
```
2020-07-17 00:55:37 +03:00
The typeface will be loaded in 400.
2021-10-10 18:39:53 +03:00
[Roboto Mono]: https://fonts.google.com/specimen/Roboto+Mono
2021-10-10 18:39:53 +03:00
### Autoloading
2020-07-21 17:01:22 +03:00
2022-09-11 20:25:40 +03:00
[:octicons-tag-24: 1.0.0][Autoloading support] ·
2021-10-10 18:39:53 +03:00
:octicons-milestone-24: Default: _none_
2020-07-21 17:01:22 +03:00
2021-10-10 18:39:53 +03:00
If you want to prevent typefaces from being loaded from [Google Fonts], e.g.
to adhere to [data privacy] regulations, and fall back to system fonts, add the
following lines to `mkdocs.yml`:
2020-07-17 00:55:37 +03:00
``` yaml
theme:
font: false
```
2022-02-20 21:49:23 +03:00
!!! tip "Automatically bundle Google Fonts"
2023-01-01 19:44:37 +03:00
The [built-in privacy plugin] makes it easy to use Google Fonts
2022-02-20 21:49:23 +03:00
while complying with the __General Data Protection Regulation__ (GDPR),
by automatically downloading and self-hosting the web font files.
2021-10-10 23:32:32 +03:00
[data privacy]: https://developers.google.com/fonts/faq#what_does_using_the_google_fonts_api_mean_for_the_privacy_of_my_users
2022-09-11 20:25:40 +03:00
[Autoloading support]: https://github.com/squidfunk/mkdocs-material/releases/tag/1.0.0
2022-02-27 15:19:44 +03:00
[built-in privacy plugin]: ensuring-data-privacy.md#built-in-privacy-plugin
2020-07-21 17:01:22 +03:00
2021-10-10 18:39:53 +03:00
## Customization
### Additional fonts
2020-07-21 17:01:22 +03:00
2021-10-10 18:39:53 +03:00
If you want to load an (additional) font from another destination or override
the system font, you can use an [additional style sheet] to add the
2020-07-21 17:01:22 +03:00
corresponding `@font-face` definition:
2020-07-17 00:55:37 +03:00
2022-09-11 20:25:40 +03:00
=== ":octicons-file-code-16: `docs/stylesheets/extra.css`"
2021-10-10 18:39:53 +03:00
``` css
@font-face {
font-family: "<font>";
src: "...";
}
```
2022-09-11 20:25:40 +03:00
=== ":octicons-file-code-16: `mkdocs.yml`"
2021-10-10 18:39:53 +03:00
``` yaml
extra_css:
- stylesheets/extra.css
```
2020-07-17 00:55:37 +03:00
2020-07-21 19:39:27 +03:00
The font can then be applied to specific elements, e.g. only headlines, or
2021-10-10 18:39:53 +03:00
globally to be used as the site-wide regular or monospaced font:
2020-07-17 00:55:37 +03:00
2020-07-21 19:39:27 +03:00
=== "Regular font"
2020-07-17 00:55:37 +03:00
``` css
2021-03-21 19:14:32 +03:00
:root {
2021-12-30 12:54:36 +03:00
--md-text-font: "<font>"; /* (1)! */
2020-07-17 00:55:37 +03:00
}
```
2021-10-10 18:39:53 +03:00
1. Always define fonts through CSS variables and not `font-family`, as
this would disable the system font fallback.
=== "Monospaced font"
2020-07-17 00:55:37 +03:00
``` css
2021-03-21 19:14:32 +03:00
:root {
2021-12-30 12:54:36 +03:00
--md-code-font: "<font>";
2020-07-17 00:55:37 +03:00
}
```
2021-10-10 18:39:53 +03:00
[additional style sheet]: ../customization.md#additional-css