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
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.
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.
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.
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.
[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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With