theNeighbourhoodGhost theNeighbourhoodGhost - 1 month ago 23
HTML Question

Zoom on image inside div without div moving

How can I make the image inside of this div scale without the actual div scaling on hover? So I only want the image to get zoomed on.

Here is the code:

<div id="container">
<img id="image" src="some-image">
</div>

Answer

Use transform: scale

#container {
  display: inline-block;
  margin: 20px;
  border: 1px solid black;
  overflow: hidden;         /*  clip the excess when child gets bigger than parent  */
}
#container img {
  display: block;
}
#container:hover img {
  transform: scale(1.3);
  transform-origin: 50% 50%;
}
<div id="container">
    <img id="image" src="http://placehold.it/200">
</div>