3

How properly set background opacity with next:

<body style="background-image: url(images/background.jpg); background-color: white;
 background-position: center center; background-size: 100%; 
 background-repeat: no-repeat">

I try with:

background-opacity: 0.5;

But fail.

It should be something like:

background-image: url(images/background.jpg) opacity: 0.5;

Even,

opacity: .4;

Doesn't affect on image, but affect on the text and whole of body.

Solutions with div doesn't accepted.

My web page: http://shell.bshellz.net/~panzerdivision/

Browsers: firefox & chrome

innocent-world
  • 548
  • 2
  • 7
  • 11

5 Answers5

2

Using opacity property, changes the opacity of the element itself.

To have a transparent background-color you could use rgba() as background color instead:

background-color: rgba(255, 255, 255, .5);

Or in short-hand:

background: rgba(255, 255, 255, .5) url(path/to/transparent/background.png) center center no-repeat;

But If you need to have a transparent background image, the image should be transparent itself.

By the way, here is a solution to give opacity to background-image but I'm not sure whether or not is could be useful in this case.

Community
  • 1
  • 1
Hashem Qolami
  • 97,268
  • 26
  • 150
  • 164
0

You cannot set the opacity of the entire background of an element using CSS.

  • You can set the opacity of an entire element (with the opacity property).
  • You can set the opacity of a background colour (with an rgba colour: background-color: rgba(50%, 50%, 50%, 0.5);)

If you want a translucent background image, you have to encode the opacity in the image itself (use the PNG or WebP image formats, which support this).

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
0

Use the following code and adjust the width as you want

<p><img style="opacity:0.9;" src="C:\Users\Desktop\images.jpg" width="300" height="231" alt="Image" /></p>
0

From https://css-tricks.com/maybe-there-kinda-is-background-opacity/

.el {
  background-image: -webkit-cross-fade(
    url(image.jpg),
    url(data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7),
    50%
  );

By fading image with a transparent base64 url encoded gif you can get to where you wanted so many years ago. :)

Does not currently work on Firefox. :(

Mikko Tapionlinna
  • 1,450
  • 1
  • 12
  • 21
0

To set the background opacity of the body element in one line using inline styles, you can use the rgba() CSS function to specify the background color with an alpha value (opacity). The rgba() function takes four parameters: red, green, blue, and alpha, where alpha specifies the opacity level from 0 (fully transparent) to 1 (fully opaque).

Here's how you can set the background opacity of the body element in one line:

<body style="background-image: url(images/background.jpg); background-color: rgba(255, 255, 255, 0.5); background-position: center center; background-size: 100%; background-repeat: no-repeat">

In this example, the rgba(255, 255, 255, 0.5) value for background-color sets the background color to white with 50% opacity, giving a semi-transparent effect.

Adjust the last parameter (0.5 in this example) as needed to control the opacity level. Use 0 for fully transparent and 1 for fully opaque, and any value in between for various levels of opacity.

Muhammad Bilawal
  • 354
  • 3
  • 11