JA EN

Dark Mode Image Implementation Guide - Switching with picture Element and CSS

· 9 min read

Dark Mode and Image Challenges - Why Image Adaptation Is Necessary

With dark mode's growing adoption, images need adaptation beyond just text and background colors. Displaying light-mode images unchanged in dark mode creates several problems.

Issues that arise:

Not all images need dark mode adaptation. Photos and illustrations are often fine as-is, but these categories should be considered: logos and icons (especially monochrome), screenshots, diagrams and flowcharts, illustrations designed for white backgrounds, and transparent-background images where foreground colors blend with dark backgrounds.

Adaptation methods vary by image type. SVGs allow easy dynamic style changes, while raster images (JPEG, PNG, WebP) may require separate versions.

Image Switching with picture Element - HTML-Only Implementation

Using HTML's <picture> element with media attributes enables dark mode image switching without JavaScript. The browser automatically selects the appropriate image source based on media query evaluation.

Basic implementation:

<picture>
<source srcset="diagram-dark.png" media="(prefers-color-scheme: dark)">
<source srcset="diagram-light.png" media="(prefers-color-scheme: light)">
<img src="diagram-light.png" alt="System architecture diagram">
</picture>

This approach's advantage is being self-contained in HTML without CSS or JavaScript dependencies. Browsers download only the needed image, preventing unnecessary transfers.

Combining with responsive images: When combining format switching (WebP/AVIF) with dark mode switching, <source> elements multiply, but browsers download only the first matching source, so there's no performance impact.

Note: prefers-color-scheme references system settings. It cannot respond to site-specific dark mode toggles (JavaScript-switched). For custom toggles, CSS variables or JavaScript switching is needed.

CSS Dark Mode Image Adaptation - Using Filters and Variables

CSS can adapt the same image file for dark mode without preparing separate versions, keeping management costs low.

CSS filter brightness/contrast adjustment:

@media (prefers-color-scheme: dark) {
.screenshot { filter: brightness(0.8) contrast(1.1); }
.diagram { filter: invert(1) hue-rotate(180deg); }
}

The invert(1) hue-rotate(180deg) combination inverts image colors while rotating hue 180 degrees, reversing brightness while preserving photo color tones. Effective for diagrams and graphs but produces unnatural results on photos.

Background image switching with CSS variables:

:root { --logo-url: url('/images/logo-light.svg'); }
@media (prefers-color-scheme: dark) {
:root { --logo-url: url('/images/logo-dark.svg'); }
}
.logo { background-image: var(--logo-url); }

Opacity adjustment: Reducing image opacity to 80-90% in dark mode improves harmony with dark backgrounds. Particularly effective for photos and illustrations, reducing glare while maintaining visibility.

@media (prefers-color-scheme: dark) {
img:not(.no-dim) { opacity: 0.85; }
img:not(.no-dim):hover { opacity: 1; }
}

SVG Dark Mode Adaptation - Dynamic Color Changes with currentColor

SVG is the most suitable image format for dark mode adaptation. Since styles can be dynamically changed with CSS, a single file can serve both light and dark modes.

Using currentColor: Setting currentColor for SVG fill or stroke inherits the parent element's color property:

<svg viewBox="0 0 24 24">
<path fill="currentColor" d="M12 2L2 7l10 5 10-5-10-5z"/>
</svg>

If CSS text colors are dark-mode-adapted, SVG icons automatically follow. Works with both inline SVG and file references, but CSS doesn't apply to external SVGs loaded via <img> tags.

Multi-color SVG with CSS variables:

<svg><rect fill="var(--svg-bg, #ffffff)" /><text fill="var(--svg-text, #333333)">Label</text></svg>

prefers-color-scheme inside SVG: Embedding <style> elements with media queries inside SVG files makes them self-contained for dark mode without external CSS dependency. Particularly effective for SVG favicons (favicon.svg).

Screenshots and Diagrams - Practical Dark Mode Workflow

Screenshots and diagrams are the most labor-intensive category for dark mode adaptation. Organize automatable versus manual portions to build an efficient workflow.

Screenshot adaptation approaches:

Diagram adaptation: When creating diagrams with Mermaid, Draw.io, or Figma, pre-defining dark mode color themes improves efficiency. Mermaid generates dark mode diagrams via theme settings.

Automation approach: Playwright or Puppeteer can automate dark mode screenshot capture. page.emulateMediaFeatures emulates dark mode, enabling scripts that auto-generate both versions. Integrating into CI/CD pipelines automatically generates both versions on documentation updates.

Performance and Accessibility Considerations

Dark mode image implementation requires attention to both performance and accessibility.

Performance optimization:

Accessibility assurance:

Testing: Chrome DevTools Rendering panel emulates prefers-color-scheme. Switch between modes to verify all images. Run Lighthouse accessibility audits in both modes.

Related Articles

Responsive Images Implementation Guide - Complete Guide to srcset, sizes, and picture Elements

Learn how to serve optimal images based on device screen size and resolution with detailed code examples for responsive image implementation.

Image Format Comparison - JPEG/PNG/WebP/AVIF/GIF/BMP Features and Use Cases

Compare technical characteristics of 6 major image formats. Organized comparison of compression methods, color depth, transparency, animation, and browser support with optimal format selection by use case.

Complete Favicon Creation Guide - ICO, SVG, and PNG Explained

Learn how favicons work, the characteristics of ICO, SVG, and PNG formats, dark mode support, and browser compatibility for modern favicon implementation.

SVG Fundamentals and Practical Usage - From Vector Basics to Animation

Complete guide to SVG from basic structure to practical applications. Learn paths, shapes, text, filters, animation implementation, and optimization techniques for web development.

How to Create HTML Image Maps and Modern Alternatives - Clickable Map Implementation Guide

Learn how to implement image maps using HTML map and area elements. Covers responsive design challenges and modern alternatives using SVG and CSS with practical code examples.

Vector vs Raster - A Guide to Choosing the Right Image Format

Understand the differences between vector and raster images from their underlying mechanics. Learn which format to choose for logos, photos, web design, and more.

Related Terms