- Published on
Best Ways To Make A Tailwind CSS dark mode switcher With Tailwind CSS

- What is Tailwind CSS?
- The description of Tailwind CSS dark mode switcher ui component
- Why use Tailwind CSS to make a Tailwind CSS dark mode switcher ui component?
- The preview of Tailwind CSS dark mode switcher ui component
- The source code of Tailwind CSS dark mode switcher ui component
- How to make a Tailwind CSS dark mode switcher with Tailwind CSS?
- Install tailwind css of verion 2.2.4
- All the unility class needed to make a Tailwind CSS dark mode switcher component
- 16 steps to make a Tailwind CSS dark mode switcher component with Tailwind CSS
- Conclusion
What is Tailwind CSS?
Tailwind CSS is a utility-first CSS framework that focuses on creating personalized user interfaces quickly. It can gives you all the building blocks you are able to create personalized designs without having to fight to override irritating opinionated styles. Also, Tailwind CSS is a highly configurable, low-level CSS framework.
The description of Tailwind CSS dark mode switcher ui component
Use this dark mode switcher using tailwind css and flowbite based on the localstorage method flowbite.com/docs/customize/dark-mode/
Why use Tailwind CSS to make a Tailwind CSS dark mode switcher ui component?
- It can make the building process of Tailwind CSS dark mode switcher ui component faster and more easily.
- Enables building complex responsive layouts and components freely.
- Minimum lines of CSS code in Tailwind CSS dark mode switcher component file.
The preview of Tailwind CSS dark mode switcher ui component
Free download of the Tailwind CSS dark mode switcher's source code
The source code of Tailwind CSS dark mode switcher ui component
<script>
// On page load or when changing themes, best to add inline in `head` to avoid FOUC
if (localStorage.getItem('color-theme') === 'dark' || (!('color-theme' in localStorage) && window.matchMedia('(prefers-color-scheme: dark)').matches)) {
document.documentElement.classList.add('dark');
} else {
document.documentElement.classList.remove('dark')
}
</script>
<!-- This is an example component -->
<div class="max-w-lg mx-auto dark:bg-gray-800 p-12 rounded">
<link rel="stylesheet" href="https://unpkg.com/@themesberg/[email protected]/dist/flowbite.min.css" />
<button id="theme-toggle" type="button" class="text-gray-500 dark:text-gray-400 hover:bg-gray-100 dark:hover:bg-gray-700 focus:outline-none focus:ring-4 focus:ring-gray-200 dark:focus:ring-gray-700 rounded-lg text-sm p-2.5">
<svg id="theme-toggle-dark-icon" class="w-5 h-5 hidden" fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path d="M17.293 13.293A8 8 0 016.707 2.707a8.001 8.001 0 1010.586 10.586z"></path></svg>
<svg id="theme-toggle-light-icon" class="w-5 h-5 hidden" fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path d="M10 2a1 1 0 011 1v1a1 1 0 11-2 0V3a1 1 0 011-1zm4 8a4 4 0 11-8 0 4 4 0 018 0zm-.464 4.95l.707.707a1 1 0 001.414-1.414l-.707-.707a1 1 0 00-1.414 1.414zm2.12-10.607a1 1 0 010 1.414l-.706.707a1 1 0 11-1.414-1.414l.707-.707a1 1 0 011.414 0zM17 11a1 1 0 100-2h-1a1 1 0 100 2h1zm-7 4a1 1 0 011 1v1a1 1 0 11-2 0v-1a1 1 0 011-1zM5.05 6.464A1 1 0 106.465 5.05l-.708-.707a1 1 0 00-1.414 1.414l.707.707zm1.414 8.486l-.707.707a1 1 0 01-1.414-1.414l.707-.707a1 1 0 011.414 1.414zM4 11a1 1 0 100-2H3a1 1 0 000 2h1z" fill-rule="evenodd" clip-rule="evenodd"></path></svg>
</button>
<p class="mt-5 dark:text-white">This dark mode switcher is part of a larger, open-source library of Tailwind CSS components. Learn more by going to the official <a class="text-blue-600 dark:text-blue-400 hover:underline" href="https://flowbite.com/docs/getting-started/introduction/" target="_blank">Flowbite Documentation</a>.</p>
</div>
<script>
var themeToggleDarkIcon = document.getElementById('theme-toggle-dark-icon');
var themeToggleLightIcon = document.getElementById('theme-toggle-light-icon');
// Change the icons inside the button based on previous settings
if (localStorage.getItem('color-theme') === 'dark' || (!('color-theme' in localStorage) && window.matchMedia('(prefers-color-scheme: dark)').matches)) {
themeToggleLightIcon.classList.remove('hidden');
} else {
themeToggleDarkIcon.classList.remove('hidden');
}
var themeToggleBtn = document.getElementById('theme-toggle');
themeToggleBtn.addEventListener('click', function() {
// toggle icons inside button
themeToggleDarkIcon.classList.toggle('hidden');
themeToggleLightIcon.classList.toggle('hidden');
// if set via local storage previously
if (localStorage.getItem('color-theme')) {
if (localStorage.getItem('color-theme') === 'light') {
document.documentElement.classList.add('dark');
localStorage.setItem('color-theme', 'dark');
} else {
document.documentElement.classList.remove('dark');
localStorage.setItem('color-theme', 'light');
}
// if NOT set via local storage previously
} else {
if (document.documentElement.classList.contains('dark')) {
document.documentElement.classList.remove('dark');
localStorage.setItem('color-theme', 'light');
} else {
document.documentElement.classList.add('dark');
localStorage.setItem('color-theme', 'dark');
}
}
});
</script>
<script src="https://unpkg.com/@themesberg/[email protected]/dist/flowbite.bundle.js"></script>
How to make a Tailwind CSS dark mode switcher with Tailwind CSS?
Install tailwind css of verion 2.2.4
Use the script
html tag to import the script of Tailwind CSS of the version 2.2.4
<script src="https://cdn.tailwindcss.com"></script>
All the unility class needed to make a Tailwind CSS dark mode switcher component
max-w-lg
mx-auto
dark:bg-gray-800
p-12
text-gray-500
dark:text-gray-400
hover:bg-gray-100
text-sm
p-2.5
w-5
h-5
hidden
mt-5
dark:text-white
text-blue-600
dark:text-blue-400
16 steps to make a Tailwind CSS dark mode switcher component with Tailwind CSS
Set the maximum width/height of an element using the
max-w-lg
utilities.Control the horizontal margin of an element to auto using the
mx-auto
utilities.Control the background color of an element to gray-800 using the
dark:bg-gray-800
utilities in dark theme.Control the padding on all sides of an element to 3rem using the
p-12
utilities.Control the text color of an element to gray-500 using the
text-gray-500
utilities.Control the text color of an element to gray-400 in dark theme using the
dark:text-gray-400
utilities.Control the background color of an element to gray-100 using the
hover:bg-gray-100
utilities on hover.Control the text color of an element to sm using the
text-sm
utilities.Control the padding on all sides of an element to 2.5 using the
p-2.5
utilities.Use
w-5
to set an element to a fixed width(1.25rem).Use
h-5
to set an element to a fixed height(1.25rem).Use
hidden
to set an element to display: none and remove it from the page layout.Control the margin on top side of an element to 1.25rem using the
mt-5
utilities.Control the text color of an element to white in dark theme using the
dark:text-white
utilities.Control the text color of an element to blue-600 using the
text-blue-600
utilities.Control the text color of an element to blue-400 in dark theme using the
dark:text-blue-400
utilities.
Conclusion
The above is a step-by-step tutorial on how to use Tailwind CSS to make a Tailwind CSS dark mode switcher components, learn and follow along to implement your own components.