How to apply background image with linear gradient in Tailwind CSS?
Asked Answered
L

3

14

I want to apply a linear gradient to my background image. on tailwind config file I wrote a custom rule like this:

 theme: {
    extend: {
      backgroundImage: (theme) => ({
        'hero-pattern': "url('../src/images/icon-bg.jpg')",
  
      }),
    },
  },

It works. but when I try to apply a linear gradient it didn't woork.

For applying linear-gradient, what I have tried is this:

 theme: {
    extend: {
      backgroundImage: (theme) => ({
        
         'hero-pattern':
          "linear-gradient(to right bottom, rgba('#7ed56f',0.8), rgba('#28b485',0.8)), url('../src/images/icon-bg.jpg')",
      }),
    },
  },

But it didn't work.

Lesley answered 1/5, 2021 at 8:56 Comment(1)
According to docs you only need to add background image to config file. you can use the linear-gradient classes directly in your element. tailwindcss.com/docs/background-image#background-imagesGusto
L
8

The problem is that you give hex color code within rgba. That's why the color is not applied.

You have to give rgba color code instead of hex color code.

Note: Hex color code inside rgba is only supported by SCSS.

Lesley answered 3/5, 2021 at 14:58 Comment(0)
G
21

don't use function. just try as a utility

theme: {
    extend: {
      backgroundImage: {
         'hero-pattern': "linear-gradient(to right bottom, rgba('#7ed56f',0.8), rgba('#28b485',0.8)), url('../src/images/icon-bg.jpg')",
      },
    },
  },

here is a working example https://play.tailwindcss.com/uHp6pKIKEc

Gusto answered 1/5, 2021 at 11:20 Comment(1)
Any way to do this using utility classes? Not being able to use the background shorthand syntax seems like a shortcoming.Marabout
S
17

You don't have to tweak config file; Use the following class:

bg-[linear-gradient(to_right_bottom,rgba(49,84,44,0.8),rgba(16,71,52,0.8)),url('../src/images/icon-bg.jpg')]
Soulsearching answered 21/2, 2023 at 16:54 Comment(0)
L
8

The problem is that you give hex color code within rgba. That's why the color is not applied.

You have to give rgba color code instead of hex color code.

Note: Hex color code inside rgba is only supported by SCSS.

Lesley answered 3/5, 2021 at 14:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.