Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change brightness of background-image?

I am wondering if it is possible to change the brightness of:

 body{
 background-image:url();
 }

Using HTML/CSS. The reason I would like to change it, is because I just spent a rather long time making the image, but when I put it on website, it is suddenly about twice as bright. I have compared the original file and the file that is input into the website and they are both very much different colours of blue.

Is there any reason for this, and is there a way I can change the brightness?

Thanks.

like image 454
user3227878 Avatar asked Jan 26 '14 12:01

user3227878


People also ask

How do I make my background picture brighter?

To set image brightness in CSS, use filter brightness(%). Remember, the value 0 makes the image black, 100% is for original image and default. Rest, you can set any value of your choice, but values above 100% would make the image brighter.

How do I make my background image dim?

We can use the darken option to make the background image darker. We can set the color of the background image using the rgba() function. For example, select the background id in CSS and use the background property to set the color and the background image.

How do I dim an image in CSS?

The brightness() function can be used as a value to apply a linear multiplier to make it appear darker or lighter than the original. To make an image darker, any value below 100% could be used to darken the image by that percentage.


5 Answers

background:linear-gradient(rgba(255,255,255,0.5), rgba(255,255,255,0.5)), url(myimage.png);

This will put 50% white over the original image.

Linear-gradient function has to be used, otherwise it doesn't work.


Or you can use:

.someObj:after{ content:''; background:rgba(255,255,255,.5); .... }

and this is better for code maintainability.

like image 63
Martin Zvarík Avatar answered Oct 05 '22 21:10

Martin Zvarík


This would be an option, but it's not very practical and wouldn't work in older browsers:

body:after {
  content: "";
  position: fixed;
  top: 0; bottom: 0; left: 0; right: 0; 
  background: rgba(0,0,0,0.1);
  pointer-events: none;
}

Or for even better color control, try hsla() colors:

body:after {
  content: "";
  position: fixed;
  top: 0; bottom: 0; left: 0; right: 0; 
  background: hsla(180,0%,50%,0.25);
  pointer-events: none;
}

Really, it's better to play with the image in a image editor until you get the browser result you want.

like image 45
ralph.m Avatar answered Oct 05 '22 21:10

ralph.m


There is no way to do this that works in every browser, but if you want, you can do it in webkit browsers (Chrome, Safari, Opera), by using the filter style:

img.lessBright {
    -webkit-filter: brightness(0.8);
    filter: brightness(0.8);
}

That results in the brightness being reduced to 80% in webkit browsers. I do recommend just saving another version of your image if you want to do this though.

like image 29
Joeytje50 Avatar answered Oct 05 '22 22:10

Joeytje50


  1. Create a child div for the background image of the same dimensions.
  2. Set the child divs background colour with RGBA, which includes an alpha channel (opacity).
  3. Set the opacity accordingly between 0 and 1. 1 = Opaque, 0=Transparent, 0.5 =Translucent

HTML:

<div id="primary_feature">
    <div class="feature"></div>
</div>

CSS:

#primary_feature{
  background-image: url("../assets/images/misc/laptop.png");
}
.feature{
  background:rgba(0,0,0,0.6);
}
like image 38
Sam_ Avatar answered Oct 05 '22 21:10

Sam_


An update to the other answer.

You can also use the Backdrop Filter for a much better effect. It can use any filter, in this case, the brightness filter.

This means your background will not be washed-out with a colour over the top, rather it will affect the background directly for a sharper more natural look while retaining all detail.

The downside, it isn't currently supported in Firefox, unless experimental settings are turned on. But that should change soon and as of writing this, Firefox is only 6.5% of the market.

enter image description here

however, it is fully supported in Chrome

body {
content: "";
    position: fixed;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    backdrop-filter: brightness(120%);
    pointer-events: none;
}
like image 30
Electron Avatar answered Oct 05 '22 21:10

Electron