MSK Institute LogoMSK.Institute
HomeCourses
Book Free Demo➔Sign In
Free Demo➔
MSK Institute LogoMSK.Institute

Leading computer training academy in Shikohabad for software engineering, Python, web development, and digital literacy.

ISO 9001:2015 Certified Center
Follow & Connect
4.9

Programs & Admissions

  • All CoursesCatalog
  • Learning PathsRoadmaps
  • Student ProjectsShowcase
  • Live Classes & ScheduleLive
  • Upcoming Batches
  • Campuses & LocationsHub
  • Verify Certificates
  • Book Free Demo Class

Resources & Tools

  • Free Online Mock TestsAll Tests
  • NIELIT CCC Mock TestFree
  • Course & Career FinderQuiz
  • Study Material Hub
  • All MSK ToolsHub
  • Keyboard ShortcutsNew
  • TypeQuest Speed LabTyping
  • MSK Code PlaygroundCode
  • Interactive Tutorials
  • Quick Cheatsheets

Institute & Contact

  • About MSK Institute
  • Careers & InternshipsHiring
  • Publications & Blogs
  • Gali No. 3, Near Gyan Jyoti Public School,
    Adarsh Nagar, Shikohabad, Uttar Pradesh 283135
  • +91 83930 42166
  • mskshikohabad@gmail.com
  • Hours:Mon – Sat: 8:00 AM – 7:00 PM | Sun: 10:00 AM – 2:00 PM
  • 4.9 ★(38 Google Reviews)
© 2026 MSK Institute. All rights reserved.•Privacy Policy•Terms of Service•Disclaimer•Contact Us

Shikohabad's Leading Computer & Coding Academy

Home
Courses
Live
Material
Class-Based Dark Mode Setup with System Preference Support0%
Study Materials
Tailwind CSSProgress 0%
1.1Why Tailwind CSS? Utility-First vs Traditional Semantic CSS1.2Installation via PostCSS, Vite & Next.js Framework Integration1.3Tailwind Core Spacing, Sizing, Padding & Margin Scales1.4Typography: Font Families, Sizes, Weights, Leading & Tracking
3.1State Variants: Hover, Focus, Active, Disabled & Group-Hover3.2Class-Based Dark Mode Setup with System Preference Support3.3Transitions, Smooth Transforms, Scale & Custom Keyframe Animations3.4Component Extraction, @apply Directives & Tailwind Merge (clsx)3.5Tailwind Config Customization: Colors, Fonts & Official Plugins
HomeTutorialsTailwind CSSClass-Based Dark Mode Setup with System Preference Support
Transitions, Smooth Transforms, Scale & Custom Keyframe Animations

Class-Based Dark Mode Setup with System Preference Support

Beginner12 min read•Updated: 2026-11-18•By Er. Sumit Kumar (MSK Institute)
Study MaterialsCheatsheet

Modern digital products demand high-contrast Dark Mode support for reduced eye strain and enhanced battery life. Tailwind CSS provides native support for dark themes using either the media strategy (automatic OS detection) or the class strategy (manual user toggle stored in localStorage).


1. Configuring Dark Mode in tailwind.config.js

For manual user toggles, configure the selector (or class) strategy:

1/** @type {import('tailwindcss').Config} */
2module.exports = {
3 darkMode: 'class', // Enables class-based dark mode (targets .dark on <html>)
4 content: ["./src/**/*.{js,ts,jsx,tsx}"],
5 theme: {
6 extend: {},
7 },
8 plugins: [],
9}

2. Using the dark: Modifier

Prefix any utility class with dark: to define its appearance when dark mode is active:

1<!-- Card with Dark Mode Variants -->
2<div class="bg-white dark:bg-slate-900 border border-slate-200 dark:border-slate-800 p-6 rounded-2xl shadow-sm">
3 <h2 class="text-slate-900 dark:text-white font-bold text-xl">
4 Full-Stack Web Development
5 </h2>
6 <p class="text-slate-600 dark:text-slate-400 mt-2 text-sm leading-relaxed">
7 Learn Node.js, Express, MongoDB, and Next.js in our Shikohabad coding lab.
8 </p>
9 <button class="mt-4 px-4 py-2 bg-blue-600 dark:bg-blue-500 text-white font-semibold rounded-lg hover:bg-blue-700 dark:hover:bg-blue-600">
10 View Syllabus
11 </button>
12</div>

3. Implementing the Theme Toggle in React / Next.js

1import { useEffect, useState } from 'react';
2 
3export default function ThemeToggle() {
4 const [darkMode, setDarkMode] = useState(false);
5 
6 useEffect(() => {
7 // Check initial preference from localStorage or OS
8 const isDark = localStorage.theme === 'dark' ||
9 (!('theme' in localStorage) && window.matchMedia('(prefers-color-scheme: dark)').matches);
10
11 setDarkMode(isDark);
12 document.documentElement.classList.toggle('dark', isDark);
13 }, []);
14 
15 const toggleTheme = () => {
16 const nextMode = !darkMode;
17 setDarkMode(nextMode);
18 document.documentElement.classList.toggle('dark', nextMode);
19 localStorage.theme = nextMode ? 'dark' : 'light';
20 };
21 
22 return (
23 <button onClick={toggleTheme} class="p-2 rounded-lg bg-slate-100 dark:bg-slate-800 text-slate-800 dark:text-slate-200">
24 {darkMode ? '🌙 Dark' : '☀️ Light'}
25 </button>
26 );
27}

Interactive Quiz• Knowledge Check

Multiple Choice Questions

Question 1
When darkMode: 'class' is configured in tailwind.config.js, how does Tailwind activate dark styles?
Question 2
What happens if darkMode: 'media' is selected instead of 'class'?
Question 3
Which class combination creates a card with a white background in light mode and dark slate background in dark mode?
Question 4
Why should theme preferences be stored in localStorage?
Question 5
How does Tailwind prevent flash of incorrect theme (FOUC) on initial page load?

Next Lesson

Transitions, Smooth Transforms, Scale & Custom Keyframe Animations

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Related Lessons

Previous LessonNext Lesson
State Variants: Hover, Focus, Active, Disabled & Group-HoverTransitions, Smooth Transforms, Scale & Custom Keyframe Animations

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

Mentor-Led Practical Training

Master Tailwind CSS with Hands-On Projects

Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.

View Full CourseBook Free Demo
PreviousNext
PreviousPrevNext