Sub topic 11
Adapting images and media
This chapter assumes you have the semantic color tokens from earlier chapters. It also assumes you know the prefers-color-scheme query and the data-theme toggle pattern.
Why images need extra work
CSS colors change when your tokens change. Images do not, because they are fixed pixels. A logo with a white background becomes a bright rectangle on a dark page. A logo with dark text on a transparent background can disappear.
You have three tools:
- Swap in a different file for dark mode.
- Dim the image with a CSS filter.
- Draw the graphic with CSS-controlled color, such as inline SVG.
Swapping files with picture
A picture element lets the browser pick a source by media condition:
<picture>
<source srcset="logo-dark.png" media="(prefers-color-scheme: dark)">
<img src="logo-light.png" alt="Company logo">
</picture>The browser checks each source in order and uses the first one that matches. The img is required. It is the fallback, and it carries the alt text.
Swapping files when you have a toggle
If your page has a manual toggle, use two images and let CSS show one of them. The selectors repeat the logic from the toggle chapter:
<img class="logo-light" src="logo-light.png" alt="Company logo">
<img class="logo-dark" src="logo-dark.png" alt="" hidden>.logo-dark {
display: none;
}
@media (prefers-color-scheme: dark) {
:root:not([data-theme="light"]) .logo-light { display: none; }
:root:not([data-theme="light"]) .logo-dark { display: inline; }
}
:root[data-theme="dark"] .logo-light { display: none; }
:root[data-theme="dark"] .logo-dark { display: inline; }The hidden attribute on the second image is only a no-CSS fallback. The display: inline rules override it because author CSS beats the browser default. Hidden images are usually still downloaded, so use this pattern for small assets like logos, not large photos.
Dimming photos
Photos usually work in both themes, but bright ones can glare against a dark background. A mild filter helps without a second file:
@media (prefers-color-scheme: dark) {
img.photo {
filter: brightness(0.85);
}
}Start near 0.85 and compare it with the original. Go much lower and the photo looks muddy. Apply the filter only to photos. Screenshots and diagrams with their own backgrounds look worse when dimmed.
To make the filter follow the toggle, put the same declaration under :root[data-theme="dark"] img.photo. Also add it inside the :not([data-theme="light"]) media block.
Icons that follow the text color
The cleanest option is an inline SVG that takes its color from the surrounding text. Set fill="currentColor":
<svg width="24" height="24" viewBox="0 0 24 24" fill="currentColor" aria-hidden="true">
<path d="M12 2l3 7h7l-5.5 4.5L18 21l-6-4-6 4 1.5-7.5L2 9h7z"/>
</svg>The icon uses whatever color is active, so it switches with your --text token and needs no extra rules. This works only for inline SVG. An SVG loaded through <img src> cannot see your page's CSS.
Other embedded content
- Transparent PNGs and charts: give them a background, such as a token-colored
backgroundon theimg, so dark lines do not vanish. - Iframes: if the embedded page's
color-schemediffers from the page that embeds it, the browser paints an opaque background behind the frame. Declaringcolor-scheme: light darkon:rootkeeps the common cases consistent. - Video: it needs no change, but check that your custom controls use tokens.
Exercise
A user's system is set to dark, but they click your toggle to choose light. Your logo uses a picture element with a prefers-color-scheme: dark source. Which logo do they see, and how do you fix it?
Show answer
They see the dark-theme logo, because picture follows only the system setting and ignores data-theme. Fix it by using two img elements and showing one with CSS rules that depend on :root[data-theme] and on the :not([data-theme="light"]) media block.