Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

css fade in transition for block text when hovering image?

So this is my code and I was looking to use css3 transitions to fade in the text and background colour when you hover the image. I've tried numerous selectors and transition types but cant seem to get it right any help is greatly appreciated.

see demo below

http://jsfiddle.net/jiceb/xsFmA/

<a href="#">
      <h2>Some Text</h2>
      <img src="http://www.sheridanrogers.com.au/wp-content/uploads/2011/03/American-pancakes.jpg"/>
</a>

and css

a { position: relative; display: inline-block }

a img {
    width:250px;
}

a h2 {
    display: none;
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    margin: 0;
    background: black;
    color: #fff;
}

a:hover h2 {
    display: block;
}
like image 970
Superfast Goose Avatar asked Oct 21 '13 10:10

Superfast Goose


People also ask

How do you make text appear when hovering over an image in CSS?

CSS. In order to position the text in over the <div>, you need to assign position: relative to the parent <div> and assign position: absolute to the child <div> element. Now the container is aligned for locating the child <div> to bottom-right assign bottom: 0 and right: 0 .


1 Answers

Rather than using display:none and display:block, simply use opacity and add a transition to your a h2 selector:

a h2 {
    opacity:0; /* Completely invisible. */
    transition:1s; /* A 1 second transition. */
}

a:hover h2 {
    opacity:1; /* Completely visible. */
}

This will cause the opacity to increase from 0 to 1 over the period of 1 second.

JSFiddle demo.

like image 187
James Donnelly Avatar answered Sep 19 '22 03:09

James Donnelly