CSS for Dark Mode: Creating Stylish and User-Friendly Night Themes

Maheen Waris | Oct 6, 2023 min read

In recent years, dark mode has taken the web by storm. It’s not just a trendy feature; it’s a significant enhancement for user experience, accessibility, and aesthetics. With the proliferation of devices and operating systems offering dark mode support, web developers have been increasingly leveraging CSS to provide users with a visually pleasing and comfortable browsing experience, regardless of the time of day. In this article, we’ll explore the world of CSS for dark mode, its benefits, and best practices for implementing it in your web projects.

The Rise of Dark Mode

Dark mode, often referred to as night mode or dark theme, is a design option that flips the traditional white background and black text to a black background with white or light-colored text. While it initially gained popularity for its sleek appearance, dark mode offers several compelling advantages:

1. Reduced Eye Strain:

Dark mode reduces the amount of blue light emitted by screens, which can alleviate eye strain during extended periods of computer use.

2. Improved Battery Life:

On devices with OLED or AMOLED screens, dark mode can save battery life because individual pixels are turned off when displaying black.

3. Enhanced Accessibility:

Dark mode can improve readability for users with visual impairments, as it provides better contrast between text and background.

4. Aesthetic Appeal:

Many users simply prefer the look and feel of dark mode, finding it more aesthetically pleasing and easier on the eyes, especially in low-light environments.

Implementing Dark Mode with CSS

Creating a dark mode for your website or web application involves leveraging CSS to manipulate the styles based on user preferences or system settings. Here are some key considerations and best practices:

1. Media Queries

Media queries are the cornerstone of responsive web design, and they’re equally valuable for implementing dark mode. You can use the prefers-color-scheme media query to detect whether the user’s system is set to light or dark mode. For example:

@media (prefers-color-scheme: dark) {
  /* Dark mode styles */
  body {
    background-color: #121212;
    color: #ffffff;

This CSS snippet applies dark mode styles when the user’s system is set to dark mode.

2. Toggle Switches

To give users the ability to switch between light and dark modes manually, you can create toggle switches or buttons. JavaScript can be used to toggle a class on the body element, which, in turn, triggers the dark mode styles. Here’s a basic example using JavaScript:

const toggleSwitch = document.querySelector("#dark-mode-toggle");
toggleSwitch.addEventListener("change", () => {

In this example, when the #dark-mode-toggle element changes (e.g., a user clicks it), the dark-mode class is toggled on the body element, which triggers the dark mode styles defined in your CSS.

3. Customizable Themes

Consider providing multiple themes beyond just light and dark. Allow users to choose from various color schemes or themes that suit their preferences. Define different sets of CSS rules for each theme and apply them dynamically based on user selections.

4. Accessibility

Ensure that your dark mode implementation is accessible. This means maintaining good color contrast, using appropriate typography, and considering users with vision impairments. Tools like the Web Content Accessibility Guidelines (WCAG) can help you make your dark mode design accessible to all.

5. Test Thoroughly

Test your dark mode implementation on various devices and browsers to ensure consistent behavior and styling. Pay attention to edge cases and user interactions to provide a seamless experience.


CSS for dark mode is not just a trend; it’s a valuable feature that enhances user experience and accessibility. Implementing dark mode in your web projects using CSS and media queries is a step toward catering to user preferences and providing a more comfortable browsing experience, regardless of the time of day or device in use. By following best practices and making your dark mode design accessible and user-friendly, you can create stylish and functional night themes that leave a positive impression on your users.