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

649 lines
18 KiB
Markdown
Raw Permalink Normal View History

2020-07-20 16:18:09 +03:00
# Setting up navigation
2023-09-14 20:09:18 +03:00
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
2022-09-11 20:25:40 +03:00
the behavior of navigational elements, including [tabs] and [sections], and one
2023-01-29 14:10:15 +03:00
of its flagship features: [instant loading].
2022-09-11 20:25:40 +03:00
[tabs]: #navigation-tabs
[sections]: #navigation-sections
[instant loading]: #instant-loading
Additional navigation can be configured [in the footer] as well as with the
[tags plugin]. The [blog plugin] also sets up additional navigation.
[in the footer]: setting-up-the-footer.md#navigation
[tags plugin]: ../plugins/tags.md
[blog plugin]: ../plugins/blog.md
## Configuration
### Instant loading
2023-09-14 20:09:18 +03:00
<!-- md:version 5.0.0 -->
<!-- md:feature -->
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.
2024-02-19 08:48:18 +03:00
!!! info "The [`site_url`][mkdocs.site_url] setting must be set"
Note that you must set [`site_url`][mkdocs.site_url] when using instant
navigation, as instant navigation relies on the generated `sitemap.xml`
which will be empty if this setting is omitted. Example:
``` yaml
site_url: https://example.com
```
2021-10-10 18:39:53 +03:00
[XHR]: https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest
2024-01-24 11:45:24 +03:00
#### Instant prefetching
2023-06-15 19:02:31 +03:00
2023-09-14 20:09:18 +03:00
<!-- md:sponsors -->
<!-- md:version insiders-4.36.0 -->
<!-- md:feature -->
<!-- md:flag experimental -->
2023-06-15 19:02:31 +03:00
Instant prefetching is a new experimental feature that will start to fetch a
page once the user hovers over a link. This will reduce the perceived loading
time for the user, especially on slow connections, as the page will be available
immediately upon navigation. Enable it with:
``` yaml
theme:
features:
- navigation.instant
- navigation.instant.prefetch
```
2023-10-02 12:37:20 +03:00
#### Progress indicator
<!-- md:version 9.4.3 -->
<!-- md:feature -->
<!-- md:flag experimental -->
In order to provide a better user experience on slow connections when using
instant navigation, a progress indicator can be enabled. It will be shown at
the top of the page and will be hidden once the page has fully loaded. You can
enable it in `mkdocs.yml` with:
``` yaml
theme:
features:
- navigation.instant
- navigation.instant.progress
```
The progress indicator will only show if the page hasn't finished loading after
400ms, so that fast connections will never show it for a better instant
experience.
2024-01-28 12:26:15 +03:00
### Instant previews :material-alert-decagram:{ .mdx-pulse title="Added on January 28, 2024" }
<!-- md:sponsors -->
<!-- md:version insiders-4.52.0 -->
<!-- md:feature -->
<!-- md:flag experimental -->
Instant previews are a brand new feature that allow the user to preview another
site of your documentation without navigating to it. They can be very helpful to
2024-03-09 10:22:56 +03:00
keep the user in context. Instant previews can be enabled on any header link
2024-02-24 08:46:13 +03:00
with the `data-preview` attribute:
2024-01-28 12:26:15 +03:00
```` markdown title="Link with instant preview"
``` markdown
[Attribute Lists](#){ data-preview }
```
````
<div class="result" markdown>
[Attribute Lists](extensions/python-markdown.md#attribute-lists){ data-preview }
</div>
2024-03-09 10:22:56 +03:00
!!! info "Limitations"
Instant previews are still an experimental feature and currently limited to
headerlinks. This means, you can use them on any internal link that points
to a header on another page, but not other elements with `id` attributes.
After we have gathered enough feedback, we will consider extending this
feature to other, and possibly arbitrary elements.
2024-02-24 08:46:13 +03:00
#### Automatic previews
<!-- md:sponsors -->
<!-- md:version insiders-4.53.0 -->
<!-- md:extension -->
<!-- md:flag experimental -->
The recommended way to work with instant previews is to use the Markdown
extension that is included with Material for MkDocs, as it allows you to enable
instant previews on a per-page or per-section level for your documentation:
``` yaml
markdown_extensions:
- material.extensions.preview:
targets:
include:
- changelog/index.md
- customization.md
- insiders/changelog/*
- setup/extensions/*
```
The above configuration is what we use for our documentation. We've enabled
instant previews for our changelogs, customization guide, and Insiders sections,
as well as for all Markdown extensions that we support.
!!! info "Full configuration example"
``` yaml
markdown_extensions:
- material.extensions.preview:
sources: # (1)!
include:
- ...
exclude:
- ...
targets: # (2)!
include:
- ...
exclude:
- ...
```
1. Sources specify the pages _on_ which instant previews should be enabled.
If this setting is omitted, instant previews will be enabled on all
pages. You can use patterns to include or exclude pages. Exclusion is
evaluated on top of inclusion, so if a page is matched by both, it will
be excluded.
2. Targets specify the pages _to_ which instant previews should be enabled.
This is the recommended way to enable instant previews.
---
2024-01-28 12:26:15 +03:00
Instant previews can also be enabled globally by adding the following lines to
2024-03-09 10:22:56 +03:00
`mkdocs.yml`, which will enable instant previews for all header links,
2024-01-28 12:26:15 +03:00
alleviating the need to add data attributes:
``` yaml
theme:
features:
- navigation.instant.preview
```
2024-02-19 08:48:18 +03:00
!!! info "The [`site_url`][mkdocs.site_url] setting must be set"
Note that you must set [`site_url`][mkdocs.site_url] when using instant
previews, as instant previews rely on the generated `sitemap.xml`
which will be empty if this setting is omitted. Example:
``` yaml
site_url: https://example.com
```
2021-02-26 20:56:25 +03:00
### Anchor tracking
2023-09-14 20:09:18 +03:00
<!-- md:version 8.0.0 -->
<!-- md:feature -->
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
```
2020-07-20 20:28:13 +03:00
### Navigation tabs
2023-09-14 20:09:18 +03:00
<!-- md:version 1.1.0 -->
<!-- md:feature -->
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]:
2023-09-14 20:09:18 +03:00
Prior to <!-- md:version 6.2.0 -->, navigation tabs had a slightly different
2021-10-10 18:39:53 +03:00
behavior. All top-level pages (i.e. all top-level entries directly
2023-01-29 14:10:15 +03:00
referring to a `*.md` file) defined inside the `nav` entry of `mkdocs.yml`
2021-10-10 18:39:53 +03:00
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
2023-09-14 20:09:18 +03:00
tab item, as was reported in #1884 and #2072. From <!-- md:version 6.2.0 -->
2021-10-10 18:39:53 +03:00
on, navigation tabs include all top-level pages and sections.
``` yaml
theme:
features:
- navigation.tabs
```
2022-09-11 20:25:40 +03:00
=== "With tabs"
2022-09-11 20:25:40 +03:00
[![Navigation tabs enabled]][Navigation tabs enabled]
2022-09-11 20:25:40 +03:00
=== "Without"
2022-09-11 20:25:40 +03:00
[![Navigation tabs disabled]][Navigation tabs disabled]
2022-09-11 20:25:40 +03:00
[Navigation tabs enabled]: ../assets/screenshots/navigation-tabs.png
[Navigation tabs disabled]: ../assets/screenshots/navigation.png
#### Sticky navigation tabs
2023-09-14 20:09:18 +03:00
<!-- md:version 7.3.0 -->
<!-- md:feature -->
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
```
2022-09-11 20:25:40 +03:00
=== "With sticky tabs"
2022-09-11 20:25:40 +03:00
[![Sticky navigation tabs enabled]][Sticky navigation tabs enabled]
2022-09-11 20:25:40 +03:00
=== "Without"
2022-09-11 20:25:40 +03:00
[![Sticky navigation tabs disabled]][Sticky navigation tabs disabled]
2022-09-11 20:25:40 +03:00
[Sticky navigation tabs enabled]: ../assets/screenshots/navigation-tabs-sticky.png
[Sticky navigation tabs disabled]: ../assets/screenshots/navigation-tabs-collapsed.png
### Navigation sections
2023-09-14 20:09:18 +03:00
<!-- md:version 6.2.0 -->
<!-- md:feature -->
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
```
2022-09-11 20:25:40 +03:00
=== "With sections"
2022-09-11 20:25:40 +03:00
[![Navigation sections enabled]][Navigation sections enabled]
2022-09-11 20:25:40 +03:00
=== "Without"
2022-09-11 20:25:40 +03:00
[![Navigation sections disabled]][Navigation sections disabled]
2022-09-11 20:25:40 +03:00
[Navigation sections enabled]: ../assets/screenshots/navigation-sections.png
[Navigation sections disabled]: ../assets/screenshots/navigation.png
2022-09-11 20:25:40 +03:00
Both feature flags, [`navigation.tabs`][tabs] and
[`navigation.sections`][sections], can be combined with each other. If both
feature flags are enabled, sections are rendered for level 2 navigation items.
### Navigation expansion
2023-09-14 20:09:18 +03:00
<!-- md:version 6.2.0 -->
<!-- md:feature -->
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
```
2022-09-11 20:25:40 +03:00
=== "With expansion"
2022-09-11 20:25:40 +03:00
[![Navigation expansion enabled]][Navigation expansion enabled]
2022-09-11 20:25:40 +03:00
=== "Without"
2022-09-11 20:25:40 +03:00
[![Navigation expansion disabled]][Navigation expansion disabled]
2022-09-11 20:25:40 +03:00
[Navigation expansion enabled]: ../assets/screenshots/navigation-expand.png
[Navigation expansion disabled]: ../assets/screenshots/navigation.png
2023-01-14 22:29:27 +03:00
### Navigation path <small>Breadcrumbs</small> { id=navigation-path }
2023-09-14 20:09:18 +03:00
<!-- md:sponsors -->
<!-- md:version insiders-4.28.0 -->
<!-- md:feature -->
<!-- md:flag experimental -->
2023-01-14 22:29:27 +03:00
When navigation paths are activated, a breadcrumb navigation is rendered above
the title of each page, which might make orientation easier for users visiting your
documentation on devices with smaller screens. Add the following lines to
`mkdocs.yml`:
``` yaml
theme:
features:
- navigation.path
```
=== "With navigation path"
[![Navigation path enabled]][Navigation path enabled]
=== "Without"
[![Navigation path disabled]][Navigation path disabled]
[Navigation path enabled]: ../assets/screenshots/navigation-path-on.png
[Navigation path disabled]: ../assets/screenshots/navigation-path-off.png
2022-06-24 15:38:01 +03:00
### Navigation pruning
2022-05-25 10:50:27 +03:00
2023-09-14 20:09:18 +03:00
<!-- md:version 9.2.0 -->
<!-- md:feature -->
<!-- md:flag experimental -->
2022-05-25 10:50:27 +03:00
2023-09-14 20:09:18 +03:00
When pruning is enabled, only the visible navigation items are included in the
rendered HTML, __reducing the size of the built site by 33% or more__. Add the
2022-05-25 10:50:27 +03:00
following lines to `mkdocs.yml`:
``` yaml
theme:
features:
- navigation.prune # (1)!
```
1. This feature flag is not compatible with
[`navigation.expand`][navigation.expand], as navigation expansion requires
the complete navigation structure.
This feature flag is especially useful for documentation sites with 100+ or even
1,000+ of pages, as the navigation makes up a significant fraction of the HTML.
Navigation pruning will replace all expandable sections with links to the first
page in that section (or the section index page).
[navigation.expand]: #navigation-expansion
2021-01-31 18:30:57 +03:00
### Section index pages
2023-09-14 20:09:18 +03:00
<!-- md:version 7.3.0 -->
<!-- md:feature -->
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:
2022-05-25 10:50:27 +03:00
- navigation.indexes # (1)!
2021-01-31 18:30:57 +03:00
```
2022-05-25 10:50:27 +03:00
1. This feature flag is not compatible with [`toc.integrate`][toc.integrate],
as sections cannot host the table of contents due to missing space.
2022-09-11 20:25:40 +03:00
=== "With section index pages"
2021-01-31 18:30:57 +03:00
2022-09-11 20:25:40 +03:00
[![Section index pages enabled]][Section index pages enabled]
2021-01-31 18:30:57 +03:00
2022-09-11 20:25:40 +03:00
=== "Without"
2021-01-31 18:30:57 +03:00
2022-09-11 20:25:40 +03:00
[![Section index pages disabled]][Section index pages 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:
2023-02-26 13:49:11 +03:00
- section/index.md # (1)!
2021-01-31 18:30:57 +03:00
- Page 1: section/page-1.md
...
- Page n: section/page-n.md
```
2023-02-26 13:49:11 +03:00
1. MkDocs also considers files called `README.md` as [index pages].
2022-09-11 20:25:40 +03:00
[Section index pages enabled]: ../assets/screenshots/navigation-index-on.png
[Section index pages disabled]: ../assets/screenshots/navigation-index-off.png
2022-02-06 13:12:14 +03:00
[toc.integrate]: #navigation-integration
2023-02-26 13:49:11 +03:00
[index pages]: https://www.mkdocs.org/user-guide/writing-your-docs/#index-pages
2021-01-31 18:30:57 +03:00
2022-02-06 13:12:14 +03:00
### Table of contents
#### Anchor following
2023-09-14 20:09:18 +03:00
<!-- md:version 8.5.0 -->
<!-- md:feature -->
<!-- md:flag experimental -->
2022-02-06 13:12:14 +03:00
When anchor following for the [table of contents] is enabled, the sidebar is
automatically scrolled so that the active anchor is always visible. Add the
following lines to `mkdocs.yml`:
``` yaml
theme:
features:
- toc.follow
```
#### Navigation integration
2023-09-14 20:09:18 +03:00
<!-- md:version 6.2.0 -->
<!-- md:feature -->
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:
2022-05-25 10:50:27 +03:00
- toc.integrate # (1)!
```
2022-05-25 10:50:27 +03:00
1. This feature flag is not compatible with
[`navigation.indexes`][navigation.indexes], as sections cannot host the
table of contents due to missing space.
2022-09-11 20:25:40 +03:00
=== "With navigation integration"
2022-09-11 20:25:40 +03:00
[![Navigation integration enabled]][Navigation integration enabled]
2022-09-11 20:25:40 +03:00
=== "Without"
2022-09-11 20:25:40 +03:00
[![Navigation integration disabled]][Navigation integration disabled]
2021-10-11 14:38:03 +03:00
[table of contents]: extensions/python-markdown.md#table-of-contents
2022-09-11 20:25:40 +03:00
[Navigation integration enabled]: ../assets/screenshots/toc-integrate.png
[Navigation integration disabled]: ../assets/screenshots/navigation-tabs.png
2021-10-10 18:39:53 +03:00
[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
2023-09-14 20:09:18 +03:00
<!-- md:version 7.1.0 -->
<!-- md:feature -->
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-05-01 18:53:39 +03:00
## Usage
### Hiding the sidebars
2023-09-14 20:09:18 +03:00
<!-- md:version 6.2.0 -->
<!-- md:flag metadata -->
The navigation and/or table of contents sidebars can be hidden for a document
2023-09-14 20:09:18 +03:00
with the front matter `hide` property. Add the following lines at the top of a
Markdown file:
2022-09-11 20:25:40 +03:00
``` yaml
---
hide:
2021-05-30 16:59:13 +03:00
- navigation
- toc
---
2023-09-14 20:09:18 +03:00
# Page title
...
```
=== "Hide navigation"
2022-09-11 20:25:40 +03:00
[![Hide navigation enabled]][Hide navigation enabled]
=== "Hide table of contents"
2022-09-11 20:25:40 +03:00
[![Hide table of contents enabled]][Hide table of contents enabled]
=== "Hide both"
2022-09-11 20:25:40 +03:00
[![Hide both enabled]][Hide both enabled]
2022-09-16 20:08:34 +03:00
[Hide navigation enabled]: ../assets/screenshots/hide-navigation.png
2022-09-11 20:25:40 +03:00
[Hide table of contents enabled]: ../assets/screenshots/hide-toc.png
[Hide both enabled]: ../assets/screenshots/hide-navigation-toc.png
2023-01-17 23:20:16 +03:00
### Hiding the navigation path
2023-09-14 20:09:18 +03:00
<!-- md:sponsors -->
<!-- md:version insiders-4.28.0 -->
<!-- md:flag metadata -->
2023-01-17 23:20:16 +03:00
While the [navigation path] is rendered above the main headline, sometimes, it
might be desirable to hide it for a specific page, which can be achieved with
the front matter `hide` property:
``` yaml
---
hide:
- path
---
2023-09-14 20:09:18 +03:00
# Page title
2023-01-17 23:20:16 +03:00
...
```
[navigation path]: #navigation-path
## Customization
### Keyboard shortcuts
Material for MkDocs includes several keyboard shortcuts that make it possible
2022-06-27 09:32:12 +03:00
to navigate your project documentation via keyboard. There are two modes:
2023-09-14 20:09:18 +03:00
<!-- md:option 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
2023-09-14 20:09:18 +03:00
<!-- md:option 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:
2022-09-11 20:25:40 +03:00
=== ":octicons-file-code-16: `docs/javascripts/shortcuts.js`"
2021-10-10 18:39:53 +03:00
``` js
keyboard$.subscribe(function(key) {
if (key.mode === "global" && key.type === "x") {
/* Add custom keyboard handler here */
2021-12-11 16:30:07 +03:00
key.claim() // (1)!
2021-10-10 18:39:53 +03:00
}
})
```
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.
2022-09-11 20:25:40 +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:
2022-09-11 20:25:40 +03:00
=== ":octicons-file-code-16: `docs/stylesheets/extra.css`"
2020-11-15 17:34:44 +03:00
``` css
.md-grid {
2021-12-11 16:30:07 +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;
}
```
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-11-15 17:34:44 +03:00
```
2021-10-10 18:39:53 +03:00
[additional style sheet]: ../customization.md#additional-css