Search code examples
htmlcsscss-transitionscss-transforms

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>

Solution

  • 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;
      transition: transform .4s;   /* smoother zoom */
    }
    #container:hover img {
      transform: scale(1.3);
      transform-origin: 50% 50%;
    }
    <div id="container">
        <img id="image" src="https://via.placeholder.com/150">
    </div>