

This means that when dark mode is enabled, the dark class will be added to the html element, and we can use this class to apply different styles for dark mode.īuilding the a project with the dark mode feture. Here, we set darkMode to 'class' to enable dark mode using the class attribute.
Css dark mode switch install#
To install Tailwind css, you need to open your terminal and run the following code below on your terminal.Įnter fullscreen mode Exit fullscreen mode Install Tailwind CSS as a dependency in your project Tailwind CSS also provides features like responsive design and dark mode support.īefore we can implement dark mode in our React application using Tailwind CSS, we need to set up Tailwind CSS.

Instead of creating custom CSS styles for each component, you can use the pre-defined classes to apply styles in a more consistent and efficient way. Tailwind CSS is a utility-first CSS framework that provides a set of pre-defined classes that you can use to style your UI components.
Css dark mode switch how to#
In this blog post, we will explore how to implement dark mode in a React application using Tailwind CSS.

It provides a darker color scheme that can be easier on the eyes, especially in low light environments. Dark mode is a popular feature that is becoming increasingly common in web and mobile applications. addEventListener ( 'click', ( ) => else if (localStorage. The question is, how can we "enable" the dark theme? We can write some JavaScript to do the work for us: const htmlClasses = document. In the above example, the text will be black for the light theme however it will change to white if the dark theme is enabled. Once this configuration is done, dark variants can be added to our application, in the following way: Light | Dark
Css dark mode switch manual#
media considers the prefers-color-scheme feature however if we'd like to enable switching between the themes manually, we need to use class.Įnabling the manual switch requires the darkMode property added to the file with a value of class. Let's take a look at a potential implementation of this using the framework mentioned above.Įnabling dark mode using Tailwind, there are two options: class and media.

Different CSS frameworks offer different solutions, including TailwindCSS. In these implementations, we can switch between the light and the dark theme, as expected. The first option is what we could call a "two-state" switch. After doing some research, which involved visiting numerous sites and reading articles around the subject, I found that there seem to be two distinctive implementation options for switching between themes.īefore I continue, I wanted to thank Chee Aun for pointing me in the right direction regarding the various state options for the light/dark theme. Large companies - most notable in the recent months, Stack Overflow and GitHub have also implemented a dark theme. What we'll discuss, however, is the correct implementation of the light/dark theme in modern websites. The science behind this is more complex, and in this article, we are not exploring this subject. Research by the Harvard Medical School shows that extended exposure to blue light can affect how well we sleep during the night. In recent years more websites have implemented "dark" mode, a theme that reduced the light emitted by the device's screen and therefore causing the reader less eye strain.
