Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the opacity of image on hover using css

I'm trying to figure out how to set all images to be 50% opacity initially, and then change to 100% opacity on hover.

I tried setting this rule in the .css file but it gives a parse error:

img {
  opacity:0.4;
  filter:alpha(opacity=40); 
}
img:hover {
  opacity:1.0;
  filter:alpha(opacity=100); 
}
like image 268
user2481095 Avatar asked Aug 14 '13 05:08

user2481095


People also ask

How do I give an image opacity in CSS?

To set the opacity of a background, image, text, or other element, you can use the CSS opacity property. Values for this property range from 0 to 1. If you set the property to 0, the styled element will be completely transparent (ie. invisible).

Is it possible to use the hover pseudo-class with opacity to make an effect?

By utilizing the :hover selector, you can set the opacity of an element to change on mouse-over to create a "fade in" or "fade out" effect.

How do you darken the image on hover?

To sum it up : Use opacity and filter property to darken an image and create cool hover effect with it. Use RGBA colors to make your background image darker.

How do you change transparency opacity in CSS?

There is no background-opacity property in CSS, but you can fake it by inserting a pseudo element with regular opacity the exact size of the element behind it.


1 Answers

Your code is good- verified in this Fiddle with a friendly fish: http://jsfiddle.net/Qrufy/

    img {
        opacity: 0.5;
        filter: alpha(opacity=40);
    }
    
    img:hover {
        opacity: 1.0;
        filter: alpha(opacity=100);
    }
    <img src="http://upload.wikimedia.org/wikipedia/commons/thumb/1/15/Colossoma_macropomum_01.jpg/800px-Colossoma_macropomum_01.jpg" />

The opacity property works in all modern browsers, and the filter:alpha covers <= IE8.

like image 166
jterry Avatar answered Sep 29 '22 08:09

jterry