Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Brightening an image using styles or Javascript

Tags:

javascript

css

I would like to brighten an image on my webpage on mouseover using css or javascript. Ive seen some examples using opacity and filters in styles but they dont seem to work for me.

Thanks in advance

CP

like image 464
CP. Avatar asked Apr 15 '10 10:04

CP.


People also ask

How do you brighten an image with CSS?

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 you brighten an image?

Brightness/Contrast When you need to brighten a photo the most obvious place to start is to go to Image > Adjustments > Brightness/Contrast, or to select this tool on an Adjustment Layer. Brightness/Contrast is a good, simple option to use if the overall image is too dark.

How do you JavaScript an image?

Create Image Element in JavaScript Create an image element using the createElement() method on the document object. Then, set an image URL to its src attribute. Finally, add the image element to the DOM hierarchy by appending it to the body element.

Can images be styled using CSS?

Styling images in CSS works exactly the same way as styling any element using the Box Model of padding, borders, and margins for the content.


2 Answers

[UPDATE]

A pure CSS solution would be to use CSS filters:

img:hover {
    filter: brightness(1.5);
}

Here we add 50% brightness to all images on hover.


Why not? You can always set the background of the parent container to #fff (white) and then lower the opacity of the image.

HTML:

<div class="white">
    <img src="image.jpg" alt="Image" />
</div>

CSS:

.white { background: #fff; }
img:hover {
    opacity: 0.5;
    filter: alpha(opacity=50);
}

Another solution is to use a JavaScript library, such as Pixastic.

like image 173
Mickel Avatar answered Oct 16 '22 10:10

Mickel


You could use CSS sprites. Create 2 versions of the image: one normal, one bright and combine them into 1 image using Photoshop or whatever you use.

There's a good explanation of what CSS sprites are here.

Of course, this may not be something you could use on your site. For example, it's probably not something you'd want to use on large images (they'd double in size). You also couldn't do it this way if the images you want to brighten come from outside sources or are uploaded by users for example. It would work well for things like navigation bar images and other UI elements.

like image 28
thesonglessbird Avatar answered Oct 16 '22 11:10

thesonglessbird