Dark Mode is a display setting built for user interfaces. It lets users change the theme color of the application to black or a color closer to black.
Recently, most developers have been including this feature in their applications. The applications contain a toggle switch that changes the color to dark or light when clicked.
The recent version of Tailwind css comes with a feature that enables users to add dark mode to their webpages.
In this tutorial, we will use the feature to add a dark theme to our page.
To follow through this article, you need to have a clear understanding of HTML, CSS, Tailwind CSS, and Javascript.
Let’s get started!
I will assume that you have the latest version of Tailwind installed. If not, I suggest you go through this article first.
We will need some content on our webpage so that we can see the cool dark mode effect.
This is the webpage we will use:
Blog page
This is the code for the webpage:
<div class="flex flex-row items-center justify-center">
<div class="max-w-md p-8 mx-auto rounded-lg">
<h2 class="items-start text-3xl font-bold text-black ">
Tailwind Dark Mode
</h2>
<p class="font-semibold text-gray-800">
We can switch from light to dark mode!
</p>
<p class="font-light">
Lorem ipsum dolor sit amet consectetur adipisicing elit. Saepe suscipit
reprehenderit deleniti perferendis debitis ullam quos praesentium, esse
totam adipisci illum illo.
</p>
<button
class="flex items-center w-24 px-2 py-2 mt-4 text-center text-white bg-blue-900 rounded-lg "
>
Read More
</button>
<!--IMAGE HERE-->
<div class="mb-5 w-96 h-46">
<img class="mt-2 rounded" src="./images/image1.jpg" />
</div>
</div>
</div>
Now that we have a page to practice on, let’s move to the next step.
Before you begin, ensure you have a clear idea of how you want your switch to look like and where you want it to be.
Some web pages have the dark mode feature in the “Settings” tab of the navigation bar. Others include this feature on the top-right corner of the page.
For our case, we will place it on the right corner because it is much simpler.
Let’s get to it!
We will use the input element for the switch.
Our first task is to ensure that the switch is at the top-right corner of the webpage.
We do this by creating a div container with the following classes:
<body>
<div
class="absolute right-0 flex items-center justify-center mx-auto top-5 left-1/3"
></div>
</body>
flex will align the contents of the switch vertically. items-center, justify-center, and mx-auto will align the switch to the center. The absolute class positions elements outside of the normal flow of the page. Therefore, to control the absolutely positioned switch, we will add top-5, right-0, and left-1/3. This means that our element will be to the right and at the top. Let’s add some content so that we can see the position.
We will add another div element under the container.
The div will contain the following classes:
<div class="relative flex items-center justify-end mx-auto space-x-2">
<span class="text-xs font-extralight">Light </span>
<span class="text-xs font-semibold">Dark</span>
</div>
This is how it should look like at this point:
Result
In the code above:Under the element that adds “Light” text, create another div element like the one below:
<div>
<input type="checkbox" name="" id="checkbox" class="hidden" />
<label for="checkbox" class="cursor-pointer">
<div class="flex items-center h-5 bg-gray-300 rounded-full w-9 p2">
<div class="w-4 h-4 bg-white rounded-full shadow"></div>
</div>
</label>
</div>
What have we added here?
First, we have created a checkbox; we have used element to do so. The checkbox will apply the “ON” and “OFF” style to our switch; it is hidden using the hidden class. There is a