Post

Responsive Image Mode Home.html

Display different images for light and dark mode using .light and .dark classes. Works in the Home Page with Liquid.

Responsive Image Mode Home.html

Light mode only Dark mode only

🖼️ Feature: Dynamic Post Image Light/Dark Mode Switching

1. YAML Front Matter Configuration

Add the following fields to the post’s YAML front matter:

1
2
3
4
5
6
7
8
light_image:
  path: /path/to/light-image.webp
  lqip: data:image/webp;base64,...
  alt: Alt text for light mode image
dark_image:
  path: /path/to/dark-image.webp
  lqip: data:image/webp;base64,...
  alt: Alt text for dark mode image

2.Layout Update

Inside the _layouts in home.html, within the {% if post.image %} block, insert the following code after {% assign card_body_col = '7' %} and before {% endif %}.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
    {% elsif post.light_image and post.dark_image %}
      <div class="col-md-5">
        <div class="preview-img  blur">
          <img
            data-src="{{ post.light_image.path }}"
            alt="{{ post.light_image.alt}}"
            data-lqip="true"
            src="{{ post.light_image.lqip }}"
            class="popup img-link light shimmer"
            loading="lazy"
          >
          <img
            data-src="{{ post.dark_image.path }}"
            alt="{{ post.dark_image.alt}}"
            data-lqip="true"
            src="{{ post.dark_image.lqip }}"
            class="popup img-link dark"
            loading="lazy"
          >
        </div>
      </div>

      {% assign card_body_col = '7' %}
    {% endif %}

At the beginning of the post, use:

1
2
![Light mode only](/path/to/light-image.webp){: .light }
![Dark mode only](/path/to/dark-image.webp){: .dark }

Images can be PNG, not just WebP. Currently, this requires Liquid. If needed, it can be added in home.html.

This post is licensed under CC BY 4.0 by the author.