Skip to content

Commit b2cb385

Browse files
committed
Merge pull request mkdocs#911 from waylan/theme-docs
Document theme_dir. Fixes mkdocs#863
2 parents 9a62248 + 4bb0c1c commit b2cb385

File tree

3 files changed

+133
-22
lines changed

3 files changed

+133
-22
lines changed

docs/user-guide/configuration.md

+5-2
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,11 @@ Lets you set a directory to a custom theme. This can either be a relative
139139
directory, in which case it is resolved relative to the directory containing
140140
your configuration file, or it can be an absolute directory path.
141141

142-
See [styling your docs](styling-your-docs.md#custom-themes) for an explanation
143-
of custom themes.
142+
See [styling your docs](styling-your-docs.md#using-the-theme_dir) for details if
143+
you would like to tweak an existing theme.
144+
145+
See [custom themes](./custom-themes) if you would like to build your own theme
146+
from the ground up.
144147

145148
**default**: `null`
146149

docs/user-guide/custom-themes.md

+13-5
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,19 @@ You would include the following setting to use the custom theme directory:
3939

4040
theme_dir: 'custom_theme'
4141

42-
If used in combination with the `theme` configuration value a custom theme can
43-
be used to replace only specific parts of a built-in theme. For example, with
44-
the above layout and if you set `theme: mkdocs` then the `base.html` file
45-
would replace that in the theme but otherwise it would remain the same. This
46-
is useful if you want to make small adjustments to an existing theme.
42+
!!! Note
43+
44+
Generally, when building your own theme, the `theme` configurations setting
45+
would be left blank. However, if used in combination with the `theme`
46+
configuration value a custom theme can be used to replace only specific
47+
parts of a built-in theme. For example, with the above layout and if you set
48+
`theme: mkdocs` then the `base.html` file would replace that in the theme
49+
but otherwise it would remain the same. This is useful if you want to make
50+
small adjustments to an existing theme.
51+
52+
For more specific information, see [styling your docs].
53+
54+
[styling your docs]: ./styling-your-docs.md#using-the-theme_dir
4755

4856
## Basic theme
4957

docs/user-guide/styling-your-docs.md

+115-15
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,22 @@ How to style and theme your documentation.
44

55
---
66

7-
MkDocs includes a number of different [built-in themes](#built-in-themes) and
8-
[external themes](#bootstrap-and-bootswatch-themes) which can easily be
9-
[customised with extra CSS or JavaScript](#customising-a-theme) or you can
10-
create a [custom theme](/user-guide/custom-themes.md) for your documentation.
7+
MkDocs includes a couple [built-in themes] as well as various [third party
8+
themes], all of which can easily be customized with [extra CSS or
9+
JavaScript][docs_dir] or overridden from the [theme directory][theme_dir]. You
10+
can also create your own [custom theme] from the grown up for your
11+
documentation.
1112

1213
To use a theme that is included in MkDocs, simply add this to your
1314
`mkdocs.yml` config file.
1415

1516
theme: readthedocs
1617

17-
Replace [`readthedocs`](#readthedocs) with any of the [built-in themes](#built-
18-
in-themes) listed below.
18+
Replace [`readthedocs`](#readthedocs) with any of the [built-in themes] listed below.
1919

20-
To create a new custom theme or more heavily customise an existing theme, see
21-
the [custom themes](#custom-themes) section below.
20+
To create a new custom theme see the [Custom Themes][custom theme] page, or to
21+
more heavily customize an existing theme, see
22+
the [Customizing a Theme][customize] section below.
2223

2324
## Built-in themes
2425

@@ -32,13 +33,21 @@ the [custom themes](#custom-themes) section below.
3233

3334
### Third Party Themes
3435

35-
Third party themes can be found in the MkDocs [community wiki](
36-
https://github.com/mkdocs/mkdocs/wiki/MkDocs-Themes).
36+
A list of third party themes can be found in the MkDocs [community wiki]. If you
37+
have created your own, please feel free to add it to the list.
3738

38-
## Customising a Theme
39+
## Customizing a Theme
40+
41+
If you would like to make a few tweaks to an existing theme, there is no need to
42+
create your own theme from scratch. For minor tweaks which only require some CSS
43+
and/or JavaScript, you can use the [docs_dir]. However, for more complex
44+
customizations, including overriding templates, you will need to use the
45+
[theme_dir].
46+
47+
### Using the docs_dir
3948

4049
The [extra_css] and [extra_javascript] configuration options can be used to
41-
make tweaks and customisations to existing themes. To use these, you simply
50+
make tweaks and customizations to existing themes. To use these, you simply
4251
need to include either CSS or JavaScript files within your [documentation
4352
directory].
4453

@@ -64,7 +73,98 @@ After making these changes, they should be visible when you run
6473
`mkdocs serve` - if you already had this running, you should see that the CSS
6574
changes were automatically picked up and the documentation will be updated.
6675

76+
!!! note
77+
78+
Any extra CSS or JavaScript files will be added to the generated HTML
79+
document after the page content. If you desire to include a JavaScript
80+
library, you may have better sucess including the library by using the
81+
[theme_dir].
82+
83+
### Using the theme_dir
84+
85+
The [theme_dir] configuration option can be used to point to a directory of
86+
files which override the files in the theme set on the [theme] configuration
87+
option. Any file in the `theme_dir` with the same name as a file in the `theme`
88+
will replace the file of the same name in the `theme`. Any additional files in
89+
the `theme_dir` will be added to the `theme`. The contents of the `theme_dir`
90+
should mirror the directory structure of the `theme`. You may include templates,
91+
JavaScript files, CSS files, images, fonts, or any other media included in a
92+
theme.
93+
94+
For example, the [mkdocs] theme ([browse source]), contains the following
95+
directory structure (in part):
96+
97+
```nohighlight
98+
- css\
99+
- fonts\
100+
- img\
101+
- favicon.ico
102+
- grid.png
103+
- js\
104+
- 404.html
105+
- base.html
106+
- content.html
107+
- nav-sub.html
108+
- nav.html
109+
- toc.html
110+
```
111+
112+
To override any of the files contained in that theme, create a new directory
113+
next to your `docs_dir`:
114+
115+
```bash
116+
mkdir custom_theme
117+
```
118+
119+
And then point your `mkdocs.yml` configuration file at the new directory:
120+
121+
```yaml
122+
theme_dir: custom_theme
123+
```
124+
125+
To override the 404 error page ("file not found"), add a new template file named
126+
`404.html` to the `custom_theme` directory. For information on what can be
127+
included in a template, review the documentation for building a [custom theme].
128+
129+
To override the favicon, you can add a new icon file at
130+
`custom_theme/img/favicon.ico`.
131+
132+
To include a JavaScript library, copy the library to the `custom_theme/js/`
133+
directory.
134+
135+
Your directory structure should now look like this:
136+
137+
```nohighlight
138+
- docs/
139+
- index.html
140+
- custom_theme/
141+
- img/
142+
- favicon.ico
143+
- js/
144+
- somelib.js
145+
- 404.html
146+
- config.yml
147+
```
148+
149+
!!! Note
150+
151+
Any files included in the `theme` but not included in the `theme_dir` will
152+
still be utilized. The `theme_dir` will only override/replace files in the
153+
`theme`. If you want to remove files, or build a theme from scratch, then
154+
you should review the documentation for building a [custom theme].
155+
156+
[built-in themes]: #built-in-themes
157+
[third party themes]: #third-party-themes
158+
[docs_dir]: #using-the-docs_dir
159+
[theme_dir]: #using-the-theme_dir
160+
[customize]: #customizing-a-theme
161+
[custom theme]: ./custom-themes.md
67162
[ReadTheDocs]: ./deploying-your-docs.md#readthedocs
68-
[documentation directory]: /user-guide/configuration/#docs_dir
69-
[extra_css]: /user-guide/configuration.md#extra_css
70-
[extra_javascript]: /user-guide/configuration.md#extra_javascript
163+
[community wiki]: https://github.com/mkdocs/mkdocs/wiki/MkDocs-Themes
164+
[documentation directory]: ./configuration/#docs_dir
165+
[extra_css]: ./configuration.md#extra_css
166+
[extra_javascript]: ./configuration.md#extra_javascript
167+
[theme_dir]: ./configuration/#theme_dir
168+
[theme]: ./configuration/#theme
169+
[mkdocs]: #mkdocs
170+
[browse source]: https://github.com/mkdocs/mkdocs/tree/master/mkdocs/themes/mkdocs

0 commit comments

Comments
 (0)