Menu
Coddy logo textTech

Dark/Light Mode Basics

Part of the Practical Frontend section of Coddy's HTML journey — lesson 13 of 35.

Dark/light mode is a popular feature that allows users to choose their preferred color scheme. Let's implement a basic dark/light mode toggle using CSS variables.

First, define the light mode color variables in the root element and apply them for different elements:

:root {
  --background-color: #ffffff;
  --text-color: #333333;
  --heading-color: #000000;
}
body {
  background-color: var(--background-color);
  color: var(--text-color);
}
h1, h2, h3 {
  color: var(--heading-color);
}

Next, create a dark mode class that will override these variables:

.dark-mode {
  --background-color: #333333;
  --text-color: #f5f5f5;
  --heading-color: #ffffff;
}

Now, the .dark-mode class controls the theme — when this class is added to <body>, the variables change and all the elements using them update automatically.

<body class="dark-mode">
<!-- your page content here -->
</body>

To switch modes dynamically, we need a bit of help from another language called JavaScript (JS) to add or remove the .dark-mode class. In this lesson, you can test it manually by adding or removing the class on the <body> tag.

challenge icon

Challenge

Easy

Your task:

  1. Add a .dark-mode class inside the <style> section.
  2. Override CSS variables to set dark colors for the background, text, headings, and buttons.
  3. Test it by adding class="dark-mode" to the <body> tag.

Cheat sheet

Use CSS variables to implement dark/light mode themes. Define light mode variables in :root:

:root {
  --background-color: #ffffff;
  --text-color: #333333;
  --heading-color: #000000;
}

Apply variables to elements:

body {
  background-color: var(--background-color);
  color: var(--text-color);
}
h1, h2, h3 {
  color: var(--heading-color);
}

Create a dark mode class that overrides the variables:

.dark-mode {
  --background-color: #333333;
  --text-color: #f5f5f5;
  --heading-color: #ffffff;
}

Toggle themes by adding/removing the .dark-mode class to the <body> element:

<body class="dark-mode">

Try it yourself

<!DOCTYPE html>
<html>
<head>
  <title>Dark/Light Mode Basics</title>
  <style>
    /* Default light theme */
    :root {
      --background-color: #f0f4f8;
      --text-color: #222222;
      --heading-color: #003366;
      --button-bg: #ffcc00;
      --button-text: #000000;
    }

    body {
      background-color: var(--background-color);
      color: var(--text-color);
      font-family: Arial, sans-serif;
      margin: 0;
      padding: 0;
    }

    header {
      background-color: var(--heading-color);
      color: white;
      padding: 2rem;
      text-align: center;
    }

    header h1 {
      margin: 0;
      font-size: 2rem;
    }

    main {
      padding: 2rem;
    }

    h2 {
      color: var(--heading-color);
      margin-top: 1.5rem;
    }

    p {
      line-height: 1.6;
    }

    .button {
      display: inline-block;
      margin-top: 1rem;
      padding: 0.75rem 1.5rem;
      background-color: var(--button-bg);
      color: var(--button-text);
      border: none;
      border-radius: 5px;
      text-decoration: none;
      font-weight: bold;
      cursor: pointer;
    }

    footer {
      background-color: var(--heading-color);
      color: white;
      text-align: center;
      padding: 1rem;
      margin-top: 2rem;
    }
  </style>
</head>
<body>
  <header>
    <h1>Olympic Games 2025</h1>
    <p>Celebrating sports, unity, and excellence</p>
  </header>

  <main>
    <h2>Top Sports</h2>
    <p>From track and field to swimming and gymnastics, the Olympics showcase the best athletes in the world.</p>
    <a href="#" class="button">See Medal Winners</a>

    <h2>Host Cities</h2>
    <p>Explore the exciting host cities where athletes come together to compete at the highest level.</p>
    <a href="#" class="button">Learn More</a>

    <h2>History</h2>
    <p>The Olympic Games have a rich history dating back to ancient Greece, promoting peace, friendship, and fair competition.</p>
  </main>

  <footer>
    &copy; 2025 Olympic Games. All rights reserved.
  </footer>
</body>
</html>
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Practical Frontend