Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a Zoom Effect on an image on hover using CSS?

Tags:

html

css

class

I'm currently trying to create a zoom effect on hover over one of my four images. The problem is most examples usually use tables or mask divs to apply some sort of effect.

Here's one example that implements what I would like this.

This is my code so far.

HTML

<div id="menu"> <img class ="blog" src="http://s18.postimg.org/il7hbk7i1/image.png" alt=""> <img class ="music" src="http://s18.postimg.org/4st2fxgqh/image.png" alt=""> <img class ="projects" src="http://s18.postimg.org/sxtrxn115/image.png" alt=""> <img class ="bio" src="http://s18.postimg.org/5xn4lb37d/image.png" alt=""> </div> 

CSS

#menu {     max-width: 1200px;     text-align: center;     margin: auto; }  #menu img {     width: 250px;     height: 375px;     padding: 0px 5px 0px 5px;     overflow: hidden; }  .blog {     height: 375px;     -webkit-transition: all 1s ease;     -moz-transition: all 1s ease;     -o-transition: all 1s ease;     -ms-transition: all 1s ease;     transition: all 1s ease; }  .blog:hover {     cursor: pointer;     height:475px;     width: 350px; }  .music {     height: 375px; }  .projects {     height: 375px; }  .bio {     height: 375px; } 
like image 501
KingPolygon Avatar asked Apr 02 '13 05:04

KingPolygon


People also ask

How do I zoom an image in CSS?

First we specify the dimensions for the parent element. Then the child can fill the parent using width: 100% and height: 100%; , as well as set the background image, ensuring it scales to cover the area. You may want to use a tool for adding prefixes for the best possible browser support.

How do you get the zoom effect in CSS?

We need a container element which will be hovered and then the image inside it should animate accordingly, i.e. zoom-in or zoom-out as per the need. Note that the image should zoom on hover inside the container element and do not come or flow outside of it when it gets zoomed.


2 Answers

What about using CSS3 transform property and use scale which ill give a zoom like effect, this can be done like so,

HTML

<div class="thumbnail">     <div class="image">         <img  src="http://placehold.it/320x240" alt="Some awesome text"/>     </div> </div> 

CSS

.thumbnail {     width: 320px;     height: 240px; }  .image {     width: 100%;     height: 100%;     }  .image img {     -webkit-transition: all 1s ease; /* Safari and Chrome */     -moz-transition: all 1s ease; /* Firefox */     -ms-transition: all 1s ease; /* IE 9 */     -o-transition: all 1s ease; /* Opera */     transition: all 1s ease; }  .image:hover img {     -webkit-transform:scale(1.25); /* Safari and Chrome */     -moz-transform:scale(1.25); /* Firefox */     -ms-transform:scale(1.25); /* IE 9 */     -o-transform:scale(1.25); /* Opera */      transform:scale(1.25); } 

Here's a demo fiddle. I removed some of the element to make it simpler, you can always add overflow hidden to the .image to hide the overflow of the scaled image.

zoom property only works in IE

like image 169
Mitchell Layzell Avatar answered Sep 28 '22 06:09

Mitchell Layzell


Here you go.

Demo

http://jsfiddle.net/27Syr/4/

HTML

<div id="menu">      <div class="fader">         <div class="text">             <p>Yay!</p>         </div>         <a href="http://stackoverflow.com/questions/15732643/jquery-masonry-and-css3/">             <img class ="blog" src="http://s18.postimg.org/il7hbk7i1/image.png" alt="">         </a>     </div>      <div class="fader">         <div class="text">             <p>Yay!</p>         </div>         <a href="http://stackoverflow.com/questions/15732643/jquery-masonry-and-css3/">             <img class ="blog" src="http://s18.postimg.org/4st2fxgqh/image.png" alt="">         </a>     </div>      <div class="fader">         <div class="text">             <p>Yay!</p>         </div>         <a href="http://stackoverflow.com/questions/15732643/jquery-masonry-and-css3/">             <img class ="projects" src="http://s18.postimg.org/sxtrxn115/image.png" alt="">         </a>     </div>      <div class="fader">         <div class="text">             <p>Yay!</p>         </div>         <a href="http://stackoverflow.com/questions/15732643/jquery-masonry-and-css3/">             <img class ="blog" src="http://s18.postimg.org/5xn4lb37d/image.png" alt="">         </a>     </div>  </div> 

CSS

#menu {     text-align: center; }   .fader {     /* Giving equal sizes to each element */     width:    250px;     height:   375px;      /* Positioning elements in lines */     display:  inline-block;      /* This is necessary for position:absolute to work as desired */     position: relative;      /* Preventing zoomed images to grow outside their elements */     overflow: hidden; }       .fader img {         /* Stretching the images to fill whole elements */         width:       100%;         height:      100%;          /* Preventing blank space below the image */         line-height: 0;          /* A one-second transition was to disturbing for me */         -webkit-transition: all 0.3s ease;         -moz-transition:    all 0.3s ease;         -o-transition:      all 0.3s ease;         -ms-transition:     all 0.3s ease;         transition:         all 0.3s ease; }          .fader img:hover {             /* Making images appear bigger and transparent on mouseover */             opacity: 0.5;             width:   120%;             height:  120%; }      .fader .text {         /* Placing text behind images */         z-index: -10;               /* Positioning text top-left conrner in the middle of elements */         position: absolute;         top:      50%;         left:     50%; }          .fader .text p {             /* Positioning text contents 50% left and top relative                to text container's top left corner */             margin-top:  -50%;              margin-left: -50%; } 

Suggestion

Instead of .fader { inline-block; } consider using some grid system. Based on your technology of preference, you can go Foundation, Susy, Masonry or their alternatives.

like image 33
Andrey Mikhaylov - lolmaus Avatar answered Sep 28 '22 08:09

Andrey Mikhaylov - lolmaus