Welcome to WebmasterWorld Guest from 54.162.172.144

Forum Moderators: not2easy

Message Too Old, No Replies

DIV overlay staying static when I need it to move

     
1:33 pm on Dec 4, 2008 (gmt 0)

Junior Member

5+ Year Member

joined:Aug 7, 2007
posts: 186
votes: 0


I'm just developing a site where I have a single column of content that is always centred on screen (so if I resize my browser it'll move and stay centered.

The problem is I have an image, in a DIV, that I need to overlay on top of the page. The problem I'm having is that when I resize the browser window - that image stays in the same place and does not move with the rest of the site. I've tried positioning that div within other divs and changing the position from absolute to relative etc but it still seems to remain static on the page.

Here's the DIV code for the overlaying image:

#image-overlay
{
position: absolute;
top: 34px;
left: 434px;
width: 547px;
height: 58px;
}

I'm sure it's something very, very simply but I'm just not sure what?! Any help would be very much appreciated.

2:12 pm on Dec 4, 2008 (gmt 0)

Full Member

10+ Year Member

joined:Oct 17, 2003
posts:238
votes: 0


It's not moving because you've set it in a specific spot with absolute positioning.

It doesn't matter how much you resize the browser window, the image is going to stay right here.

top: 34px;
left: 434px;

2:21 pm on Dec 4, 2008 (gmt 0)

Junior Member

5+ Year Member

joined:Aug 7, 2007
posts:186
votes: 0


That's sort of what I thought but how do I do it so it moves correct to it's position within the containing DIV?
6:11 pm on Dec 4, 2008 (gmt 0)

Senior Member

WebmasterWorld Senior Member swa66 is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Aug 7, 2003
posts:4783
votes: 0


Using absolute positioning to center:

#image-overlay {
top: 50%;
margin-top: -29px; /* half of the height */
left: 50%
margin-left: -274px; /* half of the width */
}

Basically it first move e.g. the left side to the middle (

left: 50%;
) and then moves it back for half of the width of the element (negative margin).

If you want to center it relative to a parent, make sure the parent has position. Position can be given by e.g. "

position: relative;
" without needing to "nudge" it itself using left/top/right/bottom.
10:42 am on Dec 5, 2008 (gmt 0)

Junior Member

5+ Year Member

joined:Aug 7, 2007
posts:186
votes: 0


Okay, what I've ended up doing is adding "relative" positioning to the containing DIV - this then results in the image_overlay to stay perfectly positioned within that containing DIV.