2020-07-27 17:49:39 +03:00
|
|
|
|
---
|
2021-12-16 19:08:57 +03:00
|
|
|
|
icon: material/image-frame
|
2020-07-27 17:49:39 +03:00
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
# Images
|
|
|
|
|
|
2023-09-14 20:09:18 +03:00
|
|
|
|
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.
|
2020-07-27 17:49:39 +03:00
|
|
|
|
|
|
|
|
|
## 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`:
|
2020-07-27 17:49:39 +03:00
|
|
|
|
|
|
|
|
|
``` yaml
|
|
|
|
|
markdown_extensions:
|
|
|
|
|
- attr_list
|
2021-10-10 13:19:14 +03:00
|
|
|
|
- md_in_html
|
2020-07-27 17:49:39 +03:00
|
|
|
|
```
|
|
|
|
|
|
2021-10-10 13:19:14 +03:00
|
|
|
|
See additional configuration options:
|
|
|
|
|
|
2024-01-28 11:51:25 +03:00
|
|
|
|
- [Attribute Lists]{ data-preview="" }
|
|
|
|
|
- [Markdown in HTML]{ data-preview="" }
|
2021-10-10 13:19:14 +03:00
|
|
|
|
|
|
|
|
|
[Attribute Lists]: ../setup/extensions/python-markdown.md#attribute-lists
|
|
|
|
|
[Markdown in HTML]: ../setup/extensions/python-markdown.md#markdown-in-html
|
2020-07-27 17:49:39 +03:00
|
|
|
|
|
2022-07-17 13:29:30 +03:00
|
|
|
|
### Lightbox
|
|
|
|
|
|
2023-09-14 20:09:18 +03:00
|
|
|
|
<!-- md:version 0.1.0 -->
|
|
|
|
|
<!-- md:plugin [glightbox] -->
|
2022-07-17 13:29:30 +03:00
|
|
|
|
|
2023-09-14 20:09:18 +03:00
|
|
|
|
If you want to add image zoom functionality to your documentation, the
|
2022-07-17 13:29:30 +03:00
|
|
|
|
[glightbox] plugin is an excellent choice, as it integrates perfectly
|
|
|
|
|
with Material for MkDocs. Install it with `pip`:
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
pip install mkdocs-glightbox
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
Then, add the following lines to `mkdocs.yml`:
|
|
|
|
|
|
|
|
|
|
``` yaml
|
|
|
|
|
plugins:
|
|
|
|
|
- glightbox
|
|
|
|
|
```
|
|
|
|
|
|
2022-07-18 13:55:50 +03:00
|
|
|
|
We recommend checking out the available
|
2022-07-17 13:29:30 +03:00
|
|
|
|
[configuration options][glightbox options].
|
|
|
|
|
|
|
|
|
|
[glightbox]: https://github.com/blueswen/mkdocs-glightbox
|
|
|
|
|
[glightbox options]: https://github.com/blueswen/mkdocs-glightbox#usage
|
|
|
|
|
|
2020-07-27 17:49:39 +03:00
|
|
|
|
## 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`:
|
2020-07-27 17:49:39 +03:00
|
|
|
|
|
|
|
|
|
=== "Left"
|
|
|
|
|
|
2022-01-10 16:31:58 +03:00
|
|
|
|
``` markdown title="Image, aligned to left"
|
2021-12-13 21:32:45 +03:00
|
|
|
|
![Image title](https://dummyimage.com/600x400/eee/aaa){ align=left }
|
2020-07-27 17:49:39 +03:00
|
|
|
|
```
|
|
|
|
|
|
2022-01-10 16:31:58 +03:00
|
|
|
|
<div class="result" markdown>
|
2020-07-27 17:49:39 +03:00
|
|
|
|
|
2023-02-06 15:59:33 +03:00
|
|
|
|
![Image title](https://dummyimage.com/600x400/f5f5f5/aaaaaa?text=–%20Image%20–){ align=left width=300 }
|
2020-07-27 17:49:39 +03:00
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
2022-01-10 16:31:58 +03:00
|
|
|
|
</div>
|
2020-07-27 17:49:39 +03:00
|
|
|
|
|
2022-01-10 16:31:58 +03:00
|
|
|
|
=== "Right"
|
2020-07-27 17:49:39 +03:00
|
|
|
|
|
2022-01-10 16:31:58 +03:00
|
|
|
|
``` markdown title="Image, aligned to right"
|
2021-12-13 21:32:45 +03:00
|
|
|
|
![Image title](https://dummyimage.com/600x400/eee/aaa){ align=right }
|
2020-07-27 17:49:39 +03:00
|
|
|
|
```
|
|
|
|
|
|
2022-01-10 16:31:58 +03:00
|
|
|
|
<div class="result" markdown>
|
2020-07-27 17:49:39 +03:00
|
|
|
|
|
2023-02-06 15:59:33 +03:00
|
|
|
|
![Image title](https://dummyimage.com/600x400/f5f5f5/aaaaaa?text=–%20Image%20–){ align=right width=300 }
|
2020-07-27 17:49:39 +03:00
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
2022-01-10 16:31:58 +03:00
|
|
|
|
</div>
|
|
|
|
|
|
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.
|
2020-07-27 17:49:39 +03:00
|
|
|
|
|
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
|
2020-08-16 10:54:08 +03:00
|
|
|
|
|
2020-07-27 17:49:39 +03:00
|
|
|
|
### Image captions
|
|
|
|
|
|
|
|
|
|
Sadly, the Markdown syntax doesn't provide native support for image captions,
|
2022-01-10 16:31:58 +03:00
|
|
|
|
but it's always possible to use the [Markdown in HTML] extension with literal
|
|
|
|
|
`figure` and `figcaption` tags:
|
2020-07-27 17:49:39 +03:00
|
|
|
|
|
2022-01-10 16:31:58 +03:00
|
|
|
|
``` html title="Image with caption"
|
|
|
|
|
<figure markdown>
|
2021-12-13 21:32:45 +03:00
|
|
|
|
![Image title](https://dummyimage.com/600x400/){ width="300" }
|
2020-07-27 17:49:39 +03:00
|
|
|
|
<figcaption>Image caption</figcaption>
|
|
|
|
|
</figure>
|
|
|
|
|
```
|
|
|
|
|
|
2022-01-10 16:31:58 +03:00
|
|
|
|
<div class="result">
|
|
|
|
|
<figure>
|
2023-02-06 15:59:33 +03:00
|
|
|
|
<img src="https://dummyimage.com/600x400/f5f5f5/aaaaaa?text=–%20Image%20–" width="300" />
|
2022-01-10 16:31:58 +03:00
|
|
|
|
<figcaption>Image caption</figcaption>
|
|
|
|
|
</figure>
|
|
|
|
|
</div>
|
2021-10-05 00:36:31 +03:00
|
|
|
|
|
2020-08-16 10:54:08 +03:00
|
|
|
|
### 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:
|
2020-08-16 10:54:08 +03:00
|
|
|
|
|
2022-01-10 16:31:58 +03:00
|
|
|
|
``` markdown title="Image, lazy-loaded"
|
2021-12-13 21:32:45 +03:00
|
|
|
|
![Image title](https://dummyimage.com/600x400/){ loading=lazy }
|
2020-08-16 10:54:08 +03:00
|
|
|
|
```
|
|
|
|
|
|
2022-01-10 16:31:58 +03:00
|
|
|
|
<div class="result" markdown>
|
2023-02-06 15:59:33 +03:00
|
|
|
|
<img src="https://dummyimage.com/600x400/f5f5f5/aaaaaa?text=–%20Image%20–" width="300" />
|
2022-01-10 16:31:58 +03:00
|
|
|
|
</div>
|
|
|
|
|
|
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
|
|
|
|
|
|
2023-09-14 20:09:18 +03:00
|
|
|
|
<!-- md:version 8.1.1 -->
|
2021-12-11 17:40:13 +03:00
|
|
|
|
|
|
|
|
|
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:
|
|
|
|
|
|
2022-01-10 16:31:58 +03:00
|
|
|
|
``` markdown title="Image, different for light and dark mode"
|
2021-12-13 21:32:45 +03:00
|
|
|
|
![Image title](https://dummyimage.com/600x400/f5f5f5/aaaaaa#only-light)
|
|
|
|
|
![Image title](https://dummyimage.com/600x400/21222c/d5d7e2#only-dark)
|
2021-12-11 17:40:13 +03:00
|
|
|
|
```
|
|
|
|
|
|
2022-01-10 16:31:58 +03:00
|
|
|
|
<div class="result" markdown>
|
2021-12-11 17:40:13 +03:00
|
|
|
|
|
2021-12-13 21:32:45 +03:00
|
|
|
|
![Zelda light world]{ width="300" }
|
|
|
|
|
![Zelda dark world]{ width="300" }
|
2021-12-11 17:40:13 +03:00
|
|
|
|
|
2022-01-10 16:31:58 +03:00
|
|
|
|
</div>
|
|
|
|
|
|
2023-05-12 17:51:42 +03:00
|
|
|
|
!!! warning "Requirements when using [custom color schemes]"
|
|
|
|
|
|
|
|
|
|
The built-in [color schemes] define the aforementioned hash fragments, but
|
|
|
|
|
if you're using [custom color schemes], you'll also have to add the
|
|
|
|
|
following selectors to your scheme, depending on whether it's a light or
|
|
|
|
|
dark scheme:
|
|
|
|
|
|
|
|
|
|
=== "Custom light scheme"
|
|
|
|
|
|
|
|
|
|
``` css
|
2023-06-22 22:59:22 +03:00
|
|
|
|
[data-md-color-scheme="custom-light"] img[src$="#only-dark"],
|
2023-05-12 17:51:42 +03:00
|
|
|
|
[data-md-color-scheme="custom-light"] img[src$="#gh-dark-mode-only"] {
|
|
|
|
|
display: none; /* Hide dark images in light mode */
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Custom dark scheme"
|
|
|
|
|
|
|
|
|
|
``` css
|
|
|
|
|
[data-md-color-scheme="custom-dark"] img[src$="#only-light"],
|
|
|
|
|
[data-md-color-scheme="custom-dark"] img[src$="#gh-light-mode-only"] {
|
|
|
|
|
display: none; /* Hide light images in dark mode */
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
Remember to change `#!css "custom-light"` and `#!css "custom-dark"` to the
|
|
|
|
|
name of your scheme.
|
|
|
|
|
|
2021-12-11 17:40:13 +03:00
|
|
|
|
[color palette toggle]: ../setup/changing-the-colors.md#color-palette-toggle
|
2021-12-13 21:32:45 +03:00
|
|
|
|
[Zelda light world]: ../assets/images/zelda-light-world.png#only-light
|
|
|
|
|
[Zelda dark world]: ../assets/images/zelda-dark-world.png#only-dark
|
2023-05-12 17:51:42 +03:00
|
|
|
|
[color schemes]: ../setup/changing-the-colors.md#color-scheme
|
|
|
|
|
[custom color schemes]: ../setup/changing-the-colors.md#custom-color-schemes
|