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?
Asked
Active
Viewed 813 times
1
-
Add a div around the image with a fixed height and width. – FireFighter Apr 18 '21 at 19:23
-
1@FireFighter can you please elaborate? I'm still a beginner when it comes to HTML so I'm not sure how to do that. – jasd Apr 18 '21 at 19:25
-
Put the already existing `
` inside a `
`. Give that div a class: `– FireFighter Apr 18 '21 at 19:28
2 Answers
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