

GNOME Terminal 3.22.1 Using VTE version 0.46.1 +GNUTLS.It is clever to add this feature to your webpages. Dark mode is said to reduce battery life in smartphones and also eye strain. In this article, we have gone through implementing dark mode in a webpage using Tailwind CSS. Here is our page in both light and dark mode: To switch between themes, we need to call our function then add an event listener.It means that, when the checkbox is checked, the HTML file will add the class dark and will remove it when the checkbox is unchecked. It tells us how the dark theme will be applied. We created a function and named it toggleDarkMode.First, we have used the querySelector() method to get the element HTML and an id we created for our checkbox.addEventListener( "click", toggleDarkMode) querySelector( "#checkbox") Ĭonst html = document.

Let’s add some custom CSS to the ball for it to move when we click on it!įor the custom CSS, let us name the ball switch-ball and add style using element under the tag. This will help us see if the checkbox is checked or unchecked since it is hidden.Īt this point, our switch should look like the one below: The second element creates a ball inside the round-shaped item.The item has a gray background and a round shape. Under, there is another element that creates an item for us to click on.The class cursor-pointer enables that feature. It will allow us to click anywhere on the switch to turn it on or off. The checkbox will apply the “ON” and “OFF” style to our switch it is hidden using the hidden class. First, we have created a checkbox we have used element to do so.Under the element that adds “Light” text, create another div element like the one below: With the words “light” and “dark” acting like icons on our page, let us add the switch which will be clickable. This means that the words will not affect the layout of the page or anything around them. relative class positions the items to the right.The element adds the words “light” and “dark”.

In this case, we have used words instead of icons for easy understanding.
