CSS: Dark Theme

By Xah Lee. Date: . Last updated: .

Simple CSS to Make Website Use Dark Theme

here's how to make your website support dark theme.

Essentially, use CSS: Media Query on prefers-color-scheme:dark. Then, set text color to white, and background color to black. Also, you might to add black background color to image tag, figure tag, or SVG files.

@media (prefers-color-scheme:dark) {
body {
color:white;
background-color:black;
}

a:visited,a:link {
color:dodgerblue;
}

img {
background-color:white;
}

}

Change Black Border to Grey

you need to change black border to grey or so

dark theme invisible black border
dark theme invisible black border

Add White Background to Img Tag

You need to make all image files with transparent background to solid white. e.g. png, webp, etc files. (gif does not supports transparency)

One easy fix is to use CSS to make all images white background.

@media (prefers-color-scheme: dark) {
img {
background-color:white;
}
}

Problem Example

in the following, the right image is desired. The left got screwed. Even if you remove alpha channel. Meaning, when removing alpha channel of png, you have to make sure, it uses a white background .

dark theme png remove alpha problem
dark theme png remove alpha problem

Problem: Embedded SVG Transparent Background

Embedded SVG that has transparent background needs to have a solid color. 〔see Practical SVG Tutorial

Problem example

In a image illustration for keyboard, the key labels disappears.

dark theme invisible svg jb4q
dark theme invisible svg

Check Pages with Image Background

If you are using a image for background, and if the image has background color white, then white text disappears when in dark theme, because text color is set to white.

dark theme disappearing white text 2022-01-10
dark theme disappearing white text 2022-01-10

Text is white, on a white image background. And there are 93 such img background CSS i have to look at case by case.

dark theme white bg img prob 9Ktt
dark theme white background image problem

Add Text Shadows as Workaround

one of the hack fix is to add text shadows when you do dark theme.

so that pages with white background, during darktheme the text has white color, but now is visible because of text shadow.

body {
background-color:black;
color:white;
text-shadow:2px 2px 2px black;
}

example result:

dark theme white bg img prob 6fSz
dark theme white bg img prob 6fSz

Programing source code syntax coloring problem

Also, programing code syntax coloring is also a problem. Simply invert color or switch black/white create ugly coloring. You need to go thru the colors and fix them.

Problem Example

dark theme syntax coloring problem HJ6G
dark theme syntax coloring problem HJ6G

other problems

the following is the thought flow while i was making my website to support dark theme .

My site is pretty simple. Bascially just 5 lines of CSS will turn it to support dark theme. But there are about 200 SVG files and lots more PNG i need to manual change. In theory, they can be fixed with backdrop-filter CSS, probably. But the problem is, it's really a case by case nasty problem. Because depending on how the image is used.

The heart of the issue is, with images, sometimes you want white background , sometimes transparent. Making them all explicit white doesn't work 100%. And they are not just all img tags. For example, img files are used as background CSS. Yeah, so i think the problem really blows up.

Search All CSS Code, on Color Value of Black or White, background, background-color, border

Check all CSS, on color value of black or white, and on property of color and background-color

CSS, Media Query

Dark Theme

CSS, Advanced