Debian Codegraph @

docs/user-guide/customizing-your-theme.md

raw
1 # Customizing Your Theme
2
3 Altering a theme to suit your needs.
4
5 ---
6
7 If you would like to make a few tweaks to an existing theme, there is no need
8 to create your own theme from scratch. For minor tweaks which only require
9 some CSS and/or JavaScript, you can [use the docs_dir](#using-the-docs_dir).
10 However, for more complex customizations, including overriding templates, you
11 will need to [use the theme custom_dir](#using-the-theme-custom_dir) setting.
12
13 ## Using the docs_dir
14
15 The [extra_css] and [extra_javascript] configuration options can be used to
16 make tweaks and customizations to existing themes. To use these, you simply
17 need to include either CSS or JavaScript files within your [documentation
18 directory].
19
20 For example, to change the color of the headers in your documentation, create
21 a file called (for example) `style.css` and place it next to the documentation Markdown. In
22 that file add the following CSS.
23
24 ```css
25 h1 {
26 color: red;
27 }
28 ```
29
30 Then you need to add it to `mkdocs.yml`:
31
32 ```yaml
33 extra_css:
34 - style.css
35 ```
36
37 After making these changes, they should be visible when you run
38 `mkdocs serve` - if you already had this running, you should see that the CSS
39 changes were automatically picked up and the documentation will be updated.
40
41 NOTE:
42 Any extra CSS or JavaScript files will be added to the generated HTML
43 document after the page content. If you desire to include a JavaScript
44 library, you may have better success including the library by using the
45 theme [custom_dir].
46
47 ## Using the theme custom_dir
48
49 The [`theme.custom_dir`][custom_dir] configuration option can be used to point
50 to a directory of files which override the files in a parent theme. The parent
51 theme would be the theme defined in the [`theme.name`][name] configuration
52 option. Any file in the `custom_dir` with the same name as a file in the
53 parent theme will replace the file of the same name in the parent theme. Any
54 additional files in the `custom_dir` will be added to the parent theme. The
55 contents of the `custom_dir` should mirror the directory structure of the
56 parent theme. You may include templates, JavaScript files, CSS files, images,
57 fonts, or any other media included in a theme.
58
59 NOTE:
60 For this to work, the `theme.name` setting must be set to a known
61 installed theme. If the `name` setting is instead set to `null` (or not
62 defined), then there is no theme to override and the contents of the
63 `custom_dir` must be a complete, standalone theme. See the [Theme
64 Developer Guide][custom theme] for more information.
65
66 For example, the [mkdocs] theme ([browse source]), contains the following
67 directory structure (in part):
68
69 ```text
70 - css\
71 - fonts\
72 - img\
73 - favicon.ico
74 - grid.png
75 - js\
76 - 404.html
77 - base.html
78 - content.html
79 - nav-sub.html
80 - nav.html
81 - toc.html
82 ```
83
84 To override any of the files contained in that theme, create a new directory
85 next to your `docs_dir`:
86
87 ```bash
88 mkdir custom_theme
89 ```
90
91 And then point your `mkdocs.yml` configuration file at the new directory:
92
93 ```yaml
94 theme:
95 name: mkdocs
96 custom_dir: custom_theme/
97 ```
98
99 To override the 404 error page ("file not found"), add a new template file named
100 `404.html` to the `custom_theme` directory. For information on what can be
101 included in a template, review the [Theme Developer Guide][custom theme].
102
103 To override the favicon, you can add a new icon file at
104 `custom_theme/img/favicon.ico`.
105
106 To include a JavaScript library, copy the library to the `custom_theme/js/`
107 directory.
108
109 Your directory structure should now look like this:
110
111 ```text
112 - docs/
113 - index.html
114 - custom_theme/
115 - img/
116 - favicon.ico
117 - js/
118 - somelib.js
119 - 404.html
120 - config.yml
121 ```
122
123 NOTE:
124 Any files included in the parent theme (defined in `name`) but not
125 included in the `custom_dir` will still be utilized. The `custom_dir` will
126 only override/replace files in the parent theme. If you want to remove
127 files, or build a theme from scratch, then you should review the [Theme
128 Developer Guide][custom theme].
129
130 ### Overriding Template Blocks
131
132 The built-in themes implement many of their parts inside template blocks which
133 can be individually overridden in the `main.html` template. Simply create a
134 `main.html` template file in your `custom_dir` and define replacement blocks
135 within that file. Just make sure that the `main.html` extends `base.html`. For
136 example, to alter the title of the MkDocs theme, your replacement `main.html`
137 template would contain the following:
138
139 ```django
140 {% extends "base.html" %}
141
142 {% block htmltitle %}
143 <title>Custom title goes here</title>
144 {% endblock %}
145 ```
146
147 In the above example, the `htmltitle` block defined in your custom `main.html` file
148 will be used in place of the default `htmltitle` block defined in the parent theme.
149 You may re-define as many blocks as you desire, as long as those blocks are
150 defined in the parent. For example, you could replace the Google Analytics
151 script with one for a different service or replace the search feature with your
152 own. You will need to consult the parent theme you are using to determine what
153 blocks are available to override. The MkDocs and ReadTheDocs themes provide the
154 following blocks:
155
156 * `site_meta`: Contains meta tags in the document head.
157 * `htmltitle`: Contains the page title in the document head.
158 * `styles`: Contains the link tags for stylesheets.
159 * `libs`: Contains the JavaScript libraries (jQuery, etc) included in the page header.
160 * `scripts`: Contains JavaScript scripts which should execute after a page loads.
161 * `analytics`: Contains the analytics script.
162 * `extrahead`: An empty block in the `<head>` to insert custom tags/scripts/etc.
163 * `site_name`: Contains the site name in the navigation bar.
164 * `site_nav`: Contains the site navigation in the navigation bar.
165 * `search_button`: Contains the search box in the navigation bar.
166 * `next_prev`: Contains the next and previous buttons in the navigation bar.
167 * `repo`: Contains the repository link in the navigation bar.
168 * `content`: Contains the page content and table of contents for the page.
169 * `footer`: Contains the page footer.
170
171 You may need to view the source template files to ensure your modifications will
172 work with the structure of the site. See [Template Variables] for a list of
173 variables you can use within your custom blocks. For a more complete
174 explanation of blocks, consult the [Jinja documentation].
175
176 ### Combining the custom_dir and Template Blocks
177
178 Adding a JavaScript library to the `custom_dir` will make it available, but
179 won't include it in the pages generated by MkDocs. Therefore, a link needs to
180 be added to the library from the HTML.
181
182 Starting the with directory structure above (truncated):
183
184 ```text
185 - docs/
186 - custom_theme/
187 - js/
188 - somelib.js
189 - config.yml
190 ```
191
192 A link to the `custom_theme/js/somelib.js` file needs to be added to the
193 template. As `somelib.js` is a JavaScript library, it would logically go in the
194 `libs` block. However, a new `libs` block that only includes the new script will
195 replace the block defined in the parent template and any links to libraries in
196 the parent template will be removed. To avoid breaking the template, a
197 [super block] can be used with a call to `super` from within the block:
198
199 ```django
200 {% extends "base.html" %}
201
202 {% block libs %}
203 {{ super() }}
204 <script src="{{ base_url }}/js/somelib.js"></script>
205 {% endblock %}
206 ```
207
208 Note that the [base_url] template variable was used to ensure that the link is
209 always relative to the current page.
210
211 Now the generated pages will include links to the template provided libraries as
212 well as the library included in the `custom_dir`. The same would be required for
213 any additional CSS files included in the `custom_dir`.
214
215 [custom theme]: ../dev-guide/themes.md
216 [extra_css]: ./configuration.md#extra_css
217 [extra_javascript]: ./configuration.md#extra_javascript
218 [documentation directory]: ./configuration.md#docs_dir
219 [custom_dir]: ./configuration.md#custom_dir
220 [name]: ./configuration.md#name
221 [mkdocs]: ./choosing-your-theme.md#mkdocs
222 [browse source]: https://github.com/mkdocs/mkdocs/tree/master/mkdocs/themes/mkdocs
223 [Template Variables]: ../dev-guide/themes.md#template-variables
224 [Jinja documentation]: https://jinja.palletsprojects.com/en/latest/templates/#template-inheritance
225 [super block]: https://jinja.palletsprojects.com/en/latest/templates/#super-blocks
226 [base_url]: ../dev-guide/themes.md#base_url