Change the image size on hover without changing the parent div's size

Viewed 702

I have a question. I need to change the image size on hover without changing the parent div's size. Also, I have some

tags inside the same div with the image. When I add hover on image and give some pxs to width and height, the

tags move with the image.

img {
    width: 200px;
    height: 200px;
}

img:hover {
    width: 250px;
    height: 250px;
    overflow: hidden;
}
 <div class="product-items">
                    <div class="desc">
                        <img src="https://cdn.vox-cdn.com/thumbor/v97OD-MBgNjw8p5crApucVs9RB8=/0x0:2050x1367/1800x1800/filters:focal(1025x684:1026x685)/cdn.vox-cdn.com/uploads/chorus_asset/file/22022572/bfarsace_201106_4269_012.0.jpg" alt="">
                        <div class="cost">
                            <p class="your_product">Ваш товар</p>
                            <p>$89.00</p>
                        </div>
                    </div>
                </div>

3 Answers

You don't need to alter your HTML - just scale the img on hover rather than alter its width and height. Scaling will not affect the positioning of other elements as the scaled element keeps its original size as far as they are concerned.

img {
  width: 200px;
  height: 200px;
}

img:hover {
  /*width: 250px;
    height: 250px;*/
  transform: scale(1.25);/* added */
  overflow: hidden;
}
<div class="product-items">
  <div class="desc">
    <img src="https://cdn.vox-cdn.com/thumbor/v97OD-MBgNjw8p5crApucVs9RB8=/0x0:2050x1367/1800x1800/filters:focal(1025x684:1026x685)/cdn.vox-cdn.com/uploads/chorus_asset/file/22022572/bfarsace_201106_4269_012.0.jpg" alt="">
    <div class="cost">
      <p class="your_product">Ваш товар</p>
      <p>$89.00</p>
    </div>
  </div>
</div>

you can do this by using the transform property

.product-items{
    width: 200px;
    height: 200px;
}

.img-div{
    height: 100px;
    width: 100%;
    overflow: hidden;
}

img {
    width: 100%;
    height: 100%;
    transition: 0.4s;
}
div:hover img {
    transform: scale(1.2,1.2);
}
<div class="product-items">
      <div class="desc">
        <div class="img-div">
          <img
            src="https://cdn.vox-cdn.com/thumbor/v97OD-MBgNjw8p5crApucVs9RB8=/0x0:2050x1367/1800x1800/filters:focal(1025x684:1026x685)/cdn.vox-cdn.com/uploads/chorus_asset/file/22022572/bfarsace_201106_4269_012.0.jpg"
            alt=""
          />
        </div>
        <div class="cost">
          <p class="your_product">Ваш товар</p>
          <p>$89.00</p>
        </div>
      </div>
    </div>

You can try add container on image and set position:relative and for img position:absolute

.img-container {
  width: 200px;
  height: 200px;
  position: relative;
}

img {
  width: 100%;
  height: 100%;
  position: absolute;
}
img:hover {
  width: 250px;
  height: 250px;
  overflow: hidden;
}
<div class="product-items">
  <div class="desc">
    <div class="img-container">
      <img src="https://cdn.vox-cdn.com/thumbor/v97OD-MBgNjw8p5crApucVs9RB8=/0x0:2050x1367/1800x1800/filters:focal(1025x684:1026x685)/cdn.vox-cdn.com/uploads/chorus_asset/file/22022572/bfarsace_201106_4269_012.0.jpg" alt="">
    </div>
    <div class="cost">
      <p class="your_product">Ваш товар</p>
      <p>$89.00</p>
    </div>
  </div>
</div>

Related