TW Elements - TailwindCSS IntelliSense. Free UI/UX design course
Tags: tailwindcss, html, beginners, tutorial
Published: 2024-06-04 12:59:00

## Colors

Colours in Tailwind CSS are defined as classes that you can apply directly to your HTML elements. In this lesson, we'll learn how they work.

## Colour utility classes

Tailwind CSS comes with a wide variety of predefined colours. Each colour has different shades, ranging from 100 (lightest) to 900 (darkest). You can use these colours and shades by adding the corresponding utility classes to your HTML elements.

For example, if you wanted to set the background colour of an element to light blue, you would add the .bg-blue-200 class to that element:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5qoaxnugh0ys4chpt7mn.png)

If you want to add a darker blue, you can use e.g. .bg-blue-500:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u06oqcfvtoh5fp2flybg.png)

And so on:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6okoi908ikm0qq43m1lb.png)

## Background colour

As you have already noticed from the examples above, we use the bg-{color} class (like .bg-blue-500) to assign a selected color to an element.

There is no magic here anymore, so we will not dwell on the subject.

## Text colour

The situation is similar with the colour of the text, with the difference that instead of bg- we use the text- prefix:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iov5t6xr6i899803krgf.png)

**HTML**
```
<h5
  class="text-lg text-primary transition duration-150 ease-in-out hover:text-primary-600 focus:text-primary-600 active:text-primary-700 dark:text-primary-400 dark:hover:text-primary-500 dark:focus:text-primary-500 dark:active:text-primary-600">
  What exactly is beauty?
</h5>
```

And so on:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ya1ulb8pbr9jke35k047.png)

**HTML**

```
<h5 class="mb-3 text-lg text-blue-100">What exactly is beauty?</h5>

<h5 class="mb-3 text-lg text-blue-200">What exactly is beauty?</h5>

<h5 class="mb-3 text-lg text-blue-300">What exactly is beauty?</h5>

<h5 class="mb-3 text-lg text-blue-400">What exactly is beauty?</h5>

<h5
  class="mb-3 text-lg text-primary transition duration-150 ease-in-out hover:text-primary-600 focus:text-primary-600 active:text-primary-700 dark:text-primary-400 dark:hover:text-primary-500 dark:focus:text-primary-500 dark:active:text-primary-600">
  What exactly is beauty?
</h5>

<h5 class="mb-3 text-lg text-blue-600">What exactly is beauty?</h5>

<h5 class="mb-3 text-lg text-blue-700">What exactly is beauty?</h5>

<h5 class="mb-3 text-lg text-blue-800">What exactly is beauty?</h5>

<h5 class="mb-3 text-lg text-blue-900">What exactly is beauty?</h5>
```

## Customizing colours

While Tailwind provides a comprehensive set of color classes, you might need to customize these for your specific project. You can do this in your Tailwind configuration file (tailwind.config.js).

You need to add theme object configuration, so you can customize the colors by extending the default colors or completely replacing them.

Suppose we want to create a custom color with the value #123456.

**TAILWIND CONFIFURATION**

```
theme: {
  extend: {
    colors: {
      'custom-color': '#123456',
    }
  }
}
```

So we should add a theme object to our configuration file. Finally, our tailwind.config.js file should look like this:

**TAILWIND.CONFIG.JS**

```
/** @type {import('tailwindcss').Config} */
module.exports = {
  content: ['./index.html', './src/**/*.{html,js}', './node_modules/tw-elements/dist/js/**/*.js'],
  plugins: [require('tw-elements/dist/plugin.cjs')],
  darkMode: 'class',
  theme: {
    extend: {
      colors: {
        'custom-color': '#123456',
      }
    }
  }
};
```

After saving the file, we should be able to use the newly created _.bg-custom-color_ class in our HTML.

It was just additional information that we will not use in the current project. So, if you added a custom color to your config for testing purposes, then when you're done experimenting, restore the tailwind.config.js file to its original state.

**TAILWIND.CONFIG.JS**
```
/** @type {import('tailwindcss').Config} */ module.exports = {
  content: ['./index.html', './src/**/*.{html,js}', './node_modules/tw-elements/dist/js/**/*.js'],
  plugins: [require('tw-elements/dist/plugin.cjs')],
  darkMode: 'class',
};
```

Change the background color of the navbar
Let's use the acquired knowledge to change the background color of our navbar.

In your project, find the _.bg-neutral-100_ class in the navbar.

**HTML**

```
<!-- Navbar -->
<nav
  class="flex-no-wrap relative flex w-full items-center justify-between bg-neutral-100 py-2 shadow-md shadow-black/5 dark:bg-neutral-600 dark:shadow-black/10 lg:flex-wrap lg:justify-start lg:py-4"
  data-twe-navbar-ref>
  [...]
</nav>
```

Then replace it with the .bg-white class to change the color of the navbar to white.

**HTML**
```
<!-- Navbar -->
<nav
  class="flex-no-wrap relative flex w-full items-center justify-between bg-white py-2 shadow-md shadow-black/5 dark:bg-neutral-600 dark:shadow-black/10 lg:flex-wrap lg:justify-start lg:py-4"
  data-twe-navbar-ref>
  [...]
</nav>
```

Once the file is saved, the navbar should change from grey to white.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ga4v55iezjfwatnvdcl6.png)