The dark mode toggle works by adding or removing the dark class on the root element (usually <html> or <body>). Tailwind CSS uses this class to apply dark mode styles automatically.
const btn = document.getElementById('toggle-btn'); btn.addEventListener('click', () => { // What goes here? });
The document.documentElement refers to the <html> element. Using classList.toggle('dark') adds the class if missing or removes it if present, toggling dark mode.
<body class="bg-white dark:bg-gray-900 text-black dark:text-white"> <h1>Hello</h1> </body>
What will the user see after toggling dark mode on?
The dark:bg-gray-900 changes the background to dark gray and dark:text-white changes the text color to white when the dark class is present on a parent element.
Tailwind applies dark mode styles using the .dark class on a parent element. The selector .dark means styles apply only when the dark class is present.
Adding aria-pressed tells screen readers if the toggle is on or off. A clear label helps all users understand the button's purpose. Avoid relying on color alone and keep the button keyboard accessible.