Click to See Complete Forum and Search --> : Absolute/Relative Positioning workaround?


amosharper
12-11-2007, 04:52 AM
Hi all,

I'm gaving trouble (not the first time) with CSS positioning. In the site below, I want the three links on the right to be positioned exactly X pixels away from the centre, no matter what width the window, I want the background image dead centre horizontally, and I want the iframe with the site content Y pixels left of centre.

Easier said than done.

I tried to achieve it by setting all divs to 100% width and then using top and left, but this means the divs overlap - in this case, either the iframe div overlaps the menu buttons and you can't click on them, or the button divs overlap the iframe and you can;t scroll or highlight text in the top half of the iframe.

http://www.freewebs.com/amosharper/playsforkidz.com/index.htm

Is there a different way of making these divs positioned relative to top and centre whilst making sure they are no wider or taller than neccesary?

Huge thanks in advance,

- Amos

WebJoel
12-11-2007, 07:52 AM
This can be done using just margins, and some absolute positioning. The main container, if "position:relative;" and centered using "margin:0 auto;", serves platform for "absolute-positioned;" links that are 'to the right of' the container. Because the container is "relative", the links are based from point 0,0 of it, not the page. Thus, as the page size changes, so do the positioned links.
This is a relatively simple layout. I can work this later this evening, -time on the computer today will be tight but this could be done fairly easily.

(nice layout, btw. a little difficult to read the dark text on dark background, but nice concept)

amosharper
12-11-2007, 10:11 AM
This can be done using just margins, and some absolute positioning. The main container, if "position:relative;" and centered using "margin:0 auto;", serves platform for "absolute-positioned;" links that are 'to the right of' the container. Because the container is "relative", the links are based from point 0,0 of it, not the page. Thus, as the page size changes, so do the positioned links.
So you would get rid of the % width and the right margin, and modify margin-left and margin-top for each? (I assume "margin: x y;" is "margin: top, left;"?)

Nice layout, btw. a little difficult to read the dark text on dark background, but nice concept.
Thanks :)

Centauri
12-11-2007, 05:16 PM
This would work much better if the #page div was given a width (current auto side margins don't do anything without a width specified) equal to the graphic, and the graphic applied as a background to #page. Then you have a reference to place all the content normally usings margins and floats.

amosharper
12-11-2007, 05:19 PM
Would it still center?

Cheers,

Amos

Centauri
12-11-2007, 05:58 PM
Yes, setting a width and auto side margins on a div is the way to centre it.

WebJoel
12-11-2007, 06:51 PM
With a "width" and any valid "!doctype", yes, any element with "margin:auto;" will center.

I did a quick-mock here... much could be done to condense and modify (not the least of which, the way I did inline-styles on the indivial "<li>"s... could do alot better but I'm pressed for time for another project...

Try this:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<meta http-equiv="Content-Style-Type" content="text/css" />
<meta http-equiv="Content-Script-Type" content="text/javascript" />
<title></title>
<style type="text/css">
* {border:0; padding:0; margin:0;}/* Set everything to "zero" */

html, body {min-height:100%; /*for compliant browsers*/
font:x-small Arial, Verdana, sans-serif;voice-family: "\"}\"";voice-family:inherit;font-size:small;/*for IE 5.5 */
font-size:100%; line-height:1.125em;}

#wrapper p {font-size:0.875em; color:black; }
/*non-content: e.g., -navigation lists, etc. Slightly smaller than content text {font-size:0.75em;} */

html>body {font-size:small; height:auto;/* Assist IE6 and earlier, 100% height */
font-size: small; voice-family: "\"}\"";voice-family: inherit; font-size: medium}
/* Assist IE rendering height, keyword-font sizes, etc. */

#wrapper {min-height:100%; overflow:hidden;} /* 100% height for compliants, enwraps content*/
* html #wrapper {height:100%;}/* and feed IE what it needs */

h1, h2, h3, h4, h5, h6 {font-size:0; font-family: 'times new roman', arial, verdana, helvetica,
sans-serif; background-color:none;font-style:normal; font-variant:normal;
font-weight:normal; margin:14px 0 4px 10px;}
h1 {font-size: 1.60em;}
h2 {font-size: 1.50em;}
h3 {font-size: 1.40em;}
h4 {font-size: 1.30em;}
h5 {font-size: 1.26em;}
h6 {font-size: 1.20em;}/* Defined default Header sizes*/

body, html {background-color:#e6e6e6;}
#main {position:relative; width:700px; height:540px; margin:20px auto; background: #3d4b91 url(background.png);}
#navs {position:absolute; right:60px; top:50px; display:block;}
#navs li {list-style-type:none;}
#navs li a {color:white; background-color:none; display:block; padding-top:15px; text-align:center; text-decoration:none;}
#navs li a:hover {color:black; text-decoration:underline;}

</style>

<script type="text/javascript"><!--
// -->
</script>
<link rel="shortcut icon" href="favicon.ico" /><!-- path to your favicon -->
</head>
<body>
<div id="wrapper">

<div id="main">

<ul id="navs">
<li title="Your YELLOW link" style="position:absolute; top:56px; left:-70px; width:90px; height:45px;">
<a href="#" style="height:45px; width:90px;">Yellow LINK</a></li>

<li title="Your RED link" style="position:absolute; top:115px; width:118px; height:30px; left:-100px;">
<a href="#" style="height:30px; width:118px;">RED LINK</a></li>

<li title="Your BLUE link" style="position:absolute; top:183px; left:-70px; width:90px; height:38px;">
<a href="#" style="width:90px; height:38px;">BLUE LINK</a></li>
</ul>

<div id="content" style="position:absolute; left:90px; top:88px; width:397px; height:428px; background-color:#3d4b91">
<iframe name="content" src="http://www.freewebs.com/amosharper/playsforkidz.com/home.htm" frameborder="0" style="width:397px; height:428px; background-color:#3d4b91">
</iframe></div>

</div><!-- end MAIN -->

</div><!-- end wrapper -->
</body>
</html> Looks the same for IE6 and Firefox. Validates (to a degree). I would add the background-color #3d4b91 to the content of the iFRAME "home.htm", to get rid of that white-splash that occurs when the page loads. That is the iFRAME's content, which apparently has a white (or default?) background color.

Questions? Let me know. :)

WebJoel
12-12-2007, 09:31 AM
..I would add the background-color #3d4b91 to the content of the iFRAME "home.htm",.. Correction: this might not get rid of that 'white splash' that occurs. That is the background-image itself, which has a large gray 'hole' in it and gets painted-over by the iFRAME when it resolves. -I'd just go ahead and bucket-fill that gray box of background-image with #3d4b91 and be done with it. And still use #3d4b91 background-color on home.htm

amosharper
12-12-2007, 10:59 AM
Correction: this might not get rid of that 'white splash' that occurs. That is the background-image itself, which has a large gray 'hole' in it and gets painted-over by the iFRAME when it resolves. -I'd just go ahead and bucket-fill that gray box of background-image with #3d4b91 and be done with it. And still use #3d4b91 background-color on home.htm
Yeah, I will do this once I've finished making the site; I wanted to be sure it was positioned exactly before I made it the same blue as the rest.

WebJoel, thanks for your (huge) adjustments. I'll fiddle around with the structure so it's a little easier for me to digest ;) and get back to you if and when I have any problems.

Thanks very much,

- Amos

WebJoel
12-12-2007, 02:15 PM
No problem. Only took about 10-mins for that.
What would be good at some point is to cut-up that image into 3 or 4 sections and position just those, and even though it's more http-calls for images, the total image-weight could be reduced by 66% or more. What we have right now is wasted image behind actual content (the IFRAME stuff). Removing that, as it serves no purpose except slower download times.
Optimizing the images, -more KB-size reduction. :)

amosharper
12-12-2007, 02:55 PM
I've made the CSS a little more legible (the way I like it ;)) and re-introduced the pictures, and it all seems to work very smoothly. Thanks.

amosharper
12-12-2007, 04:15 PM
Hi WebJoel,

The website is getting close to finality and I was wondering how easy it would be to split the image up?

http://www.freewebs.com/amosharper/playsforkidz.com

- Amos

WebJoel
12-13-2007, 07:31 AM
I've got your new code and image on my desktop. I'll be away from the computer most of today, but will return to this later this evneing.
-How necessary is this image to be *png? If it's 256 colors or less (I highly suspect that it is), a *gif might be an additional size-reduction.
I could do both (*png and *gif) image-background and positions.

Shouldn't be too big a chore.

I'll do a size-comparison too, to demonstrate the pageweight reduction. :)

(kewl-looking site) :D

amosharper
12-13-2007, 08:54 AM
Saving as a .gif brings it from 13.9kb to 12.4kb. There is no noticeable colour difference whatsoever. Thanks for spotting that ;)

WebJoel
12-13-2007, 11:27 AM
-That entire image is under 13K? Wow... -I'd have guessed it close to twice or 3X that. But my intention to slice-apart and position sections over a grand-unified background-color will certainly draw this smaller yet.. :D

amosharper
12-13-2007, 11:31 AM
As long as you think it's worth it ;) The blue space behind the iframe takes up 1.35kb (png) or 670 bytes (gif).

WebJoel
12-14-2007, 01:57 PM
Actually you're right... the image is KB-small enough to not really warrant slicing-up sections. Advantage *png. Had this been *png, we'd have some size problem, eh? :)