How Do You Create Dark Mode with CSS?

Learn How Do You Create Dark Mode with CSS? with practical examples, explanations, best practices, and real-world coding techniques.

Try Editor

CSS dark mode is a design approach that provides a darker color theme for a website or application. A dark theme typically uses darker backgrounds with appropriately adjusted text, borders, surfaces, and accent colors to create a comfortable alternative to a light interface.

Modern CSS provides several ways to implement dark mode. You can respond automatically to a user's operating-system preference using prefers-color-scheme, define supported color schemes with color-scheme, or create a manual theme switcher using CSS custom properties and a class or attribute. CSS also provides the light-dark() color function for expressing light and dark color alternatives. ([developer.mozilla.org](https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/At-rules/%40media/prefers-color-scheme?utm_source=chatgpt.com))

Common Dark Mode Approaches

prefers-color-scheme

Automatically responds to the user's preferred color scheme.

CSS Variables

Centralize theme colors and replace them when the theme changes.

color-scheme

Tell the browser which color schemes the page or component supports.

light-dark()

Select between two color values based on the active color scheme.

What Is Dark Mode?

Dark mode is an alternative visual theme in which the interface uses darker surfaces and lighter foreground content. It is commonly used for websites, dashboards, developer tools, mobile applications, and content-heavy interfaces.

A good dark theme is not simply a light theme with every color inverted. Backgrounds, text, borders, icons, form controls, shadows, illustrations, and interactive states may all need to be adjusted so that the interface remains readable and visually consistent.

Why Use Dark Mode?

User Preference

Some users prefer darker interfaces as a personal visual preference.

Comfortable Viewing

A dark theme can feel more comfortable for some users in low-light environments.

Product Design

Dark themes are often useful for applications where users spend long periods viewing content.

The Simplest Dark Mode with CSS

One simple approach is to use CSS custom properties for the theme colors and override them inside a dark-theme selector.

:root {
  --background: #ffffff;
  --text: #1e293b;
  --surface: #f8fafc;
  --border: #cbd5e1;
}

.dark {
  --background: #0f172a;
  --text: #f8fafc;
  --surface: #1e293b;
  --border: #475569;
}

body {
  background-color: var(--background);
  color: var(--text);
}

.card {
  background-color: var(--surface);
  border: 1px solid var(--border);
}

The important idea is that components use semantic variables instead of hardcoding separate light and dark colors everywhere.

Using CSS Variables for Dark Mode

CSS custom properties are especially useful for theme systems because a value can be defined once and consumed by many components.

:root {
  --color-bg: #ffffff;
  --color-text: #0f172a;
  --color-primary: #ea580c;
  --color-muted: #64748b;
  --color-surface: #f8fafc;
}

.dark {
  --color-bg: #020617;
  --color-text: #f8fafc;
  --color-primary: #fb923c;
  --color-muted: #94a3b8;
  --color-surface: #0f172a;
}

Components can then use the variables:

body {
  background: var(--color-bg);
  color: var(--color-text);
}

.card {
  background: var(--color-surface);
}

.button {
  background: var(--color-primary);
}

.muted {
  color: var(--color-muted);
}

Automatic Dark Mode with prefers-color-scheme

The prefers-color-scheme media feature allows a page to detect whether the user has indicated a preference for a light or dark color scheme through operating-system or user-agent settings. ([developer.mozilla.org](https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/At-rules/%40media/prefers-color-scheme?utm_source=chatgpt.com))

body {
  background: #ffffff;
  color: #1e293b;
}

@media (prefers-color-scheme: dark) {
  body {
    background: #0f172a;
    color: #f8fafc;
  }
}

When the user's active preference is dark, the media query matches and the dark styles apply. When the preference is light, the dark override does not apply. ([developer.mozilla.org](https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/At-rules/%40media/prefers-color-scheme?utm_source=chatgpt.com))

Dark Mode with CSS Variables and prefers-color-scheme

Combining custom properties with prefers-color-scheme makes the theme easier to maintain.

:root {
  --bg: #ffffff;
  --text: #1e293b;
  --surface: #f8fafc;
  --border: #cbd5e1;
}

@media (prefers-color-scheme: dark) {

  :root {
    --bg: #020617;
    --text: #f8fafc;
    --surface: #0f172a;
    --border: #334155;
  }

}

body {
  background-color: var(--bg);
  color: var(--text);
}

.card {
  background-color: var(--surface);
  border-color: var(--border);
}

This approach avoids duplicating component rules. The components simply consume semantic variables while the theme defines the actual color values.

What Is color-scheme in CSS?

The color-scheme property tells the browser which color schemes an element or document supports. It can influence browser-provided UI such as form controls, scrollbars, and other user-agent interface surfaces. ([developer.mozilla.org](https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Properties/color-scheme?utm_source=chatgpt.com))

:root {
  color-scheme: light dark;
}

This tells the browser that the page supports both light and dark color schemes. Your own page styles can still use media queries or other mechanisms to define the exact visual appearance. ([developer.mozilla.org](https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Properties/color-scheme?utm_source=chatgpt.com))

Why color-scheme Matters for Forms

Native controls such as inputs and scrollbars can have browser-provided colors. Declaring a supported color scheme allows the browser to adapt its own UI to the selected scheme. ([developer.mozilla.org](https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Properties/color-scheme?utm_source=chatgpt.com))

:root {
  color-scheme: light dark;
}

input,
textarea,
select {
  font: inherit;
}

This is particularly useful when creating a dark application with native form controls.

What Is light-dark() in CSS?

The light-dark() CSS function lets you provide two color or image values. The first value is used for a light color scheme and the second for a dark color scheme, based on the element's active color scheme. ([developer.mozilla.org](https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Values/color_value/light-dark?utm_source=chatgpt.com))

:root {
  color-scheme: light dark;
}

body {
  color: light-dark(
    #1e293b,
    #f8fafc
  );

  background-color: light-dark(
    #ffffff,
    #0f172a
  );
}

This can express light and dark color choices directly in the property value without writing a separate prefers-color-scheme media query for every property. ([developer.mozilla.org](https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Values/color_value/light-dark?utm_source=chatgpt.com))

light-dark() with CSS Variables

You can combine light-dark() with custom properties to create a compact theme definition.

:root {
  color-scheme: light dark;

  --page-bg: light-dark(
    #ffffff,
    #020617
  );

  --page-text: light-dark(
    #1e293b,
    #f8fafc
  );

  --surface: light-dark(
    #f8fafc,
    #0f172a
  );
}

body {
  background: var(--page-bg);
  color: var(--page-text);
}

.card {
  background: var(--surface);
}

Manual Dark Mode with a Class

Many applications need a theme switcher so that the user can choose light or dark mode manually. A common approach is to add a class such as .dark to the root element.

:root {
  --bg: #ffffff;
  --text: #1e293b;
}

html.dark {
  --bg: #020617;
  --text: #f8fafc;
}

body {
  background: var(--bg);
  color: var(--text);
}

JavaScript can then add or remove the class when the user changes the theme. CSS remains responsible for the actual appearance.

Manual Theme Switching with data-theme

An attribute can be used instead of a class. This approach is useful when the application supports multiple themes rather than only light and dark.

:root {
  --bg: #ffffff;
  --text: #1e293b;
}

[data-theme="dark"] {
  --bg: #020617;
  --text: #f8fafc;
}

body {
  background: var(--bg);
  color: var(--text);
}

The HTML could look like:

<html data-theme="dark">

  ...

</html>

System Theme vs Manual Theme

System Preference

Use prefers-color-scheme when the website should automatically follow the user's system preference.

@media (prefers-color-scheme: dark)

Manual Preference

Use a class or data attribute when the user should explicitly control the theme from inside the application.

html[data-theme="dark"]

Supporting System and Manual Modes Together

A polished application can support three conceptual states:

Light

Always use the light theme.

Dark

Always use the dark theme.

System

Follow the operating-system preference.

CSS can handle the actual theme styles, while application code can decide which theme state is active. This separation keeps presentation and preference management easier to maintain.

Dark Mode with Semantic Color Variables

Avoid naming variables after specific colors when they represent a role in the interface. Semantic names make the theme easier to change.

Less Flexible

--gray-900: #111827;

Better

--surface-primary: #111827;

A variable such as --surface-primary describes what the value means rather than permanently associating it with one specific color.

Dark Mode for Cards and Components

:root {
  --page-bg: #ffffff;
  --surface: #f8fafc;
  --text: #1e293b;
  --muted: #64748b;
  --border: #cbd5e1;
}

@media (prefers-color-scheme: dark) {

  :root {
    --page-bg: #020617;
    --surface: #0f172a;
    --text: #f8fafc;
    --muted: #94a3b8;
    --border: #334155;
  }

}

body {
  background: var(--page-bg);
  color: var(--text);
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
}

.card p {
  color: var(--muted);
}

Dark Mode for Form Controls

Forms are often overlooked during theme development. Inputs, textareas, selects, checkboxes, and buttons need colors that remain readable in both themes.

:root {
  color-scheme: light dark;

  --input-bg: #ffffff;
  --input-text: #1e293b;
  --input-border: #cbd5e1;
}

@media (prefers-color-scheme: dark) {

  :root {
    --input-bg: #1e293b;
    --input-text: #f8fafc;
    --input-border: #475569;
  }

}

input,
textarea,
select {
  background: var(--input-bg);
  color: var(--input-text);
  border: 1px solid var(--input-border);
}

Dark Mode and Focus States

Focus indicators must remain clearly visible in both light and dark themes. Do not choose a focus color that disappears against the dark background.

button:focus-visible,
input:focus-visible,
textarea:focus-visible {
  outline: 3px solid #60a5fa;
  outline-offset: 3px;
}

Dark Mode and Contrast

Dark mode does not remove the need for readable contrast. Dark gray text on a slightly lighter dark background can still be difficult to read. Likewise, pure white text on pure black may create a harsh visual result depending on the design.

Evaluate actual text, surfaces, borders, disabled controls, links, and focus indicators rather than checking only the primary background and text colors.

Dark Mode and Images

Images designed for a light interface may not fit visually into a dark theme. Logos, illustrations, icons, and decorative images may need theme-specific alternatives or styling.

.logo {
  max-width: 160px;
}

@media (prefers-color-scheme: dark) {
  .logo {
    filter: brightness(1.15);
  }
}

Filters can help in simple cases, but theme-specific assets are often more reliable when the artwork itself needs a fundamentally different treatment.

Dark Mode and Shadows

Shadows designed for light backgrounds may become too subtle or visually strange on dark surfaces. Dark themes often benefit from carefully chosen borders, elevation differences, and softer shadow treatments.

:root {
  --surface: #ffffff;
  --shadow: 0 10px 30px
    rgba(15, 23, 42, 0.12);
}

@media (prefers-color-scheme: dark) {

  :root {
    --surface: #0f172a;
    --shadow: 0 10px 30px
      rgba(0, 0, 0, 0.35);
  }

}

.card {
  background: var(--surface);
  box-shadow: var(--shadow);
}

Dark Mode and Transitions

A theme switch can be animated, but a full-page color transition should be used carefully. Users who prefer reduced motion should not be forced to experience unnecessary animation.

body {
  transition:
    background-color 0.2s ease,
    color 0.2s ease;
}

@media (prefers-reduced-motion: reduce) {

  body {
    transition: none;
  }

}

Preventing Theme Flash

Applications that store a user's theme preference may briefly display the wrong theme before JavaScript runs and applies the saved preference. A common strategy is to determine the theme as early as possible and apply the root theme state before the page becomes visible.

The exact implementation depends on the framework and rendering architecture. The CSS responsibility is to ensure that the root theme state immediately produces the correct visual variables.

Dark Mode with the HTML meta color-scheme

The HTML <meta name="color-scheme"> element can communicate supported color schemes at the document level. This can help user agents know the page's color-scheme expectations early in the loading process. ([developer.mozilla.org](https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/meta/name/color-scheme?utm_source=chatgpt.com))

<head>

  <meta
    name="color-scheme"
    content="light dark"
  >

  ...

</head>

This complements the CSS color-scheme property and your own theme styles. ([developer.mozilla.org](https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/meta/name/color-scheme?utm_source=chatgpt.com))

Multiple Themes with CSS Variables

The same architecture can support more than just light and dark themes. You can define different variable sets for themes such as light, dark, high-contrast, or brand-specific interfaces.

:root {
  --bg: #ffffff;
  --text: #1e293b;
  --accent: #2563eb;
}

[data-theme="dark"] {
  --bg: #020617;
  --text: #f8fafc;
  --accent: #60a5fa;
}

[data-theme="high-contrast"] {
  --bg: #000000;
  --text: #ffffff;
  --accent: #ffff00;
}

body {
  background: var(--bg);
  color: var(--text);
}

a {
  color: var(--accent);
}

Common Dark Mode Mistakes

Simply Inverting Colors

filter: invert() or blindly swapping every color rarely produces a polished theme.

Forgetting Form Controls

Inputs, selects, checkboxes, and buttons may still look like light-theme components if they are not included in the theme system.

Poor Contrast

Muted colors that look attractive on a mockup can become unreadable in the actual dark interface.

Missing Focus Styles

Focus indicators that worked in light mode may disappear against dark backgrounds.

Hardcoding Colors Everywhere

Repeating raw colors across components makes theme maintenance much harder.

Ignoring User Preference

When supporting automatic themes, respect the user's selected color-scheme preference unless the application has a deliberate manual override.

Dark Mode Best Practices

  • Use semantic CSS variables such as --page-bg, --surface, --text, and --border.
  • Use prefers-color-scheme when you want to respond to system preferences.
  • Use color-scheme: light dark when the document supports both schemes.
  • Use light-dark() when direct light/dark value selection makes the CSS clearer and browser support requirements allow it.
  • Design focus, hover, disabled, validation, and active states for both themes.
  • Test inputs, selects, scrollbars, and other browser-provided UI in dark mode.
  • Use a manual theme class or data attribute when users need an application-level theme switch.
  • Do not rely on color alone to communicate state.
  • Respect reduced-motion preferences when animating theme changes.
  • Test the theme with real content, images, forms, and components rather than only a single demo card.

Complete Automatic Dark Mode Example

:root {
  color-scheme: light dark;

  --bg: #ffffff;
  --surface: #f8fafc;
  --text: #1e293b;
  --muted: #64748b;
  --border: #cbd5e1;
  --primary: #ea580c;
}

@media (prefers-color-scheme: dark) {

  :root {
    --bg: #020617;
    --surface: #0f172a;
    --text: #f8fafc;
    --muted: #94a3b8;
    --border: #334155;
    --primary: #fb923c;
  }

}

body {
  margin: 0;

  background-color: var(--bg);
  color: var(--text);

  font-family:
    Arial,
    sans-serif;
}

.card {
  width: min(
    calc(100% - 2rem),
    500px
  );

  margin: 3rem auto;
  padding: 2rem;

  background-color: var(--surface);

  border:
    1px solid var(--border);

  border-radius: 12px;
}

.card p {
  color: var(--muted);
}

.button {
  display: inline-flex;
  align-items: center;
  justify-content: center;

  min-height: 44px;
  padding: 0.75rem 1rem;

  border: 0;
  border-radius: 8px;

  background-color: var(--primary);
  color: white;

  font: inherit;
  font-weight: 600;
}

.button:focus-visible {
  outline: 3px solid #60a5fa;
  outline-offset: 3px;
}

input {
  width: 100%;
  padding: 0.75rem 1rem;

  box-sizing: border-box;

  background-color: var(--surface);
  color: var(--text);

  border:
    1px solid var(--border);

  border-radius: 8px;

  font: inherit;
}

Key Takeaways

CSS dark mode provides an alternative visual theme with darker surfaces and appropriately adjusted foreground colors.

CSS custom properties are one of the most maintainable ways to organize theme colors.

prefers-color-scheme lets CSS respond to the user's operating-system or browser color-scheme preference. ([developer.mozilla.org](https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/At-rules/%40media/prefers-color-scheme?utm_source=chatgpt.com))

color-scheme communicates supported color schemes to the browser and can affect browser-provided UI such as controls and scrollbars. ([developer.mozilla.org](https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Properties/color-scheme?utm_source=chatgpt.com))

light-dark() provides a direct CSS mechanism for choosing between light and dark color values based on the active color scheme. ([developer.mozilla.org](https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Values/color_value/light-dark?utm_source=chatgpt.com))

Manual theme switching can be implemented using a root class such as .dark or an attribute such as data-theme="dark".

A good dark theme must include forms, focus states, links, validation states, borders, images, shadows, and other components rather than changing only the page background.

Accessible dark mode requires readable contrast, visible focus indicators, usable layouts, and careful handling of motion and interactive states.

Code Examples

css
/* Automatic system dark mode */

:root {
  --bg: #ffffff;
  --text: #1e293b;
  --surface: #f8fafc;
}

@media (prefers-color-scheme: dark) {

  :root {
    --bg: #020617;
    --text: #f8fafc;
    --surface: #0f172a;
  }

}

body {
  background: var(--bg);
  color: var(--text);
}

.card {
  background: var(--surface);
}
    
css
/* Manual theme switching */

:root {
  --bg: #ffffff;
  --text: #1e293b;
}

html.dark {
  --bg: #020617;
  --text: #f8fafc;
}

body {
  background-color: var(--bg);
  color: var(--text);
}
    
html
<!DOCTYPE html>
<html>
<head>

  <meta
    name="color-scheme"
    content="light dark"
  >

  <style>

    :root {
      color-scheme: light dark;

      --bg: #ffffff;
      --surface: #f8fafc;
      --text: #1e293b;
      --muted: #64748b;
      --border: #cbd5e1;
      --primary: #ea580c;
    }

    @media (prefers-color-scheme: dark) {

      :root {
        --bg: #020617;
        --surface: #0f172a;
        --text: #f8fafc;
        --muted: #94a3b8;
        --border: #334155;
        --primary: #fb923c;
      }

    }

    body {
      margin: 0;
      padding: 2rem;

      font-family: Arial, sans-serif;

      background: var(--bg);
      color: var(--text);
    }

    .card {
      width: min(
        100%,
        500px
      );

      margin-inline: auto;
      padding: 2rem;

      background: var(--surface);

      border:
        1px solid var(--border);

      border-radius: 12px;
    }

    .card p {
      color: var(--muted);
    }

    button {
      min-height: 44px;
      padding: 0.75rem 1rem;

      border: 0;
      border-radius: 8px;

      background: var(--primary);
      color: white;

      font: inherit;
      font-weight: 600;
    }

    button:focus-visible {
      outline: 3px solid #60a5fa;
      outline-offset: 3px;
    }

  </style>

</head>

<body>

  <article class="card">

    <h1>
      CSS Dark Mode
    </h1>

    <p>
      This page automatically follows
      the user's preferred color scheme.
    </p>

    <button>
      Continue
    </button>

  </article>

</body>
</html>
    
css
/* light-dark() */

:root {
  color-scheme: light dark;
}

body {
  background-color: light-dark(
    #ffffff,
    #020617
  );

  color: light-dark(
    #1e293b,
    #f8fafc
  );
}

.card {
  background-color: light-dark(
    #f8fafc,
    #0f172a
  );

  border-color: light-dark(
    #cbd5e1,
    #334155
  );
}

/*
First value -> light
Second value -> dark
*/
    
css
/* Dark mode design tokens */

:root {
  --page-bg: #ffffff;
  --surface-primary: #ffffff;
  --surface-secondary: #f8fafc;

  --text-primary: #0f172a;
  --text-secondary: #64748b;

  --border: #cbd5e1;

  --accent: #ea580c;
  --accent-hover: #c2410c;
}

[data-theme="dark"] {
  --page-bg: #020617;
  --surface-primary: #0f172a;
  --surface-secondary: #1e293b;

  --text-primary: #f8fafc;
  --text-secondary: #94a3b8;

  --border: #334155;

  --accent: #fb923c;
  --accent-hover: #fdba74;
}

body {
  background: var(--page-bg);
  color: var(--text-primary);
}

.card {
  background: var(--surface-primary);
  border: 1px solid var(--border);
}

.muted {
  color: var(--text-secondary);
}

.button {
  background: var(--accent);
}

.button:hover {
  background: var(--accent-hover);
}
    

Pro Tip

Build dark mode around semantic CSS variables rather than scattered color overrides. Use names such as --page-bg, --surface, --text, --muted, --border, and --primary so the same component CSS works across themes. Use prefers-color-scheme for automatic system-based themes, a root class or data attribute for manual theme control, and color-scheme or light-dark() when they simplify the implementation.

More CSS lessons

🎨 IgKnight Docs

Master HTML, CSS, JS, Python, .NET and more with easy-to-follow lessons and practical code examples.

© 2025 IgKnight Learning Platform. All rights reserved.