Forum Moderators: not2easy

Message Too Old, No Replies

Horizontal and vertical overlapping div alignment

Aligning divs horizontal and vertical

         

stupidcooper

11:17 am on Sep 14, 2009 (gmt 0)

10+ Year Member



Hi I am having problems making a couple of divs align in the centre for all resolutions, on my screen of 1680x1050 everything is fine as I have set the margins using px. Basically the set of of the page is a large frame in the middle of the screen with a wall light above it. The background is just an image 1679 width and 1200 height (don't know if this causes an issue with different resolutions or not)

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style type="text/css">
<!--
#apDiv1 {
position:absolute;
left:427px;top:293px;width:800px;height:500px;
z-index:1;visibility: inherit;
}
#apDiv3 {
position:absolute;
left:378px;top:120px;width:473px;height:485px;
z-index:0;
}
-->
</style>
<script src="Scripts/AC_RunActiveContent.js" type="text/javascript"></script>
</head>

<body background="wallpaper fullCOVEv2.jpg">

<div id="apDiv1">
<!--flash content is in this div-->
</div>
<div id="apDiv3"><img src="light test 2x.png" alt="" width="900" height="639" /></div>
</body>
</html>

D_Blackwell

3:48 pm on Sep 14, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



.....everything is fine as I have set the margins using px.....

In the sample code provided, you have not set margins at all - you have created the effect that you want at one resolution setting by positioning the elements. There are no margin declarations to be found.

For example:

#apDiv1 {
position:absolute;
left:427px;top:293px;

This will be positioned at those exact coordinates regardless of browser resolution. It will only center at one specific resolution. I would use something like:

#apDiv1 {
margin: 293px auto 0 auto;
}

That is quite possibly all that is required.

Do you really need the z-index: and visibility: declarations? Maybe. Quite possibly not.
.............................

.....just an image 1679 width and 1200 height (don't know if this causes an issue with different resolutions or not)

Yes it will be an issue.

1) You are not tiling a small file as would be typical for a body background but using a single image. How large is the file? Is it worth it?

2) Depending upon the image, it may need to be positioned to center as well.

3) I would remove this from inline and embed in the head.

html, body {
background: url(wallpaper fullCOVEv2.jpg) no-repeat center top; margin: 0; padding: 0;
}
.............................

Based on the description of what you want, and the markup, I would say that the approach to positioning the elements is fatally flawed as a result of relying on position: absolute; in this way and would use margin: instead.

swa66

9:09 pm on Sep 14, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There are a few ways to center absolutely positioned elements:

The first is a trick with negative margins and 50% positioning.

e.g.:


#apDiv1 {
position:absolute;
width:800px;
height:500px;
top: 50%
margin-top: -250px; /* half the height */
left: 50%
margin-left: -400px; /* half the width */
}

The key here is that you first take the side of the element and put it in the middle, and then move it back half of the size of the element itself to have it in the middle.
I think this works in all currently in use browser, including the legacy IE versions.
The caveat comes when the parent is not large enough to hold the element (so when the 50% is smaller than half the width), so you need to defend against that occurrence.

A second way is to use auto margins:

e.g.:


#apDiv1 {
position:absolute;
width:800px;
height:500px;
top: 0;
bottom: 0;
left: 0;
right: 0;
margin: auto;
}

This sets the top and bottom and the width, leaving only the margins to be determined automatically. At which point browsers should make them equal and center your element.
(dito horizontally)
This is cleaner (no negative margins, one must mot be able to predict the size (yes it will work on an <img> for which the CSS doesn't know the size etc), ...) but it's support in legacy IE versions is horrible. IE7.js fixes the worst of it for IE6.

Remember that an absolutely positioned element uses the closest parent that has "position" as reference for top/left/right/bottom. If there is no such parent it will use the viewport instead.
To intentionally give an element "position" all you need is "poisition:relative" on it.

stupidcooper

10:22 am on Sep 15, 2009 (gmt 0)

10+ Year Member



Thanks for both your replys. I have used the following..

#apDiv1 {
position:absolute;
width:800px;
height:500px;
top: 293px;
bottom: 0;
left: 0;
right: 0;
margin: auto;
}
#apDiv3 {
position:absolute;
width:900px;
height:639px;
top: 120px;
bottom: 0;
left: 0;
right: 0;
margin: auto;
}
Because I needed the Div3 to be centered but slightly higher up than div 1.

Can I add another quick question along the same sort of lines (positioning divs). I want to put a Home, About Us and Contact button at the top of the page but again I have used absolute positioning and just put in px numbers as below

#apDiv14 {
position:absolute;
left:74px;
top:78px;
width:65px;
height:22px;
z-index:2;
}
#apDiv15 {
position:absolute;
left:150px;
top:78px;
width:99px;
height:22px;
z-index:2;
}
#apDiv16 {
position:absolute;
left:270px;
top:78px;
width:83px;
height:22px;
z-index:2;
}

These are positioned so they are over the top of a certain part of the background but I now think that this won't be the case in all resolutions. It is hard to explain without someone seeing a page.