Scaling And Shrinking Effects On HTML Elements Using CSS3

Introduction

In this article we will learn how to add a shrinking and growing effect on an image whenever a user hovers the mouse over an image. This effect is applied by shrinking  or growing the size of the image. The shrink effect takes it's time to shrink the image, and that results in a beautiful animation effect on the image.

Scaling effect

The name seems to be self-explanatory and that's true. Scale effect is used for shrinking and growing the HTML element. Please note that it can shrink and grow a HTML element, not just an image. Scaling effects comes in four variants. These variants are as follows:

  1. Equal Scaling on both axes: scale(K)
     
  2. Unequal scaling on X and Y axes: scale(K1,K2)
     
  3. Scaling around X-axes: scaleX(K)
     
  4. Scaling around Y-axes: scaleY(K)

In all the preceding methods K is an integer scaling or shrinking factor. If K is less then one then it is a shrinking effect and if it is greater then one then it is a scaling effect. When K is equal to one, the element returns to its original position.

Mathematically it can be represented as follows:

If K>1 : Scaling

If K<1 : Shrinking

If K=1 : Original Size.

So let's see the preceding methods in action. First comes the equal scaling on both axes.

Equal Scaling on both axes: scale(K)

<!DOCTYPE html>

<html>

<head>

  <metacharset="utf-8">

  <title>JS Bin</title>

</head>

<body>

   <divid="imgbox-shrink">

    <imgsrc="http://www.c-sharpcorner.com/UploadFile/AuthorImage/4aac15.jpg"/>

</div>

   <divid="imgbox-grow">

    <imgsrc="http://www.c-sharpcorner.com/UploadFile/AuthorImage/4aac15.jpg"/>

</div>

</body>

</html>

The above HTML will create two simple divs with an image placed in it. Now to add an effect to it use the following CSS3:

#imgbox-shrink{

  border:2px dotted black;

  width:160px;

  height:160px;

}

 

#imgbox-shrink img{

  -webkit-transition:all 0.5s linear;

  -moz-transition:all 0.5s linear ;

  -ms-width:all 0.5s linear ;

  -o-width:all 0.5s linear ;

}

 

#imgbox-shrink img:hover{

  -webkit-transform:scale(0.5);

  -moz-transition:scale(0.5) ;

  -ms-width:scale(0.5) ;

  -o-width:scale(0.5) ;

}

#imgbox-grow{

  border:2px dotted black;

  width:160px;

  height:160px;

}

 

#imgbox-growimg{

  -webkit-transition:all 0.5s linear;

  -moz-transition:all 0.5s linear ;

  -ms-width:all 0.5s linear ;

  -o-width:all 0.5s linear ;

}

 

#imgbox-grow img:hover{

  -webkit-transform:scale(1.5);

  -moz-transition:scale(1.5) ;

  -ms-width:scale(1.5) ;

  -o-width:scale(1.5) ;

}

Most of the code is boiler plate for the sake of compatibility among various browser rendering engines. The important point in code above is "scale(0.5)". It's a CSS3 effect that is used to scale any element by the scaling factor K. Here K is any number pass to scale function. As K is less then one, so the shrinking will occur as expected. "scale(1.5)" is a case where K is greater then one, so the scaling will occur by the factor of 1.5 on both the axes as expected.

   

Unequal scaling on X and Y axes: scale(K1,K2)

When two different values of K are passed to scale function say K1 and K2 then the element is scaled by a K1 factor on the X-axes and by a K2 factor on the Y-axes.

<div id="imgbox-mix">

<img src="http://www.c-sharpcorner.com/UploadFile/AuthorImage/4aac15.jpg"/>

</div>

 

CSS

#imgbox-mix{

  border:2px dotted black;

  width:160px;

  height:160px;

}

 

#imgbox-mix img{

  -webkit-transition:all 0.5s linear;

  -moz-transition:all 0.5s linear ;

  -ms-width:all 0.5s linear ;

  -o-width:all 0.5s ;

}

#imgbox-mix img:hover{

  -webkit-transform:scale(0.5,1.5);

  border:2px dotted black;

  -moz-transition:scale(0.5,1.5) ;

  -ms-width:scale(0.5,1.5) ;

  -o-width:scale(0.5,1.5) ;

}

As you can see I have passed two different scaling factors in the scale function, one is less the 1 and the other is greater than 1. So in action the image will be scaled on the Y axes and shrinked along the X axes.

 

 
 
Scaling around X-axes: scaleX(K)

In this effect we apply a scaling effect only along the X-axes and in the result the element is scaled by a factor of K.

<div id="imgbox-scaleX">

<img src="http://www.c-sharpcorner.com/UploadFile/AuthorImage/4aac15.jpg"/>

</div>

CSS

#imgbox-scaleX{

  border:2px dotted black;

  width:160px;

  height:160px;

}

 

#imgbox-scaleX img{

  -webkit-transition:all 0.5s linear;

  -moz-transition:all 0.5s linear ;

  -ms-width:all 0.5s linear ;

  -o-width:all 0.5s

}

#imgbox-scaleX img:hover{

  -webkit-transform:scaleX(0.5);

  border:2px dotted black;

  -moz-transition:scaleX(0.5) ;

  -ms-width:scaleX(0.5) ;

  -o-width:scaleX(0.5) ;

}

As per the preceding CSS the image will be shrinked along the X axes as the value passed to the scale function is less than one. The point to note here is that the image height remains unaffected by this scaling but the width of the image is reduced by 0.5.
 
 

Scaling around Y-axes: scaleY(K)

This works exactly in the same way as "scaleX(K)" except that the scaling function is applied on the Y axes instead of the X axes. In this effect the height of the image is changed and the width of the image remains unaffected. Check out the following demo:

<divid="imgbox-scaleY">

<imgsrc="http://www.c-sharpcorner.com/UploadFile/AuthorImage/4aac15.jpg"/>

</div>

CSS

#imgbox-scaleY{

  border:2px dotted black;

  width:160px;

  height:160px;

}

#imgbox-scaleY img{

  -webkit-transition:all 0.5s linear;

  -moz-transition:all 0.5s linear ;

  -ms-width:all 0.5s linear ;

  -o-width:all 0.5s

}

#imgbox-scaleY img:hover{

  -webkit-transform:scaleY(0.5);

  border:2px dotted black;

  -moz-transition:scaleY(0.5) ;

  -ms-width:scaleY(0.5) ;

  -o-width:scaleY(0.5) ;

} 

 
 
Live Demo List
  1. Equal Scaling on both axes: scale(K)
     
  2. Unequal scaling on X and Y axes: scale(K1,K2)
     
  3. Scaling around X-axes: scaleX(K)
     
  4. Scaling around Y-axes: scaleY(K)

Summary

That is all I have for this article. I hope you have enjoyed reading this article. It has shown some very basic effects but can add stars to your website if it's used sensibly. In case of any doubt feel free to ask in the comments section. 

Next Recommended Readings