Kinda Code
Home/HTML & CSS/Tailwind CSS: Light/Dark theme based on system settings

Tailwind CSS: Light/Dark theme based on system settings

Last updated: June 05, 2022

In Tailwind CSS, you can give an element both light and dark color schemes. Which scheme will be used depends on the settings of the user’s system or browser. To target light mode, just use standard utility classes. To target dark mode, add the dark modifier to utility classes. You can use the dark modifier with other modifiers, like so:

<a href="#" class="text-blue-500 hover:text-blue-700 
   dark:text-amber-500 dark:hover:text-red-700>
   KindaCode.com
</a>

Example

Preview:

The code:

<body class="w-screen h-screen bg-white dark:bg-gray-900 p-20 space-y-5">
   <h1 class="text-4xl text-black dark:text-white">Welcome to KindaCode.com</h1>
   <button class="px-10 py-3 bg-blue-500 text-white rounded-md hover:bg-blue-700
   dark:bg-amber-500 dark:text-dark dark:hover:bg-red-500
   ease-in duration-300">
       I am a button
   </button>
</body>

Note: The way to switch between dark and light mode depends on your computer and operating system.

Further reading:

You can also check out our CSS category page for the latest tutorials and examples.