1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648
|
# Setting up navigation
A clear and concise navigation structure is an important aspect of good project
documentation. Material for MkDocs provides a multitude of options to configure
the behavior of navigational elements, including [tabs] and [sections], and one
of its flagship features: [instant loading].
[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
<!-- md:version 5.0.0 -->
<!-- md:feature -->
When instant loading is enabled, clicks on all internal links will be
intercepted and dispatched via [XHR] without fully reloading the page. Add
the following lines to `mkdocs.yml`:
``` yaml
theme:
features:
- navigation.instant
```
The resulting page is parsed and injected and all event handlers and components
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.
!!! 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
```
[XHR]: https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest
#### Instant prefetching
<!-- md:sponsors -->
<!-- md:version insiders-4.36.0 -->
<!-- md:feature -->
<!-- md:flag experimental -->
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
```
#### 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.
### Instant previews
<!-- 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
keep the user in context. Instant previews can be enabled on any header link
with the `data-preview` attribute:
```` 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>
!!! 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.
#### 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.
---
Instant previews can also be enabled globally by adding the following lines to
`mkdocs.yml`, which will enable instant previews for all header links,
alleviating the need to add data attributes:
``` yaml
theme:
features:
- navigation.instant.preview
```
!!! 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
```
### Anchor tracking
<!-- md:version 8.0.0 -->
<!-- md:feature -->
When anchor tracking is enabled, the URL in the address bar is automatically
updated with the active anchor as highlighted in the table of contents. Add the
following lines to `mkdocs.yml`:
``` yaml
theme:
features:
- navigation.tracking
```
### Navigation tabs
<!-- md:version 1.1.0 -->
<!-- md:feature -->
When tabs are enabled, top-level sections are rendered in a menu layer below
the header for viewports above `1220px`, but remain as-is on mobile.[^1] Add
the following lines to `mkdocs.yml`:
[^1]:
Prior to <!-- md:version 6.2.0 -->, navigation tabs had a slightly different
behavior. All top-level pages (i.e. all top-level entries directly
referring 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 <!-- md:version 6.2.0 -->
on, navigation tabs include all top-level pages and sections.
``` yaml
theme:
features:
- navigation.tabs
```
=== "With tabs"
[![Navigation tabs enabled]][Navigation tabs enabled]
=== "Without"
[![Navigation tabs disabled]][Navigation tabs disabled]
[Navigation tabs enabled]: ../assets/screenshots/navigation-tabs.png
[Navigation tabs disabled]: ../assets/screenshots/navigation.png
#### Sticky navigation tabs
<!-- md:version 7.3.0 -->
<!-- md:feature -->
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
```
=== "With sticky tabs"
[![Sticky navigation tabs enabled]][Sticky navigation tabs enabled]
=== "Without"
[![Sticky navigation tabs disabled]][Sticky navigation tabs disabled]
[Sticky navigation tabs enabled]: ../assets/screenshots/navigation-tabs-sticky.png
[Sticky navigation tabs disabled]: ../assets/screenshots/navigation-tabs-collapsed.png
### Navigation sections
<!-- md:version 6.2.0 -->
<!-- md:feature -->
When sections are enabled, top-level sections are rendered as groups in the
sidebar for viewports above `1220px`, but remain as-is on mobile. Add the
following lines to `mkdocs.yml`:
``` yaml
theme:
features:
- navigation.sections
```
=== "With sections"
[![Navigation sections enabled]][Navigation sections enabled]
=== "Without"
[![Navigation sections disabled]][Navigation sections disabled]
[Navigation sections enabled]: ../assets/screenshots/navigation-sections.png
[Navigation sections disabled]: ../assets/screenshots/navigation.png
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
<!-- md:version 6.2.0 -->
<!-- md:feature -->
When expansion is enabled, the left sidebar will expand all collapsible
subsections by default, so the user doesn't have to open subsections manually.
Add the following lines to `mkdocs.yml`:
``` yaml
theme:
features:
- navigation.expand
```
=== "With expansion"
[![Navigation expansion enabled]][Navigation expansion enabled]
=== "Without"
[![Navigation expansion disabled]][Navigation expansion disabled]
[Navigation expansion enabled]: ../assets/screenshots/navigation-expand.png
[Navigation expansion disabled]: ../assets/screenshots/navigation.png
### Navigation path <small>Breadcrumbs</small> { id=navigation-path }
<!-- md:sponsors -->
<!-- md:version insiders-4.28.0 -->
<!-- md:feature -->
<!-- md:flag experimental -->
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
### Navigation pruning
<!-- md:version 9.2.0 -->
<!-- md:feature -->
<!-- md:flag experimental -->
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
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
### Section index pages
<!-- md:version 7.3.0 -->
<!-- md:feature -->
When section index pages are enabled, documents can be directly attached to
sections, which is particularly useful for providing overview pages. Add the
following lines to `mkdocs.yml`:
``` yaml
theme:
features:
- navigation.indexes # (1)!
```
1. This feature flag is not compatible with [`toc.integrate`][toc.integrate],
as sections cannot host the table of contents due to missing space.
=== "With section index pages"
[![Section index pages enabled]][Section index pages enabled]
=== "Without"
[![Section index pages disabled]][Section index pages disabled]
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:
``` yaml
nav:
- Section:
- section/index.md # (1)!
- Page 1: section/page-1.md
...
- Page n: section/page-n.md
```
1. MkDocs also considers files called `README.md` as [index pages].
[Section index pages enabled]: ../assets/screenshots/navigation-index-on.png
[Section index pages disabled]: ../assets/screenshots/navigation-index-off.png
[toc.integrate]: #navigation-integration
[index pages]: https://www.mkdocs.org/user-guide/writing-your-docs/#index-pages
### Table of contents
#### Anchor following
<!-- md:version 8.5.0 -->
<!-- md:feature -->
<!-- md:flag experimental -->
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
<!-- md:version 6.2.0 -->
<!-- md:feature -->
When navigation integration for the [table of contents] is enabled, it is always
rendered as part of the navigation sidebar on the left. Add the following lines
to `mkdocs.yml`:
``` yaml
theme:
features:
- toc.integrate # (1)!
```
1. This feature flag is not compatible with
[`navigation.indexes`][navigation.indexes], as sections cannot host the
table of contents due to missing space.
=== "With navigation integration"
[![Navigation integration enabled]][Navigation integration enabled]
=== "Without"
[![Navigation integration disabled]][Navigation integration disabled]
[table of contents]: extensions/python-markdown.md#table-of-contents
[Navigation integration enabled]: ../assets/screenshots/toc-integrate.png
[Navigation integration disabled]: ../assets/screenshots/navigation-tabs.png
[navigation.indexes]: #section-index-pages
### Back-to-top button
<!-- md:version 7.1.0 -->
<!-- md:feature -->
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`:
``` yaml
theme:
features:
- navigation.top
```
## Usage
### Hiding the sidebars
<!-- md:version 6.2.0 -->
<!-- md:flag metadata -->
The navigation and/or table of contents sidebars can be hidden for a document
with the front matter `hide` property. Add the following lines at the top of a
Markdown file:
``` yaml
---
hide:
- navigation
- toc
---
# Page title
...
```
=== "Hide navigation"
[![Hide navigation enabled]][Hide navigation enabled]
=== "Hide table of contents"
[![Hide table of contents enabled]][Hide table of contents enabled]
=== "Hide both"
[![Hide both enabled]][Hide both enabled]
[Hide navigation enabled]: ../assets/screenshots/hide-navigation.png
[Hide table of contents enabled]: ../assets/screenshots/hide-toc.png
[Hide both enabled]: ../assets/screenshots/hide-navigation-toc.png
### Hiding the navigation path
<!-- md:sponsors -->
<!-- md:version insiders-4.28.0 -->
<!-- md:flag metadata -->
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
---
# Page title
...
```
[navigation path]: #navigation-path
## Customization
### Keyboard shortcuts
Material for MkDocs includes several keyboard shortcuts that make it possible
to navigate your project documentation via keyboard. There are two modes:
<!-- 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:
* ++arrow-down++ , ++arrow-up++ : select next / previous result
* ++esc++ , ++tab++ : close search dialog
* ++enter++ : follow selected result
<!-- md:option mode:global -->
: 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:
* ++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
JavaScript], you can subscribe to the `keyboard$` observable and attach
your custom event listener:
=== ":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)!
}
})
```
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.
=== ":octicons-file-code-16: `mkdocs.yml`"
``` yaml
extra_javascript:
- javascripts/shortcuts.js
```
[additional JavaScript]: ../customization.md#additional-javascript
### Content area width
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.
This can easily be achieved with an [additional style sheet] and a few lines
of CSS:
=== ":octicons-file-code-16: `docs/stylesheets/extra.css`"
``` css
.md-grid {
max-width: 1440px; /* (1)! */
}
```
1. If you want the content area to always stretch to the available screen
space, reset `max-width` with the following CSS:
``` css
.md-grid {
max-width: initial;
}
```
=== ":octicons-file-code-16: `mkdocs.yml`"
``` yaml
extra_css:
- stylesheets/extra.css
```
[additional style sheet]: ../customization.md#additional-css
|