Theme & Front End CSS Intermediate

Add a Basic Dark Mode

Switch your site to dark colours automatically for visitors whose device is set to dark mode.

Tested on WordPress 7.1 No plugin needed Updated
style.css 28 lines
@media (prefers-color-scheme: dark) {
	:root {
		color-scheme: dark;
	}

	body {
		background: #15131F;
		color: #E4E1EE;
	}

	a {
		color: #A898F0;
	}

	h1, h2, h3, h4, h5, h6 {
		color: #FFFFFF;
	}

	input, textarea, select {
		background: #1F1C2C;
		color: #E4E1EE;
		border-color: #3A364D;
	}

	img:not([src$=".svg"]) {
		filter: brightness(0.92);
	}
}

Where to add this

Recommended Additional CSS Appearance → Customize → Additional CSS, or your page builder's global CSS.
Child theme At the end of your child theme's style.css file.
A code snippets plugin As a CSS snippet set to load on the front end.

Many visitors use dark mode on their phones and laptops. A bright white page at night is jarring. This CSS gives your site a dark version that switches on automatically to match their device.

How it works

prefers-color-scheme: dark matches when the visitor’s system is set to dark mode. Inside it, the snippet sets a dark background, light text, a lighter link colour and dark form fields. color-scheme: dark tells the browser to draw its own controls, like scrollbars and checkboxes, in dark mode too.

Photos are dimmed slightly so they don’t glare against the dark background. SVG logos are left alone.

Things to watch

  • This is a starting point. Themes set colours on many elements, such as headers, cards and buttons, so check every page type and add rules for anything that stays light.
  • Dark logos disappear on dark backgrounds. Use a light version of your logo inside the media query, or give it a light background.

How to test it

Switch your device to dark mode, or in Chrome DevTools open Rendering and set “Emulate CSS media feature prefers-color-scheme” to dark. The site should turn dark.

This website uses cookies to enhance your browsing experience and ensure the site functions properly. By continuing to use this site, you acknowledge and accept our use of cookies.

Accept All Accept Required Only