How to Enable Dark Mode in Tailwind CSS ?
Last Updated :
09 Oct, 2024
Tailwind CSS offers built-in features for enabling dark mode, allowing developers to create a dark-themed version of their web pages. By configuring the dark mode setting in the Tailwind configuration file and using the dark class, specific elements can seamlessly switch to dark-themed styles.
There are mainly two approaches for enabling dark mode in Tailwind CSS :
Approach 1: Using Dark Class Configuration
The Dark Class Configuration approach in Tailwind CSS enables dark mode by adding the darkMode: 'class' setting in the configuration file. This allows applying dark-specific styles using the `dark:` prefix, toggling themes by adding the dark class to elements.
Syntax
<script>
tailwind.config = {
darkMode: 'class',
}
</script>
Example 1: In this example we are using Tailwind CSS to implement dark mode. The page content features a title and paragraph, with the background and text colors dynamically adjusting between light and dark themes.
HTML
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://cdn.tailwindcss.com">
</script>
<script src=
"https://cdn.tailwindcss.com?plugins=forms,typography,aspect-ratio,line-clamp">
</script>
</head>
<script>
tailwind.config = {
darkMode: 'class',
}
</script>
<style>
body {
color: #fff;
height: 800px;
}
.bg-white {
height: 100%;
}
</style>
<body class="dark">
<div class="bg-white dark:bg-gray-900">
<h1 class="text-gray-900 dark:text-white">
GeeksforGeeks</h1>
<p class="text-gray-600 dark:text-gray-300">
GeeksforGeeks is Computer Science Education Portal.
</p>
<p>GeeksforGeeks</p>
</div>
</body>
</html>
Output:
How to enable dark mode in Tailwind CSS?Approach 2: Using JavaScript Toggle Functionality
The JavaScript Toggle Functionality approach enables dark mode by toggling a custom class (e.g., .dark-mode) on elements. JavaScript functions dynamically add or remove this class, allowing users to switch between light and dark themes with a button click.
Syntax
onclick="document.body.classList.toggle('dark-mode')"
Example : In this example we example toggles dark mode using a custom .dark-mode class defined in the <style> section. Clicking the "Switch theme" button toggles the dark background and text color on the <body> element.
HTML
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width,
initial-scale=1.0">
<script src="https://cdn.tailwindcss.com">
</script>
<script src=
"https://cdn.tailwindcss.com?plugins=forms,typography,aspect-ratio,line-clamp">
</script>
</head>
<style>
.dark-mode {
background-color: rgb(12, 12, 23);
color: white;
}
</style>
<body class="dark-mode">
<div class="justify-center grid items-center h-screen">
<div class="bg-dark-mode-white">
<p class="text-dark-mode-black">GeeksForGeeks</p>
<button class="border p-2"
onclick="document.body.classList.toggle('dark-mode')">
Switch theme
</button>
</div>
</div>
</body>
</html>
Output:
enable dark mode in Tailwind CSS Using JavaScript Toggle Functionality
Similar Reads
How to Add Dark Mode in ReactJS using Tailwind CSS ? Tailwind CSS is a CSS framework that helps in building rapid custom UI. It is a highly customizable, low-level CSS framework that gives you all of the building blocks that you need. Tailwind CSS creates small utilities with a defined set of options enabling easy integration of existing classes direc
3 min read
How to Disable Tailwind on a Div or Component? Sometimes, you may want to turn off Tailwind's styling on a specific div or component. This article will explore how to do just that, along with some effective methods to achieve it. Tailwind applies CSS classes to style elements based on their configuration.Below are the following approaches to dis
3 min read
How to set Transition duration in Tailwind CSS ? In this article, we will see how to set transition duration in Tailwind CSS. Tailwind CSS is a highly customizable, utility-first CSS framework from which we can use utility classes to build any design. Transition duration class accepts lots of value in tailwind CSS in which all the properties are c
2 min read
How to Change the Direction of a Gradient in Tailwind CSS ? In Tailwind CSS, you can change the direction of a gradient using the bg-gradient-to utility class. For example, bg-gradient-to-r sets the gradient from left to right, while bg-gradient-to-b sets it from top to bottom. Choose the appropriate direction class to achieve the desired gradient orientatio
3 min read
How to Add New Colors in Tailwind CSS ? Tailwind CSS is a popular utility-first CSS framework that offers a wide range of pre-designed styles and classes to simplify the process of styling web applications. However, the default set of colors provided by Tailwind may not always meet the requirements of your project. In such cases, you may
4 min read