Skip to content

Inverting images for dark mode with pure CSS

A simple CSS trick to make light-background screenshots look good in dark mode without maintaining two versions of every image.

March 21, 2026
4 min read

When I added Catppuccin dark mode to this site (Catppuccin is a pastel color theme with light and dark variants), most things adapted automatically thanks to DaisyUI’s semantic color classes. But screenshots taken in light mode stuck out: bright white rectangles in an otherwise dark page.

Maintaining dark versions of every image isn’t practical for a blog whose old posts are full of screenshots of third-party UIs. You can see the result in action on this post about Dependabot. Switch to dark mode and watch the screenshots adapt.

The trick: invert plus hue-rotate

filter: invert(1) flips all colors. Black becomes white, white becomes black. The problem is that it also inverts all hues: blue becomes orange, green becomes purple. Adding hue-rotate(180deg) rotates the hues back by 180°, mostly restoring the original colors while keeping the inverted brightness. Not pixel-perfect, but pretty close.

.markdown img:not([src$=".svg"]) {
  filter: invert(1) hue-rotate(180deg);
}

SVGs are excluded since they typically use currentColor and already adapt to the theme. That’s basically it, but as written the rule inverts images in light mode as well. How it gets scoped to dark mode is further down.

Adding a caption with CSS pseudo-selectors

Since the colors won’t be 100% accurate, a small note below each image makes this transparent to readers. Markdown renders images as <p><img /></p>, so :has() can target the wrapping paragraph:

.markdown p:has(> img:not([src$=".svg"]))::after {
  content: "(colors inverted programmatically for dark mode)";
  display: block;
  text-align: center;
  font-size: 0.75rem;
  font-style: italic;
  opacity: 0.5;
}

Scoping it to dark mode

The theme switcher on this site has three states. An explicit choice sets data-theme="latte" or data-theme="mocha" on the <html> element; choosing “system” removes the attribute and leaves the decision to the operating system. So “dark mode” is two different conditions: the attribute says mocha, or there is no attribute and prefers-color-scheme is dark.

CSS has no way to combine a selector and a media query in a single rule, so the rules are written twice. The first copy is prefixed with [data-theme="mocha"]. The second sits inside @media (prefers-color-scheme: dark) and is prefixed with :root:not([data-theme]), so that an explicit choice of the light theme on a dark desktop leaves the images alone.

The specificity rabbit hole

My first version put these rules inside @layer components, next to the rest of the markdown styling, and the caption ended up a long way below its image. @tailwindcss/typography sets margin-bottom: 2em on images through a :where(img) selector. :where() has zero specificity, so on paper my margin-bottom: 0 should have won.

It did not, because the plugin’s rule lives in a layer that comes after @layer components, and cascade layers are compared before specificity is even looked at: a declaration in a later layer beats one in an earlier layer no matter how specific the selectors are. Unlayered styles beat every layer, so the fix was to move the rules out of any layer.

The full solution

This is what the site runs, in app.css outside any @layer block:

[data-theme="mocha"] .markdown img:not([src$=".svg"]) {
  filter: invert(1) hue-rotate(180deg);
  border-radius: var(--radius-box);
  margin-bottom: 0;
}

[data-theme="mocha"] .markdown p:has(> img:not([src$=".svg"]))::after {
  content: "(colors inverted programmatically for dark mode)";
  @apply mt-2 block text-center text-xs italic opacity-50;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme]) .markdown img:not([src$=".svg"]) {
    filter: invert(1) hue-rotate(180deg);
    border-radius: var(--radius-box);
    margin-bottom: 0;
  }

  :root:not([data-theme]) .markdown p:has(> img:not([src$=".svg"]))::after {
    content: "(colors inverted programmatically for dark mode)";
    @apply mt-2 block text-center text-xs italic opacity-50;
  }
}

Two details differ from the minimal rules above. margin-bottom: 0 on the image replaces the plugin’s 2em so the caption sits directly beneath it, with mt-2 on the caption as the only gap. border-radius: var(--radius-box) gives the image the same rounded corners as the theme’s other boxes.

It takes two filters and one pseudo-element, with no build step, no JavaScript and no image processing. The old posts keep their original files.

—

Back to Blog

Next: Ecto.UUID.cast/1 accepts any sixteen-byte string as a UUID

Phil-Bastian Berndt

Phil-Bastian Berndt
Tech Lead at Naymspace