Teyro
CoursesFor creatorsBlog
Log inGet started
Tey Mascot
LOADING.

Every expert you admire was once a beginner who refused to quit.

Teyro

The fun way to finish learning coding and AI: short daily lessons with streaks, leagues and friends.

Get the app

Learn

  • All courses
  • Learn coding
  • Learn AI
  • Learn online
  • How it works
  • Leagues
  • Get the app

Creators

  • Teach on Teyro
  • How teaching works
  • Teach coding online
  • Teach AI online
  • Join as a Founding Creator
  • Creator log in

Resources

  • Blog
  • Features
  • Use cases
  • Compare
  • FAQ

Legal

  • Terms of Use
  • Privacy Policy

© 2026 Teyro · support@teyro.app

HomeBlogSkill Building & Careers
Skill Building & Careers

How to Add a Dark Mode Toggle With CSS and JavaScript

Add a dark mode toggle that respects the system setting and remembers the choice. Uses CSS variables, a data attribute and localStorage.

The Teyro TeamOct 26, 20261 min read
On this page7 sections
  1. Direct answer
  2. CSS
  3. JavaScript
  4. Tips
  5. Where Teyro fits
  6. The bottom line
  7. More guides

A small feature that people notice and appreciate.

Direct answer

Define your colours as CSS variables, override them for dark mode, toggle a data-theme attribute on <html> with JavaScript and save the user's choice in localStorage. Use prefers-color-scheme as the default.

CSS

:root {
  --bg: #ffffff;
  --text: #111111;
}
:root[data-theme="dark"] {
  --bg: #111111;
  --text: #f5f5f5;
}
body {
  background: var(--bg);
  color: var(--text);
}

JavaScript

const root = document.documentElement;
const saved = localStorage.getItem("theme");
const prefersDark = window.matchMedia("(prefers-color-scheme: dark)").matches;

root.dataset.theme = saved || (prefersDark ? "dark" : "light");

document.querySelector("#toggle").addEventListener("click", () => {
  const next = root.dataset.theme === "dark" ? "light" : "dark";
  root.dataset.theme = next;
  localStorage.setItem("theme", next);
});

Tips

  • Check contrast in both themes.
  • Avoid a flash by setting the theme early in the page head.

Where Teyro fits

Teyro's Interactive Web Pages with JavaScript course turns this into short lessons you can play in a few minutes a day. It has 37 lessons, and you can start free.

The bottom line

Variables plus a data attribute plus localStorage makes a neat, small feature.

More guides

  • JavaScript Animation Basics
  • Build a To-Do List App in JavaScript
  • Debounce in JavaScript
  • Javascript localstorage
  • Interactive Web Pages with JavaScript course

Frequently asked questions

How do I add dark mode with JavaScript?

Define colours as CSS variables, switch them with a data attribute on the html element and save the choice in localStorage.

How do I detect the user's system theme?

Use the prefers-color-scheme media query or window.matchMedia.

Do I need JavaScript for dark mode?

Not for system-based dark mode, but you need it for a manual toggle.

Learn it by playing

Interactive Web Pages with JavaScript turns this into short, playable lessons. Free to start.

See the Interactive Web Pages with JavaScript course

Free to start · No credit card needed

Share

Written by

The Teyro Team

Learning Team at Teyro

We build Teyro — an AI-powered learning platform that turns skill-building into a daily habit through bite-sized, gamified lessons. We write about the learning science and study strategies that shape our product.

More from The Teyro Team

On this page

  • Direct answer
  • CSS
  • JavaScript
  • Tips
  • Where Teyro fits
  • The bottom line
  • More guides

Keep reading

Skill Building & Careers

Best Careers for 2027: High-Demand Jobs and the Skills to Get Them

The best careers for 2027 and the jobs most in demand, based on BLS and World Economic Forum data, plus the skills each path needs.

Oct 30, 20263 min read
Skill Building & Careers

Best Certifications for 2027: Tech, Data, Cloud and Business

The best certifications for 2027 in cloud, security, data, project management and AI, who each one suits, and when a portfolio matters more.

Oct 30, 20263 min read
Skill Building & Careers

Best Programming Language to Learn in 2027 (for Your Goal)

The best programming language to learn in 2027 depends on what you want to build. Compare Python, JavaScript, TypeScript, SQL and C.

Oct 30, 20263 min read