mkdocs-material/docs/setup/setting-up-navigation.md

403 lines
12 KiB
Markdown
Raw Normal View History

---
template: overrides/main.html
---
2020-07-20 16:18:09 +03:00
# Setting up navigation
A clear and concise navigation structure is an important aspect of good project
2020-11-15 17:34:44 +03:00
documentation. Material for MkDocs provides a multitude of options to configure
2021-10-10 18:39:53 +03:00
the behavior of navigational elements, including [tabs][navigation.tabs] and
[sections][navigation.sections], and its flag-ship feature: [instant loading]
[navigation.instant].
2021-10-10 18:39:53 +03:00
[navigation.tabs]: #navigation-tabs
[navigation.sections]: #navigation-sections
[navigation.instant]: #instant-loading
## Configuration
### Instant loading
2021-10-10 18:39:53 +03:00
[:octicons-tag-24: 5.0.0][navigation.instant support] ·
:octicons-unlock-24: Feature flag
2021-10-10 18:39:53 +03:00
When instant loading is enabled, clicks on all internal links will be
intercepted and dispatched via [XHR] without fully reloading the page. Add
2021-01-31 21:23:28 +03:00
the following lines to `mkdocs.yml`:
``` yaml
theme:
features:
- navigation.instant
```
The resulting page is parsed and injected and all event handlers and components
2021-10-10 18:39:53 +03:00
are rebound automatically, i.e., __Material for MkDocs now behaves like a Single
Page Application__. Now, the search index survives navigation, which is
especially useful for large documentation sites.
2021-10-10 18:39:53 +03:00
[navigation.instant support]: https://github.com/squidfunk/mkdocs-material/releases/tag/5.2.0
[XHR]: https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest
2021-02-26 20:56:25 +03:00
### Anchor tracking
2021-11-28 18:58:52 +03:00
[:octicons-tag-24: 8.0.0][Anchor tracking support] ·
2021-02-26 20:56:25 +03:00
:octicons-unlock-24: Feature flag ·
2021-10-10 18:39:53 +03:00
:octicons-beaker-24: Experimental
2021-02-26 20:56:25 +03:00
2021-10-10 18:39:53 +03:00
When anchor tracking is enabled, the URL in the address bar is automatically
2021-02-26 20:56:25 +03:00
updated with the active anchor as highlighted in the table of contents. Add the
following lines to `mkdocs.yml`:
``` yaml
theme:
features:
- navigation.tracking
```
2021-11-28 18:58:52 +03:00
[Anchor tracking support]: https://github.com/squidfunk/mkdocs-material/releases/tag/8.0.0
2021-09-23 10:23:42 +03:00
2020-07-20 20:28:13 +03:00
### Navigation tabs
2021-10-10 18:39:53 +03:00
[:octicons-tag-24: 1.1.0][navigation.tabs support] ·
:octicons-unlock-24: Feature flag
2021-10-10 18:39:53 +03:00
When tabs are enabled, top-level sections are rendered in a menu layer below
2021-01-31 21:23:28 +03:00
the header for viewports above `1220px`, but remain as-is on mobile.[^1] Add
the following lines to `mkdocs.yml`:
[^1]:
2021-10-10 18:39:53 +03:00
Prior to :octicons-tag-24: 6.2.0, navigation tabs had a slightly different
behavior. All top-level pages (i.e. all top-level entries directly
refefring to a `*.md` file) defined inside the `nav` entry of `mkdocs.yml`
were grouped under the first tab which received the title of the first page.
This made it impossible to include a top-level page (or external link) as a
tab item, as was reported in #1884 and #2072. From :octicons-tag-24: 6.2.0
on, navigation tabs include all top-level pages and sections.
``` yaml
theme:
features:
- navigation.tabs
```
2021-10-10 13:19:14 +03:00
=== ":octicons-check-circle-fill-16: Enabled"
2021-10-10 18:39:53 +03:00
[![navigation.tabs enabled]][navigation.tabs enabled]
2021-10-10 13:19:14 +03:00
=== ":octicons-skip-16: Disabled"
2021-10-10 18:39:53 +03:00
[![navigation.tabs disabled]][navigation.tabs disabled]
2021-10-10 18:39:53 +03:00
[navigation.tabs support]: https://github.com/squidfunk/mkdocs-material/releases/tag/1.1.0
[navigation.tabs enabled]: ../assets/screenshots/navigation-tabs.png
[navigation.tabs disabled]: ../assets/screenshots/navigation.png
#### Sticky navigation tabs
2021-10-10 18:39:53 +03:00
[:octicons-tag-24: 7.3.0][navigation.tabs.sticky support] ·
:octicons-unlock-24: Feature flag ·
2021-09-23 10:23:42 +03:00
:octicons-beaker-24: Experimental
2021-10-10 18:39:53 +03:00
When sticky tabs are enabled, navigation tabs will lock below the header and
always remain visible when scrolling down. Just add the following two feature
flags to `mkdocs.yml`:
``` yaml
theme:
features:
- navigation.tabs
- navigation.tabs.sticky
```
2021-10-10 13:19:14 +03:00
=== ":octicons-check-circle-fill-16: Enabled"
2021-10-10 18:39:53 +03:00
[![navigation.tabs.sticky enabled]][navigation.tabs.sticky enabled]
2021-10-10 13:19:14 +03:00
=== ":octicons-skip-16: Disabled"
2021-10-10 18:39:53 +03:00
[![navigation.tabs.sticky disabled]][navigation.tabs.sticky disabled]
2021-10-10 18:39:53 +03:00
[navigation.tabs.sticky support]: https://github.com/squidfunk/mkdocs-material/releases/tag/7.3.0
[navigation.tabs.sticky enabled]: ../assets/screenshots/navigation-tabs-sticky.png
[navigation.tabs.sticky disabled]: ../assets/screenshots/navigation-tabs-collapsed.png
### Navigation sections
2021-10-10 18:39:53 +03:00
[:octicons-tag-24: 6.2.0][navigation.sections support] ·
2020-12-21 19:38:58 +03:00
:octicons-unlock-24: Feature flag
2021-10-10 18:39:53 +03:00
When sections are enabled, top-level sections are rendered as groups in the
2021-01-31 21:23:28 +03:00
sidebar for viewports above `1220px`, but remain as-is on mobile. Add the
following lines to `mkdocs.yml`:
``` yaml
theme:
features:
- navigation.sections
```
2021-10-10 13:19:14 +03:00
=== ":octicons-check-circle-fill-16: Enabled"
2021-10-10 18:39:53 +03:00
[![navigation.sections enabled]][navigation.sections enabled]
2021-10-10 13:19:14 +03:00
=== ":octicons-skip-16: Disabled"
2021-10-10 18:39:53 +03:00
[![navigation.sections disabled]][navigation.sections disabled]
2021-10-10 18:39:53 +03:00
[navigation.sections support]: https://github.com/squidfunk/mkdocs-material/releases/tag/6.2.0
[navigation.sections enabled]: ../assets/screenshots/navigation-sections.png
[navigation.sections disabled]: ../assets/screenshots/navigation.png
2021-10-10 18:39:53 +03:00
Both feature flags, [`navigation.tabs`][navigation.tabs] and
[`navigation.sections`][navigation.sections], can be combined with each other.
If both feature flags are enabled, sections are rendered for level 2 navigation
items.
### Navigation expansion
2021-10-10 18:39:53 +03:00
[:octicons-tag-24: 6.2.0][navigation.expand support] ·
2020-12-21 19:38:58 +03:00
:octicons-unlock-24: Feature flag
2021-10-10 18:39:53 +03:00
When expansion is enabled, the left sidebar will expand all collapsible
subsections by default, so the user doesn't have to open subsections manually.
2021-01-31 21:23:28 +03:00
Add the following lines to `mkdocs.yml`:
``` yaml
theme:
features:
- navigation.expand
```
2021-10-10 13:19:14 +03:00
=== ":octicons-check-circle-fill-16: Enabled"
2021-10-10 18:39:53 +03:00
[![navigation.expand enabled]][navigation.expand enabled]
2021-10-10 13:19:14 +03:00
=== ":octicons-skip-16: Disabled"
2021-10-10 18:39:53 +03:00
[![navigation.expand disabled]][navigation.expand disabled]
2021-10-10 18:39:53 +03:00
[navigation.expand support]: https://github.com/squidfunk/mkdocs-material/releases/tag/6.2.0
[navigation.expand enabled]: ../assets/screenshots/navigation-expand.png
[navigation.expand disabled]: ../assets/screenshots/navigation.png
2021-01-31 18:30:57 +03:00
### Section index pages
2021-10-10 18:39:53 +03:00
[:octicons-tag-24: 7.3.0][navigation.indexes support] ·
2021-01-31 18:30:57 +03:00
:octicons-unlock-24: Feature flag ·
2021-09-23 10:23:42 +03:00
:octicons-beaker-24: Experimental
2021-01-31 18:30:57 +03:00
2021-10-10 18:39:53 +03:00
When section index pages are enabled, documents can be directly attached to
2021-01-31 21:23:28 +03:00
sections, which is particularly useful for providing overview pages. Add the
following lines to `mkdocs.yml`:
2021-01-31 18:30:57 +03:00
``` yaml
theme:
features:
- navigation.indexes
```
2021-10-10 13:19:14 +03:00
=== ":octicons-check-circle-fill-16: Enabled"
2021-01-31 18:30:57 +03:00
2021-10-10 18:39:53 +03:00
[![navigation.indexes enabled]][navigation.indexes enabled]
2021-01-31 18:30:57 +03:00
2021-10-10 13:19:14 +03:00
=== ":octicons-skip-16: Disabled"
2021-01-31 18:30:57 +03:00
2021-10-10 18:39:53 +03:00
[![navigation.indexes disabled]][navigation.indexes disabled]
2021-01-31 18:30:57 +03:00
In order to link a page to a section, create a new document with the name
`index.md` in the respective folder, and add it to the beginning of your
navigation section:
2021-01-31 18:30:57 +03:00
``` yaml
nav:
- Section:
- section/index.md
- Page 1: section/page-1.md
...
- Page n: section/page-n.md
```
2021-10-10 18:39:53 +03:00
This feature flag is not compatible with [`toc.integrate`][toc.integrate].
2021-01-31 18:30:57 +03:00
2021-10-10 18:39:53 +03:00
[navigation.indexes support]: https://github.com/squidfunk/mkdocs-material/releases/tag/7.3.0
[navigation.indexes enabled]: ../assets/screenshots/navigation-index-on.png
[navigation.indexes disabled]: ../assets/screenshots/navigation-index-off.png
[toc.integrate]: #integrated-table-of-contents
2021-01-31 18:30:57 +03:00
2021-10-10 18:39:53 +03:00
### Integrated table of contents
2021-10-10 18:39:53 +03:00
[:octicons-tag-24: 6.2.0][toc.integrate support] ·
2021-03-29 19:59:53 +03:00
:octicons-unlock-24: Feature flag
2021-10-11 14:38:03 +03:00
When navigation integration for the [table of contents] is enabled, it is always
2021-10-10 18:39:53 +03:00
rendered as part of the navigation sidebar on the left. Add the following lines
to `mkdocs.yml`:
``` yaml
theme:
features:
2021-10-10 18:39:53 +03:00
- toc.integrate
```
2021-10-10 18:39:53 +03:00
=== ":octicons-check-circle-fill-16: Enabled"
2021-10-10 18:39:53 +03:00
[![toc.integrate enabled]][toc.integrate enabled]
2021-10-10 18:39:53 +03:00
=== ":octicons-skip-16: Disabled"
2021-10-10 18:39:53 +03:00
[![toc.integrate disabled]][toc.integrate disabled]
2021-10-10 18:39:53 +03:00
This feature flag is not compatible with [`navigation.indexes`]
[navigation.indexes].
2021-10-11 14:38:03 +03:00
[table of contents]: extensions/python-markdown.md#table-of-contents
2021-10-10 18:39:53 +03:00
[toc.integrate support]: https://github.com/squidfunk/mkdocs-material/releases/tag/7.3.0
[toc.integrate enabled]: ../assets/screenshots/toc-integrate.png
[toc.integrate disabled]: ../assets/screenshots/navigation-tabs.png
[navigation.indexes]: #section-index-pages
2021-10-10 18:39:53 +03:00
### Back-to-top button
2020-11-15 17:34:44 +03:00
2021-10-10 18:39:53 +03:00
[:octicons-tag-24: 7.1.0][navigation.top support] ·
2020-12-21 19:38:58 +03:00
:octicons-unlock-24: Feature flag
2020-11-15 17:34:44 +03:00
2021-10-10 18:39:53 +03:00
A back-to-top button can be shown when the user, after scrolling down, starts
to scroll up again. It's rendered centered and just below the header. Add the
following lines to `mkdocs.yml`:
2020-11-15 17:34:44 +03:00
``` yaml
theme:
features:
2021-10-10 18:39:53 +03:00
- navigation.top
2020-11-15 17:34:44 +03:00
```
2021-10-10 18:39:53 +03:00
[navigation.top support]: https://github.com/squidfunk/mkdocs-material/releases/tag/7.1.0
2020-11-15 17:34:44 +03:00
2021-05-01 18:53:39 +03:00
## Usage
### Hiding the sidebars
2021-10-10 18:39:53 +03:00
When [Metadata] is enabled, the navigation and/or table of contents sidebars
can be hidden for a document with custom front matter. Add the following lines
at the top of a Markdown file:
2021-05-30 16:59:13 +03:00
``` bash
---
hide:
2021-05-30 16:59:13 +03:00
- navigation
- toc
---
2021-05-30 16:59:13 +03:00
# Document title
...
```
=== "Hide navigation"
2021-10-10 18:39:53 +03:00
[![hide.navigation enabled]][hide.navigation enabled]
=== "Hide table of contents"
2021-10-10 18:39:53 +03:00
[![hide.toc enabled]][hide.toc enabled]
=== "Hide both"
2021-10-10 18:39:53 +03:00
[![hide.* enabled]][hide.* enabled]
2021-10-10 18:39:53 +03:00
[Metadata]: extensions/python-markdown.md#metadata
[hide.navigation enabled]: ../assets/screenshots/hide-navigation.png
[hide.toc enabled]: ../assets/screenshots/hide-toc.png
[hide.* enabled]: ../assets/screenshots/hide-navigation-toc.png
## Customization
### Keyboard shortcuts
Material for MkDocs includes several keyboard shortcuts that make it possible
to navigate your project documentation via keyboard. There're two modes:
2021-10-10 20:22:13 +03:00
`search`{ #mode-search }
: This mode is active when the _search is focused_. It provides several key
bindings to make search accessible and navigable via keyboard:
2020-07-27 13:05:07 +03:00
* ++arrow-down++ , ++arrow-up++ : select next / previous result
* ++esc++ , ++tab++ : close search dialog
* ++enter++ : follow selected result
2021-10-10 20:22:13 +03:00
`global`{ #mode-global }
2020-07-26 15:46:09 +03:00
: This mode is active when _search is not focussed_ and when there's no other
focussed element that is susceptible to keyboard input. The following keys
are bound:
2020-07-27 13:05:07 +03:00
* ++f++ , ++s++ , ++slash++ : open search dialog
* ++p++ , ++comma++ : go to previous page
* ++n++ , ++period++ : go to next page
Let's say you want to bind some action to the ++x++ key. By using [additional
2021-10-10 18:39:53 +03:00
JavaScript], you can subscribe to the `keyboard$` observable and attach
your custom event listener:
2021-10-10 18:39:53 +03:00
=== ":octicons-file-code-16: docs/javascripts/shortcuts.js"
``` js
keyboard$.subscribe(function(key) {
if (key.mode === "global" && key.type === "x") {
/* Add custom keyboard handler here */
key.claim() // (1)
}
})
```
2021-10-10 18:39:53 +03:00
1. The call to `key.claim()` will execute `preventDefault()` on the
underlying event, so the keypress will not propagate further and
touch other event listeners.
2021-10-10 18:39:53 +03:00
=== ":octicons-file-code-16: mkdocs.yml"
2020-11-15 17:34:44 +03:00
2021-10-10 18:39:53 +03:00
``` yaml
extra_javascript:
- javascripts/shortcuts.js
```
2020-11-15 17:34:44 +03:00
2021-10-10 18:39:53 +03:00
[additional JavaScript]: ../customization.md#additional-javascript
### Content area width
2020-11-15 17:34:44 +03:00
The width of the content area is set so the length of each line doesn't exceed
80-100 characters, depending on the width of the characters. While this
is a reasonable default, as longer lines tend to be harder to read, it may be
desirable to increase the overall width of the content area, or even make it
stretch to the entire available space.
2021-10-10 18:39:53 +03:00
This can easily be achieved with an [additional style sheet] and a few lines
2020-11-15 17:34:44 +03:00
of CSS:
2021-10-10 18:39:53 +03:00
=== ":octicons-file-code-16: docs/stylesheets/extra.css"
2020-11-15 17:34:44 +03:00
``` css
.md-grid {
2021-10-10 18:39:53 +03:00
max-width: 1440px; /* (1) */
2020-11-15 17:34:44 +03:00
}
```
2021-10-10 18:39:53 +03:00
1. If you want the content area to always stretch to the available screen
space, reset `max-width` with the following CSS:
2020-11-15 17:34:44 +03:00
2021-10-10 18:39:53 +03:00
``` css
.md-grid {
max-width: initial;
}
```
=== ":octicons-file-code-16: mkdocs.yml"
``` yaml
extra_css:
- stylesheets/extra.css
2020-11-15 17:34:44 +03:00
```
2021-10-10 18:39:53 +03:00
[additional style sheet]: ../customization.md#additional-css