Forum Moderators: not2easy

Message Too Old, No Replies

please don't laugh!

css positioning

         

dilby

11:25 am on Apr 26, 2007 (gmt 0)

10+ Year Member



Hello all. I am currently make a website and attempting CSS positioning for the first time. I believe what I'm doing is very simple, so I thought someone could possibly tell me how to do it very quickly?

I have a background image (which is an swf) and simply need to create two div's in which to put php tags into to load dynamic content. The background image needs to be centered (it is 780 wide) while one div needs to be against the left of the background towards the bottom and the other div against the right of the background towards the top.

I have tried but it's just becoming a mess and is completely different in different browsers. Any help would be a lifesaver!

Robin_reala

1:15 pm on Apr 26, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi dilby, welcome to WebmasterWorld

As far as I can understand your problem, something like this might do it:

<div id="wrapper">
<div id="box1"></div>
<div id="box2"></div>
</div>

#wrapper {
position: relative;
background: url(my_image.png) no-repeat 50% 50%;
width: 780px;
height: 780px;
}
#box1 {
position: absolute;
width: 200px;
bottom: 10px;
left: 10px;
}
#box2 {
position: absolute;
width: 200px;
top: 10px;
right: 10px;
}

Absolute positioning will take it's coordinates from the nearest positioned parent (in this case #wrapper) so we can align them how you want. The width:200px is arbitrary but you can tweak this.

dilby

4:01 pm on Apr 26, 2007 (gmt 0)

10+ Year Member



Great, thanks Robin, I'll give it a whirl! :D

dilby

4:32 pm on Apr 26, 2007 (gmt 0)

10+ Year Member



Hi Robin, still having trouble centering background? It sits on the left? What am I doing wrong?

Robin_reala

6:26 pm on Apr 26, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Oh, the whole lot centered in the middle of the screen? If you add
margin: 0 auto;
to your #wrapper selector then that should center the wrapper in the middle of the screen.

Dabrowski

10:22 pm on Apr 26, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Robin, I think it's worth noting the point of the relative position on #wrapper.

Without it dilby the boxes would take their position from #wrapper's parent instead, most likely <body> in your case.

So currently, bottom left and top right are of #wrapper, but without the relative position, that would be bottom left and top right of <body> (the whole screen) instead.

I think that's right, I'm not an expert myself, can you confirm that Robin?

Xapti

3:04 am on Apr 27, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Since the background is a flash file, you wouldn't want to use a background style, since AFAIK it doesn't support flash. Wrapper div would just have the swf object in it instead.

Robin_reala

6:46 am on Apr 27, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yep Dab, that's right. I alluded to it in my original post but your explanation was much better :) One slight adjjustment to your explanation - you said that if #wrapper wasn’t positioned then it’d be relative to the next parent. That’s only true if that parent is positioned itself. If not, then it keeps on working up the tree. If none of its parents are positioned then it uses the initial containing block to calculate its position from.

Xapti

8:26 am on Apr 28, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I always thought that was a Strange behaviour, but whatever.

Personally I think it should always go to parent, and you have to add something extra if you want it going with respect to something else.

Dabrowski

11:33 am on Apr 28, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I agree with you Xapti, you can effect this easily by adding in your CSS:

DIV { position: relative; }

Won't affect the normal flow but will mean any position is done in respect to parent.