Can we change background color of image in html?


Example

Set a background-image for the element:

body {
 background-image: url("paper.gif");
 background-color: #cccccc;
}

Try it Yourself »

Example

Set two background images for the element:

body {
  background-image: url("img_tree.gif"), url("paper.gif");
  background-color: #cccccc;
}

Try it Yourself »

More "Try it Yourself" examples below.


Definition and Usage

The background-image property sets one or more background images for an element.

By default, a background-image is placed at the top-left corner of an element, and repeated both vertically and horizontally.

Tip: The background of an element is the total size of the element, including padding and border (but not the margin).

Tip: Always set a background-color to be used if the image is unavailable.

Show demo ❯

Default value:none
Inherited:no
Animatable:no. Read about animatable
Version:CSS1 + new values in CSS3
JavaScript syntax: object.style.backgroundImage="url(img_tree.gif)" Try it


Browser Support

The numbers in the table specify the first browser version that fully supports the property.

Property
background-image 1.0 4.0 1.0 1.0 3.5

Note: IE8 and earlier do not support multiple background images.



CSS Syntax

background-image: url|none|initial|inherit;

Property Values


More Examples

Example

Sets two background images for the element. Let the first image appear only once (with no-repeat), and let the second image be repeated:

body {
  background-image: url("img_tree.gif"), url("paper.gif");
  background-repeat: no-repeat, repeat;
  background-color: #cccccc;
}

Try it Yourself »

Example

Use different background properties to create a "hero" image:

.hero-image {
  background-image: url("photographer.jpg"); /* The image used */
  background-color: #cccccc; /* Used if the image is unavailable */
  height: 500px; /* You must set a specified height */
  background-position: center; /* Center the image */
  background-repeat: no-repeat; /* Do not repeat the image */
  background-size: cover; /* Resize the background image to cover the entire container */
}

Try it Yourself »

Example

Sets a linear-gradient (two colors) as a background image for a

element:

#grad1 {
  height: 200px;
  background-color: #cccccc;
  background-image: linear-gradient(red, yellow);
}

Try it Yourself »

Example

Sets a linear-gradient (three colors) as a background image for a

element:

#grad1 {
  height: 200px;
  background-color: #cccccc;
  background-image: linear-gradient(red, yellow, green);
}

Try it Yourself »

Example

The repeating-linear-gradient() function is used to repeat linear gradients:

#grad1 {
  height: 200px;
  background-color: #cccccc;
  background-image: repeating-linear-gradient(red, yellow 10%, green 20%);
}

Try it Yourself »

Example

Sets a radial-gradient (two colors) as a background image for a

element:

#grad1 {
  height: 200px;
  background-color: #cccccc;
  background-image: radial-gradient(red, yellow);
}

Try it Yourself »

Example

Sets a radial-gradient (three colors) as a background image for a

element:

#grad1 {
  height: 200px;
  background-color: #cccccc;
  background-image: radial-gradient(red, yellow, green);
}

Try it Yourself »

Example

The repeating-radial-gradient() function is used to repeat radial gradients:

#grad1 {
  height: 200px;
  background-color: #cccccc;
  background-image: repeating-radial-gradient(red, yellow 10%, green 20%);
}

Try it Yourself »


CSS tutorial: CSS Background, CSS Backgrounds (advanced), CSS Gradients

HTML DOM reference: backgroundImage property



Is there a way to apply background color on

Can we change background color of image in html?

Above code is not working .

Please help me .

Thanks

asked Sep 6, 2017 at 6:39

3

Yes it's possible. You just need to apply padding to image. Then you can get the result below:

or

You also can use .png images and apply transparent property.

img {
  max-width: 50%;
}
Can we change background color of image in html?

answered Sep 6, 2017 at 6:42

Can we change background color of image in html?

MaulikMaulik

7453 silver badges14 bronze badges

2

You could make use of the background-blend-mode property.

In particular, the background-blend-mode: multiply seems to be what you need here.

From the spec:

multiply blend mode

The source color is multiplied by the destination color and replaces the destination.

NB:

As you can't change your markup, there is still a way to style the images in the above way. (although it's a little bit hacky)

We can replace the img with the same background image with css by adding padding to the image and then setting the width and height of the image to zero. (more details here)

answered Sep 6, 2017 at 6:51

DanieldDanield

117k36 gold badges218 silver badges242 bronze badges

4

Since you use Slick slider, you might want to do something like the example below. As noticed before, you also might want to use PNG instead of JPG since PNG has the support of transparency.

.slick-slide .image img {
   background-color: red;
   padding: 5px; //some padding to show the background color
} 

answered Sep 6, 2017 at 7:06

How do you change the background color of an image?

You have to use an image with transparency. wrap img tag in a div tag and apply background color on div tag.

How do I cover the background of an image in HTML?

If you want the background image to cover the entire element, you can set the background-size property to cover.

Can we change background color of image in CSS?

It can be given by using the color name, rgb() value, or the hexadecimal value. The transparent value of this property is the default value, which specifies the transparent background color.

Can you change image color HTML?

You can change an image to grayscale, sepia and lot more (look at the example). So you can now change the color of a PNG file with filters.