mkdocs-material/docs/reference/images.md

156 lines
4.9 KiB
Markdown
Raw Normal View History

---
template: overrides/main.html
---
# Images
While images are first-class citizens of Markdown and part of the core syntax,
it can be difficult to work with them. Material for MkDocs makes working with
2021-10-10 13:19:14 +03:00
images more comfortable, providing styles for image alignment and image
captions.
## Configuration
2021-10-10 13:19:14 +03:00
This configuration adds the ability to align images, add captions to images
(rendering them as figures), and mark large images for lazy-loading. Add the
following lines to `mkdocs.yml`:
``` yaml
markdown_extensions:
- attr_list
2021-10-10 13:19:14 +03:00
- md_in_html
```
2021-10-10 13:19:14 +03:00
See additional configuration options:
- [Attribute Lists]
- [Markdown in HTML]
[Attribute Lists]: ../setup/extensions/python-markdown.md#attribute-lists
[Markdown in HTML]: ../setup/extensions/python-markdown.md#markdown-in-html
## Usage
### Image alignment
2021-10-10 13:19:14 +03:00
When [Attribute Lists] is enabled, images can be aligned by adding the
respective alignment directions via the `align` attribute, i.e. `align=left` or
`align=right`:
=== "Left"
_Example_:
``` markdown
![Placeholder](https://dummyimage.com/600x400/eee/aaa){ align=left }
```
_Result_:
![Placeholder](https://dummyimage.com/600x400/f5f5f5/aaaaaa&text=%20Image%20){ align=left width=300 }
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla et euismod
nulla. Curabitur feugiat, tortor non consequat finibus, justo purus auctor
massa, nec semper lorem quam in massa.
=== "Right"
_Example_:
``` markdown
![Placeholder](https://dummyimage.com/600x400/eee/aaa){ align=right }
```
_Result_:
![Placeholder](https://dummyimage.com/600x400/f5f5f5/aaaaaa&text=%20Image%20){ align=right width=300 }
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla et euismod
nulla. Curabitur feugiat, tortor non consequat finibus, justo purus auctor
massa, nec semper lorem quam in massa.
2021-10-10 13:19:14 +03:00
If there's insufficient space to render the text next to the image, the image
will stretch to the full width of the viewport, e.g. on mobile viewports.
??? question "Why is there no centered alignment?"
The [`align`][align] attribute doesn't allow for centered alignment, which
is why this option is not supported by Material for MkDocs.[^1] Instead,
the [image captions] syntax can be used, as captions are optional.
[^1]:
You might also realize that the [`align`][align] attribute has been
deprecated as of HTML5, so why use it anyways? The main reason is
portability it's still supported by all browsers and clients, and is very
unlikely to be completely removed, as many older websites still use it. This
ensures a consistent appearance when a Markdown file with these attributes
is viewed outside of a website generated by Material for MkDocs.
2021-10-10 13:19:14 +03:00
[align]: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img#deprecated_attributes
[image captions]: #image-captions
### Image captions
Sadly, the Markdown syntax doesn't provide native support for image captions,
2021-10-10 13:19:14 +03:00
but it's always possible to use HTML. Using `figure` and `figcaption`, captions
can be added to images.
_Example_:
```html
2021-12-11 16:30:07 +03:00
<figure markdown> <!-- (1)! -->
2021-12-11 17:40:13 +03:00
![Placeholder](https://dummyimage.com/600x400/){ width="300" }
<figcaption>Image caption</figcaption>
</figure>
```
2021-10-05 00:36:31 +03:00
1. :man_raising_hand: Remember to enable the [Markdown in HTML] extension.
_Result_:
2020-09-22 19:09:39 +03:00
2021-10-05 00:36:31 +03:00
<figure markdown>
2021-12-11 17:40:13 +03:00
![Placeholder]{ width="300" }
<figcaption>Image caption</figcaption>
</figure>
2021-12-11 17:40:13 +03:00
[Placeholder]: https://dummyimage.com/600x400/f5f5f5/aaaaaa&text=%20Image%20
2021-10-05 00:36:31 +03:00
[Markdown in HTML]: ../setup/extensions/python-markdown.md#markdown-in-html
### Image lazy-loading
2021-10-10 13:19:14 +03:00
Modern browsers provide [native support for lazy-loading images][lazy-loading]
through the `loading=lazy` directive, which degrades to eager-loading in
2021-10-10 20:22:13 +03:00
browsers without support:
``` markdown
2021-12-11 17:40:13 +03:00
![Placeholder](https://dummyimage.com/600x400/){ loading=lazy }
```
2021-10-10 13:19:14 +03:00
[lazy-loading]: https://caniuse.com/#feat=loading-lazy-attr
2021-12-11 17:40:13 +03:00
### Light and dark mode
[:octicons-tag-24: Unreleased][Light and dark mode support] ·
:octicons-beaker-24: Experimental
If you added a [color palette toggle] and want to show different images for
light and dark color schemes, you can append a `#only-light` or `#only-dark`
hash fragment to the image URL:
_Example_:
``` markdown
![Placeholder light](https://dummyimage.com/600x400/f5f5f5/aaaaaa#only-light)
2021-12-11 19:21:57 +03:00
![Placeholder dark](https://dummyimage.com/600x400/21222c/d5d7e2#only-dark)
2021-12-11 17:40:13 +03:00
```
_Result_:
![Placeholder light]{ width="300" }
![Placeholder dark]{ width="300" }
[Light and dark mode support]: https://github.com/squidfunk/mkdocs-material/releases/tag/8.1.1
[color palette toggle]: ../setup/changing-the-colors.md#color-palette-toggle
[Placeholder light]: https://dummyimage.com/600x400/f5f5f5/aaaaaa&text=%20Image%20#only-light
[Placeholder dark]: https://dummyimage.com/600x400/21222c/d5d7e2&text=%20Image%20#only-dark