简体   繁体   中英

Zoom an image when hover

I'm trying to build a website, so I have an image when I hover it I want it to zoom but the problem is the image zoom with a bad way it come out of a div how I can fix that?

<div class="container">
  <img src="image.jpg" alt="" height="400" width="500"/>
</div>

My css style

.container{
  height: 400px;
  width: 500px;
  border: 2px solid red;
}
.container:hover img{
  height: 500px;
  width: 600px;
}

FIDDLE

something like this JS Fiddle ?

 .container { height: 400px; width: 500px; border: 2px solid red; overflow: hidden } .container img { /* add this rule */ width:100%;; height:100%; transition:all 0.7s ease-in-out; } .container:hover img { /* change this rule */ width:120%; height:120%; margin:-10% 0 0 -10%; transition:all 0.7s ease-in-out; } 
 <div class="container"> <img src="http://motivationhacks.org/wp-content/uploads/2015/07/steve-jobs-black-white.jpg" alt="" height="400" width="500" /> </div> 

One of the most efficient method will be to use scale transition CSS property as follows,

.container{
  height: 400px;
  width: 500px;
  border: 2px solid red;
  overflow: hidden;
}

.container:hover img{
  transform: scale(1.25);
}

By using the scale property, the image would zoom smoothly on all devices including mobile devices. So you don't need to specify accurate height and width values for each and every device.

Hope this helps :)

try to add overflow: hidden to the div so the content will be clipped, and the rest of the content will be invisible

.container{
  height: 400px;
  width: 500px;
  border: 2px solid red;
  overflow: hidden
}
.container:hover img{
  height: 500px;
  width: 600px;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM