58

I want to absolute position an image that I will be moving around in a div and want anything that extends outside the div to be clipped. Here is an example of the problem:

<html>
<body>
  <div style="width: 500px; height: 200px; border: 1px solid black; overflow: hidden;">
    <div style="width: 200px; height: 50px; margin: auto; border: 1px solid black; background: gray;">On top of image.</div>
    <div style="position: absolute; top: 10px; left: 250px; z-index: -1;"><img src="http://www.google.com/logos/worldcupfinale10-hp.gif" /></div>
  </div>
</body>
</html>

So, I want the right edge of the logo to not display. Ideas?

Garrett
  • 4,007
  • 2
  • 41
  • 59
David A
  • 583
  • 1
  • 4
  • 4

3 Answers3

118

Try adding position: relative to your outer div. This will position the image relative to that div (honoring the overflow style) instead of relative to the page.

Example:

<html>
<body>
  <div style="position: relative; width: 500px; height: 200px; border: 1px solid black; overflow: hidden;">
    <div style="width: 200px; height: 50px; margin: auto; border: 1px solid black; background: gray;">On top of image.</div>
    <div style="position: absolute; top: 10px; left: 250px; z-index: -1;"><img src="http://www.google.com/logos/worldcupfinale10-hp.gif" /></div>
  </div>
</body>
</html>

See it on JS Bin

Brian Moeskau
  • 20,103
  • 8
  • 71
  • 73
24

Since the image's container is positioned absolutely, it is outside of the flow of the "containing" div.

Your choices are to either position relatively or to adjust the dimensions of the absolutely-positioned div, dynamically, with jQuery.

Brock Adams
  • 90,639
  • 22
  • 233
  • 295
  • OK, I was afraid of that. Thanks! – David A Jul 12 '10 at 00:29
  • Hm what about this example `
    ` is there any reason for the inside div not to hide. Experiencing this behaviour in Safari for windows and Opera
    – Olga May 03 '12 at 12:11
  • 8
    This is not correct. Adding overflow:hidden; to your parent container will make the absolutely positioned element inside it not show outside . – Martin Andersson Oct 27 '12 at 17:09
  • 1
    @MartinAndersson, When replying to a comment, please use the `@user-name` address. This both clarifies what you are referring to *and* alerts the specified user. See ["Replying in comments", here](http://stackoverflow.com/editing-help#comment-formatting). – Brock Adams Oct 27 '12 at 20:27
  • 4
    @BrockAdams he wasn't replying to a comment though, he was commenting on an answer. – jazzpi Jun 25 '18 at 18:24
2

Move the position absolute to the image, then add the relative to the parent container. Worked for me in a similar situation.

<html>
<body>
  <div style="width: 500px; height: 200px; border: 1px solid black; overflow: hidden;">
    <div style="width: 200px; height: 50px; margin: auto; border: 1px solid black; background: gray;">On top of image.</div>
    <div style="position: relative; overflow:hidden;"><img style="position: absolute; top: 10px; left: 250px; z-index: -1;" src="http://www.google.com/logos/worldcupfinale10-hp.gif" /></div>
  </div>
</body>
</html>
Christos Hrousis
  • 707
  • 4
  • 16