Ask Your Question
0

How can we use the tailwind.config.js file to implement styling that depends on whether the mode is light or dark?

asked 2023-03-19 11:00:00 +0000

ladyg gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2022-06-19 17:00:00 +0000

pufferfish gravatar image

We can use the theme() function and the darkMode property in the tailwind.config.js file to implement styling that depends on whether the mode is light or dark.

Here is an example of how to do this:

// tailwind.config.js

module.exports = {
  darkMode: 'class',
  theme: {
    extend: {
      colors: {
        primary: theme => ({
          light: theme('colors.blue.500'),
          dark: theme('colors.green.500'),
        }),
      },
    },
  },
};

In this example, we have set the darkMode property to 'class', which means that we will use the CSS class dark to switch between light and dark modes.

Next, we define a custom color called primary, which has different values depending on the mode. We use the theme() function to access the colors defined in Tailwind CSS, and we use the dark property to define the dark mode color.

Finally, we can use this custom color in our CSS by referencing it with the text-primary-light and text-primary-dark classes:

<!-- HTML -->

<h1 class="text-primary-light dark:text-primary-dark">Hello World!</h1>

In this example, the text color will be blue in light mode and green in dark mode.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-03-19 11:00:00 +0000

Seen: 9 times

Last updated: Jun 19 '22