<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Rhys Lloyd</title>
    <description>The latest articles on Forem by Rhys Lloyd (@justrhysism).</description>
    <link>https://forem.com/justrhysism</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F200085%2F48e33743-261f-4368-9bd4-c26242609b6e.jpeg</url>
      <title>Forem: Rhys Lloyd</title>
      <link>https://forem.com/justrhysism</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/justrhysism"/>
    <language>en</language>
    <item>
      <title>Dimming Images for 'Dark Mode'</title>
      <dc:creator>Rhys Lloyd</dc:creator>
      <pubDate>Tue, 14 Jan 2020 10:02:03 +0000</pubDate>
      <link>https://forem.com/justrhysism/dimming-images-for-dark-mode-ijf</link>
      <guid>https://forem.com/justrhysism/dimming-images-for-dark-mode-ijf</guid>
      <description>&lt;p&gt;&lt;em&gt;Note: This article is cross-posted from my blog: &lt;a href="https://rhyslloyd.me/dimming-images-for-dark-mode/"&gt;https://rhyslloyd.me/dimming-images-for-dark-mode/&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;When applying a "Dark Mode" theme to your website or blog, it can be challenging to determine what to do with bright images to avoid blinding your users as they navigate your site; especially when there's no logical dark alternative.&lt;/p&gt;

&lt;p&gt;In my last post, I demonstrated a straightforward way to &lt;a href="https://dev.to/serve-dark-mode-images-natively/"&gt;serve dark-mode alternative images&lt;/a&gt;. However, not all images have a logical dark alternative, nor does everyone have access to, skills with, or time for expensive software to &lt;a href="https://design.tutsplus.com/tutorials/how-to-turn-day-into-night-in-photoshop--cms-29530"&gt;convert light images to dark&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;When I posted my &lt;a href="https://www.reddit.com/r/webdev/comments/d6ch4g/adding_a_dark_mode_scheme_dont_forget_about_your/"&gt;previous article on Reddit&lt;/a&gt;, many Redditors suggested using CSS filters to darken images. While I do believe that the approach is valid, I do think it is a slightly-different use-case than serving completely different images. That said, I think applying CSS filters to darken images could work; so long as the user can &lt;em&gt;remove&lt;/em&gt; the filters. "Opt-in brightness" if you will.&lt;/p&gt;

&lt;p&gt;I wanted to explore this idea further, and try to find some approaches to apply this technique of "dimming" an image and the user toggling the "dimming" on and off.&lt;/p&gt;

&lt;h2&gt;
  
  
  CSS Filters for Image Dimming
&lt;/h2&gt;

&lt;p&gt;There are many ways to dim images using CSS filters, the example I've provided is just one way to achieve the effect. Any specific implementation should be adjusted to match the look and feel of the site.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If you're new to CSS filters, I recommend taking a look at &lt;a href="https://alligator.io/css/css-filter-examples/"&gt;this article on Alligator.io&lt;/a&gt; which demonstrates several filters with examples of what they do.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;For this example, I've chosen to reduce the image brightness and remove some colour to soften the brightness and vividness slightly.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;.dim&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;brightness&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;40%&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;grayscale&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;50%&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nl"&gt;transition-property&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="c"&gt;/* Transition to "off" (dim) */&lt;/span&gt;
    &lt;span class="nl"&gt;transition-duration&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0.5s&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;transition-delay&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0.5s&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c"&gt;/* Delay slightly */&lt;/span&gt;
    &lt;span class="nl"&gt;transition-timing-function&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;ease-out&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nc"&gt;.dim&lt;/span&gt;&lt;span class="nd"&gt;:focus&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
&lt;span class="nc"&gt;.dim&lt;/span&gt;&lt;span class="nd"&gt;:active&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;brightness&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;100%&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;grayscale&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;0%&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="c"&gt;/* Transition to "on" (bright) */&lt;/span&gt;
    &lt;span class="nl"&gt;transition-duration&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0.1s&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;transition-delay&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0.01s&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c"&gt;/* No delay. Set to 0.01 Because weird things happen when set to '0' */&lt;/span&gt;
    &lt;span class="nl"&gt;transition-timing-function&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;ease&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Approach #1: CSS only
&lt;/h2&gt;

&lt;p&gt;Where possible, I prefer to avoid javascript (a conversation for another time); so when looking for solutions I tend to search for browser-native or CSS-only solutions first.&lt;/p&gt;

&lt;p&gt;My first approach was to reveal the image when it is clicked and the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/CSS/:active"&gt;&lt;code&gt;:active&lt;/code&gt; pseudo-class&lt;/a&gt; is triggered.&lt;/p&gt;

&lt;p&gt;Since, &lt;code&gt;:active&lt;/code&gt; is only erm... &lt;em&gt;active&lt;/em&gt; on &lt;strong&gt;mouse down&lt;/strong&gt;, once the user releases the mouse button the image will return to it's dimmed state. The effect can be softened slightly with a slight transition delay. Adding the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/CSS/:focus"&gt;&lt;code&gt;:focus&lt;/code&gt; pseudo-class&lt;/a&gt; also makes this approach &lt;em&gt;somewhat&lt;/em&gt; more accessible; however, it could be a little surprising as there's no indication that the image will brighten when it receives focus.&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/justrhysism/embed/eYOQLvz?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;The beauty of this approach is in its simplicity. It doesn't require any extra markup and uses just a single of pseudo-class for the effect. However, this approach completely breaks down on touch-devices, and either doesn't work or is intermittent on iOS devices (in particular) resulting in a rather &lt;em&gt;infuriating&lt;/em&gt; experience.&lt;/p&gt;

&lt;h2&gt;
  
  
  Approach #2: HTML+CSS
&lt;/h2&gt;

&lt;p&gt;Because the first approach doesn't effectively work on touch devices, I wanted to find a way &lt;em&gt;I could&lt;/em&gt; get it to work (without JavaScript). To achieve this we can leverage the &lt;em&gt;old-school&lt;/em&gt; "&lt;a href="https://css-tricks.com/the-checkbox-hack/"&gt;checkbox hack&lt;/a&gt;".&lt;/p&gt;

&lt;p&gt;The "hack" here is exploiting CSS's &lt;a href="https://developer.mozilla.org/en-US/docs/Web/CSS/:checked"&gt;:checked pseudo-class selector&lt;/a&gt; and the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/CSS/General_sibling_combinator"&gt;general sibling combinator&lt;/a&gt; in the form of:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;input&lt;/span&gt;&lt;span class="nd"&gt;:checked&lt;/span&gt; &lt;span class="o"&gt;~&lt;/span&gt; &lt;span class="nt"&gt;img&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;brightness&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;100%&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;grayscale&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;0%&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;...which enables us to apply different styling when the checkbox is &lt;em&gt;checked&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Wrapping the image with a &lt;code&gt;&amp;lt;label&amp;gt;&lt;/code&gt; and placing a hidden checkbox within it (creating an &lt;a href="https://developer.mozilla.org/en-US/docs/Learn/HTML/Forms/How_to_structure_an_HTML_form#The_%3Clabel%3E_element"&gt;implicit association&lt;/a&gt;), we can provide a simple (&lt;em&gt;somewhat&lt;/em&gt; accessible, assuming it's labelled correctly) toggle which &lt;em&gt;actually works&lt;/em&gt; on touch devices.&lt;/p&gt;

&lt;p&gt;Since we're already adding in some extra markup, let's add in a cute SVG icon to indicate to the user that the image has been dimmed deliberately and &lt;em&gt;not a bug in our CSS&lt;/em&gt; which we were unable to achieve with the CSS-only approach.&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/justrhysism/embed/JjPeaWy?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;While I do love HTML and CSS-only solutions, the most apparent issue with this approach is all the &lt;strong&gt;extra markup required&lt;/strong&gt; to achieve the effect. Frankly, &lt;em&gt;it would be pretty damn annoying&lt;/em&gt; to manually wrap images you wish to dim with all the additional markup, particularly if you're trying apply the technique to an existing site (say, for example, &lt;a href="https://rhyslloyd.me"&gt;a blog&lt;/a&gt;). It would be a rather arduous task to go back through every page and/or post to apply the technique.&lt;/p&gt;

&lt;h2&gt;
  
  
  Approach #3: JavaScript
&lt;/h2&gt;

&lt;p&gt;Recognising the impracticality of the manual HTML+CSS only approach, let's sprinkle &lt;em&gt;a little&lt;/em&gt; JavaScript which enables us to apply the technique.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;For brevity, I'll write another post (soon, I promise!) breaking down the how and why of what the JavaScript is doing.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Building on approach #2, we can apply a very similar technique. Since we're already writing JavaScript, it's (arguably) simpler for us to add the relevant click and keyboard listeners to toggle the dimming effect rather than using the "checkbox hack".&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/justrhysism/embed/gOYqBPq?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;The essential advantage of the JavaScript approach is the ability to apply the technique to existing pages without necessarily needing to go back and update the markup; which means it can be applied to a website retrospectively.&lt;/p&gt;

&lt;h2&gt;
  
  
  Wrapping Up
&lt;/h2&gt;

&lt;p&gt;With all of that said, it may not be desirable to apply the dimming technique to &lt;em&gt;all&lt;/em&gt; images. In my next post, I'll explore a method for automatically determining image brightness so only "bright" images are dimmed.&lt;/p&gt;

&lt;p&gt;In the meantime, reach out to me on Twitter (&lt;a href="https://twitter.com/justrhysism"&gt;@justrhysism&lt;/a&gt;) and let me know if you've explored this idea already, and any alternative approaches you might have!&lt;/p&gt;

&lt;p&gt;Until next time, &lt;em&gt;&lt;a href="https://en.wiktionary.org/wiki/hooroo"&gt;hooroo&lt;/a&gt;!&lt;/em&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>darkmode</category>
      <category>javascript</category>
      <category>css</category>
    </item>
    <item>
      <title>Serve 'Dark Mode' Images Natively</title>
      <dc:creator>Rhys Lloyd</dc:creator>
      <pubDate>Wed, 02 Oct 2019 00:16:39 +0000</pubDate>
      <link>https://forem.com/justrhysism/serve-dark-mode-images-natively-1dfj</link>
      <guid>https://forem.com/justrhysism/serve-dark-mode-images-natively-1dfj</guid>
      <description>&lt;p&gt;&lt;em&gt;&lt;strong&gt;Note:&lt;/strong&gt; This article is cross-posted from my blog: &lt;a href="https://rhyslloyd.me/serve-dark-mode-images-natively/"&gt;https://rhyslloyd.me/serve-dark-mode-images-natively/&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;More and more websites, particularly technical blogs, are leveraging the &lt;em&gt;prefers-color-scheme&lt;/em&gt; media query to provide a dark or light theme based on the user's OS preferences. &lt;/p&gt;

&lt;p&gt;I think this is &lt;em&gt;brilliant&lt;/em&gt;. Personally, I like to dark-mode &lt;strong&gt;all the things&lt;/strong&gt;, so this works for me. That said, when it comes to images (particularly screenshots), I find a white image in a dark theme can be quite jarring. &lt;em&gt;&lt;a href="https://media.giphy.com/media/VKVDU8pvi3w4w/giphy.gif"&gt;My eyes! The goggles do nothing!&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;What if we could present different images tailored for a user's dark (or light) preferences?&lt;/em&gt;&lt;/p&gt;


  
  
  Screen capture demonstrating toggling OS dark and light modes and an image changing in a browser when the mode switch occurs.


&lt;p&gt;Well, it turns out we can! &lt;em&gt;Natively&lt;/em&gt;. That's right! No JavaScript, no CSS wizardry, just good ol' plain ol' &lt;strong&gt;native HTML&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;"I've seen enough, gimmie the code already!"&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The &lt;code&gt;prefers-color-scheme&lt;/code&gt; media feature
&lt;/h2&gt;

&lt;p&gt;This media feature is relatively new to browsers (&lt;a href="https://hacks.mozilla.org/2019/05/firefox-67-dark-mode-css-webrender/"&gt;landed in Firefox&lt;/a&gt; in May 2019) and seems to be spreading like a locust plague as those of us on the dark side clamber for some of that dark-mode goodness. &lt;/p&gt;

&lt;p&gt;The team at &lt;a href="https://developer.mozilla.org/en-US/docs/Web/CSS/@media/prefers-color-scheme"&gt;MDN describe&lt;/a&gt; it more succinctly:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;"The prefers-color-scheme CSS media feature is used to detect if the user has requested the system use a light or dark color theme."&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;em&gt;A very basic demonstration of a &lt;code&gt;prefers-color-scheme&lt;/code&gt; media query:&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;body&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c"&gt;/* Default Light Scheme */&lt;/span&gt;
    &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;black&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;background&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;hsl&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;0%&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;85%&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;@media&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;prefers-color-scheme&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;dark&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nt"&gt;body&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c"&gt;/* Lovely moody Dark Scheme */&lt;/span&gt;
        &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;white&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nl"&gt;background&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;hsl&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;0%&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;15%&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  The &lt;code&gt;&amp;lt;picture&amp;gt;&lt;/code&gt; element
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;I suspect many of you may have just had the &lt;strong&gt;aha!&lt;/strong&gt; moment I had a few weeks ago and figured it out. You might as well just stop reading right now and go and figure out how to extend it further&lt;/em&gt; 😉.  &lt;/p&gt;

&lt;p&gt;The &lt;code&gt;&amp;lt;picture&amp;gt;&lt;/code&gt; element provides a nice native mechanism for presenting different images based on specific media queries.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;A generic &lt;code&gt;&amp;lt;picture&amp;gt;&lt;/code&gt; element example stolen verbatim &lt;a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/picture"&gt;from MDN&lt;/a&gt;:&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;picture&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;source&lt;/span&gt; &lt;span class="na"&gt;srcset=&lt;/span&gt;&lt;span class="s"&gt;"/media/examples/surfer-240-200.jpg"&lt;/span&gt;
            &lt;span class="na"&gt;media=&lt;/span&gt;&lt;span class="s"&gt;"(min-width: 800px)"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;img&lt;/span&gt; &lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"/media/examples/painted-hand-298-332.jpg"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/picture&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  With your powers combined...
&lt;/h2&gt;

&lt;p&gt;Combining these two very standard out-of-the-box native techniques, we can serve different images depending on a user's preferred colour scheme!&lt;/p&gt;

&lt;p&gt;This is a bit like the first time you see sour cream and sweet chilli sauce served together with your potato wedges; at first the combination of these two simple ingredients seems a &lt;em&gt;bit weird&lt;/em&gt;, but together it just works &lt;strong&gt;so well&lt;/strong&gt;. &lt;em&gt;(Oh wait, &lt;a href="https://www.reddit.com/r/australia/comments/2572fh/til_wedges_with_sour_cream_and_sweet_chilli_sauce/"&gt;is that just us Aussies...?&lt;/a&gt;)&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;picture&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;source&lt;/span&gt; &lt;span class="na"&gt;srcset=&lt;/span&gt;&lt;span class="s"&gt;"https://placehold.co/400x300/1D1E22/fff?text=Dark+mode"&lt;/span&gt; &lt;span class="na"&gt;media=&lt;/span&gt;&lt;span class="s"&gt;"(prefers-color-scheme: dark)"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;img&lt;/span&gt; &lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"https://placehold.co/400x300?text=Light+mode"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt; 
&lt;span class="nt"&gt;&amp;lt;/picture&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;em&gt;And a real-life example using some &lt;a href="https://twitter.com/justrhysism"&gt;pleb's twitter profile&lt;/a&gt;; open up your OS theme toggle and switch modes to see the image change:&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;br&gt;
    &lt;br&gt;
    &lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--87hYMcS_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://res.cloudinary.com/justrhysism/image/upload/f_auto%2Cq_auto%2Cw_auto%2Cdpr_auto/posts/twitter-light-mode-justrhysism-small-cap.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--87hYMcS_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://res.cloudinary.com/justrhysism/image/upload/f_auto%2Cq_auto%2Cw_auto%2Cdpr_auto/posts/twitter-light-mode-justrhysism-small-cap.png" alt="Screenshot of @justrhysism's twitter account"&gt;&lt;/a&gt; &lt;br&gt;
&lt;/p&gt;
&lt;h2&gt;
  
  
  Inevitable caveat
&lt;/h2&gt;

&lt;p&gt;Whilst playing with this technique, I quickly realised that it &lt;strong&gt;falls down if you've implemented&lt;/strong&gt; a manual toggle to switch the scheme locally on your website, like the way &lt;a href="https://hankchizljaw.com/wrote/create-a-user-controlled-dark-or-light-mode/"&gt;Andy Bell describes in great detail&lt;/a&gt;. To see what I mean, toggle the theme on this site by clicking the sun (or moon) on the top-right of the viewport.&lt;/p&gt;

&lt;p&gt;This is because the media query doesn't consider your local site settings (which are bespoke), nor is it possible to use CSS selectors inside a media query.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;That said&lt;/em&gt;, I'm sure it wouldn't take much to sprinkle on a bit of magic JS dust to get it all happening. A post for another time.&lt;/p&gt;
&lt;h2&gt;
  
  
  TL;DR Sandbox Demo
&lt;/h2&gt;

&lt;p&gt;In the meantime, check out my sandbox, open your OS scheme settings and have a play around.&lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/dark-mode-images-l3j8o"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  Wrapping up
&lt;/h2&gt;

&lt;p&gt;Sometimes the coolest little techniques are right under our noses, and are just a matter of combining a few ingredients we might not have previously thought about going together.&lt;/p&gt;

&lt;p&gt;By combining the &lt;code&gt;prefers-color-scheme&lt;/code&gt; media feature and the &lt;code&gt;&amp;lt;picture&amp;gt;&lt;/code&gt; element which &lt;em&gt;supports&lt;/em&gt; media queries, we're able to display images better suited to the user's colour-scheme preferences.&lt;/p&gt;

&lt;p&gt;Reach out to me on twitter (&lt;a href="https://twitter.com/justrhysism"&gt;@justrhysism&lt;/a&gt;) and let me know of the cool things you're doing with this technique, or other simple combinations with powerful results. &lt;em&gt;I'd love to see them!&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Until next time, &lt;em&gt;&lt;a href="https://en.wiktionary.org/wiki/hooroo"&gt;hooroo&lt;/a&gt;!&lt;/em&gt;&lt;/p&gt;

</description>
      <category>html</category>
      <category>css</category>
      <category>webdev</category>
      <category>darkmode</category>
    </item>
  </channel>
</rss>
