1

I followed the steps here: Changing image size during mouse hover However, when the image gets resized, the rest of the content moves around the page. How can I prevent this?

jasd
  • 67
  • 4

2 Answers2

1

Just use transform: scale() in your CSS for the elements pseudo hover rule.

.imgclass:hover {
  transform: scale(1.3);
}

#parent {
  display: flex;
}

.main {
  margin-left: 10px;
  width: 200px;
}

.img {
  height: 200px;
  width: 200px;
  background-color: red;
}

.img:hover {
  transform: scale(1.3);
}

#cont {
  margin-top: 10px;
  height: 200px;
  width: 400px;
  background-color: green;
}
<div id="parent">
  <img class="img" src="https://zoodegranby.com/content/images/_300xAUTO_fit_center-center/Panda_280x280.jpg">
  <div class="main">
    <div>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum."
    </div>
  </div>
</div>
<div id="cont"><div>
dale landry
  • 7,831
  • 2
  • 16
  • 28
0

Another method is to wrap the image into container and set image's position to absolute:

.imgBox,
.imgBox > img
{
  width: 50px;
  height: 50px;
}
span.imgBox
{
  display: inline-block;
}
.imgBox
{
  position: relative;
}
.imgBox > img
{
  position: absolute;
}
.imgBox:hover > img
{
  width: 200px;
  height: 200px;
}
<span>text before</span>
<span class="imgBox">
  <img id="image" src="https://www.google.com/images/srpr/logo11w.png" align="middle" alt="Img1"/>
</span>
<span>text after</span>
<div class="imgBox">
  <img id="image2" src="https://www.google.com/images/srpr/logo11w.png" align="middle" alt="Img1"/>
</div>
<span>text bottom</span>
vanowm
  • 9,466
  • 2
  • 21
  • 37