21

I want to have a square image inside a circle.

When the user hovers over the image, the image should scale (zoom in).

The circle should remain the same size.

Only during the CSS transition, the square image overlaps the circle (as if overflow:hidden weren't there at all).

Here's a demo with the weird behavior in Chrome and Safari: http://codepen.io/jshawl/full/flbau

Working ok in firefox.

0

4 Answers 4

50

You need to add this code to the parent of your img :

position:relative;
z-index:1;

Example here : http://codepen.io/DavidN/pen/dIzJK

Sign up to request clarification or add additional context in comments.

7 Comments

I already had position:relative, but adding z-index:1 fixed it. Thank you!
Yes this solution worked a treat for me. I wasn't using a circle, so didn't want to use the mask. Seems kind of obvious now.
Another +1 here, this works when you have an image inside an anchor with rounded corners and want to 'zoom in' on hover. Simple when you think about it. Thanks
This is better answer as using mask also hides box-shadow.
This has helped me, thanks! Lesson learned - always apply some kind of positioning to the block element. Block in BEM methodology.
|
23

I removed some superfluous markup (the circle and square containers... only needs one) and styled the img itself:

#wrapper {
  width: 500px;
  height: 500px;
  border-radius: 50%;
  overflow: hidden;
  -webkit-mask-image: -webkit-radial-gradient(circle, white, black);
}

#test {
  width: 100%;
  height: 100%;
  transition: all 2s linear;
}

#test:hover {
  transform: scale(1.2);
}
<div id="wrapper">
  <img src="http://rack.3.mshcdn.com/media/ZgkyMDEzLzA1LzA4L2JlL2JhYmllc193aXRoLjA5NGNjLnBuZwpwCXRodW1iCTg1MHg1OTA-CmUJanBn/8f195417/e44/babies_with_swagg.jpg" id="test">
</div>

2 Comments

the circle should stay the same size
dont over complecate it this is the answer position:relative; z-index:1;
5

Add this code to your parent div and solve problem :

-webkit-backface-visibility: hidden;
-moz-backface-visibility: hidden;
-webkit-transform: translate3d(0, 0, 0);
-moz-transform: translate3d(0, 0, 0);

1 Comment

Could you please elaborate more your answer adding a little more description about the solution you provide?
3

It appears as though you were styling one too many elements! I've created a fork here

I edited some of your SASS code to utilize the compass library and make better use of the transition and transform properties which can be seen here:

body { padding: 3em; }

.circle {
    height: 500px;
    width: 500px;
    border: 1px solid black;
    @include border-radius(500px);
    overflow: hidden;
}

.circle img {
    height: 500px;
    width: 500px;
    @include transition(all 0.3s ease);
    &:hover { @include transform(scale(1.1)); }
}

Hopefully this helps! Just think of the circle element as the parent container which has general information about the space (e.g. 500px wide and 500px tall). The image itself has a rounded border of 500px. This is the element you want to edit! You can scale and transform this element here without interacting with the parent circle container. Reference compass for additional information about using the library! Good luck!

5 Comments

It's weird that adding overflow:hidden to the image works. Shouldn't it be on the parent?
The best way to find out is by trying! Remove the overflow property from .circle img and place it in .circle and see the results. Do you even need the overflow property?
oh wait I just realized your fork makes the circle bigger. the circle should stay the same size. the image should get bigger
see this fork. no border radius works. border radius doesn't codepen.io/jshawl/pen/skGJl

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.