tags:

views:

35

answers:

2

I've been banging my head on this one. 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?

+2  A: 

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.

bmoeskau
But then it's not positioned relative to the page anymore.
David A
If the image is suppose to "move around in the div" why would you want it relative to the page? Maybe I'm missing something.
bmoeskau
A: 

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
OK, I was afraid of that. Thanks!
David A