Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS size transition to stay inside parent div

I want to have an image frame that when I hove over it the image inside will zoom in a little (I am using size transition), but the frame will stay the same size.

What happens now that even if the frame has a fixed width and height it is stilled zoomed with the image

HTML:

<div class="img-wrapper">
  <img class="thumbnail" src="http://placekitten.com/400/200">
</div>

and CSS

.img-wrapper {
  width: 400px;
}
.thumbnail {
  width: 400px;
}

.thumbnail {
  -webkit-transition: all 0.2s ease-out;
  -moz-transition: all 0.2s ease-out;
  transition: all 0.2s ease-out;
}

.thumbnail:hover {
  width: 500px;

  -webkit-transition: all 0.2s ease-out;
  -moz-transition: all 0.2s ease-out;
  transition: all 0.2s ease-out;
}

http://codepen.io/pen/KCJny

like image 244
ilyo Avatar asked Dec 19 '25 03:12

ilyo


2 Answers

One way to fix this would be to set overflow:hidden;

So, this might work:

.img-wrapper {
  width: 400px;
  height:200px;
  overflow:hidden;
}
like image 54
Brian Hoover Avatar answered Dec 20 '25 20:12

Brian Hoover


If you want the image to stay centered (as an addition to Brian's answer) you can do this:

.thumbnail {
  width: 400px;
  position:relative;
  left:50%;
  margin-left:-200px;
}

.thumbnail:hover {
  width: 500px;
  margin-left:-250px;
  -webkit-transition: all 0.2s ease-out;
  -moz-transition: all 0.2s ease-out;
  transition: all 0.2s ease-out;
}
like image 23
Shmiddty Avatar answered Dec 20 '25 20:12

Shmiddty



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!