Link to home
Start Free TrialLog in
Avatar of Belazir
Belazir

asked on

Centering website using DIV absolute layout

I'm using DIVs to set my web page layout, using the position: absolute element.  I can't get the website to center if I do this though, and if I use float instead the background flows round the edge of the layout, which isn't what I want.  I've attached both example css and jsp, can anyone help?
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
	<title>leagues ahead : test</title>
	<link rel="stylesheet" type="text/css" href="test.css" />
</head>
<body>
	<div id="header"><img src="Logo_small.jpg" alt="Leagues Ahead"></div>
	<div id="leftcol">Left Section</div>
	<div id="shortmenu">Short menu</div>
	<div id="content">Content Section</div>
	<div id="footer">Footer Section</div>
</body>
</html>
 
@CHARSET "ISO-8859-1";
<style type="text/css">
<!--
body {
	background-position: center;	
	background-color: #EEEEEE;
}
#header {
  background: white;
  position: absolute;
  top: 0px;
  left: 0px;
  width: 800px;
  height: 125px;
}
#leftcol {
  background: #106070;
  position: absolute;
  top: 125px;
  left: 0px;
  width: 150px;
  height: 450px;
}
#shortmenu {
	background: #88b050;
	position: absolute;
	left: 150px;
	width: 650px;
	top: 125px;
	height: 25px;
	border-left-width: 1px;
	border-left-style: solid;
	border-left-color: white;
}
#content {
  background: #fff;
  position: absolute;
  top: 150px;
  left: 150px;
  width: 650px;
  height: 425px;
  padding-left: 6px;
  padding-top: 3px; 
}
#footer {
	background: #106070;
	position: absolute;
	top: 575px;
	left: 0px;
	width: 800px;
	height: 25px;
	border-top: 1px solid white;
}
-->
</style>

Open in new window

Avatar of Graham Hirst
Graham Hirst
Flag of United Kingdom of Great Britain and Northern Ireland image

add into the containing div's

margin:0px auto;
cover all the elements in another DIV like this:
<body>
 <div>
   your DIV here
 </div>
</body>

and the covered div style:
height:100%;
position:absolute;
left:20%;
width:auto;

let me know how it does!
ASKER CERTIFIED SOLUTION
Avatar of codeQuantum
codeQuantum
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Just remember when coding in HTML : Use simple tags for simple things. Only use more advanced stuff if you need to do something very specific that can't be done with simple commands.

Centering a page is very simple stuff. You don't need to use absolute or float at all, and trying to do so will just make things a lot harder. (Or even impossible!)
>>The mentioned solutions won't work. When you use absolute positioning, it means you are placing your elements according to the absolute size of the window and you do not inherit any positions from the containing elements or divs.

Absolutely false!!! position:absolute; left indicates its relative to the wrapper! In the wrapper's offset width,height its absolute positioning, not on the entire window!! Can you set up a small test page and prove it?

Sure, have a look at this code :
<html>
<head>
<style type="text/css">
.goright {
	text-align: right;
	width: 200px;
}
.goleft
{
width: 200px;
position:absolute;
left:0px;
top:150px
}
</style>
</head>
 
<body>
<div class="goright">
<h4 class="goleft">Absolute positionnnig tells this heading to go left, while its containing element tells it to go right. <br><br>
As you can see, absolute positionning completely ignore the containing element. (There are some exceptions.)</h4>
</div>
</body>
</html>

Open in new window

Oh my bad. Just noticed you absolutely positioned the container in your code... yeah that would work!!

Mea culpa, sorry ram_0218.
Avatar of icedesignstudios
icedesignstudios

Here is a short snippet of code that will centre a div that is 500 pixels wide:

If you modify it, it should do just what you're looking for.
<style type="text/css">
<!--
#centre {
	background-color: #000000;
	height: 500px;
	width: 500px;
        position: absolute;
	left: 50%; /* You move the left side of the container to the middle of the screen */
	margin-left: -250px; /* Then you pull it back by subtracting 1/2 of its width */
}
-->
</style>

Open in new window

not to interfere with the above experts, but seems to me --

<DIV align = "center" > 
 .. other stuff ...
<DIV align = "center">
   ... what you want to center
</DIV>
  ... other stuff
</DIV>

would do it simply, no ?
scrathcyboy: See my reply earlier. Since it is absolutely positioned it will ignore align="center" and use the pixel coordinates instead. (It only take into account other absolutely positioned elements, and that is one of the exceptions I mentioned in my code example.)
" I can't get the website to center "

Well if that's all he wants to do, leave the align = center, and remove absolute and replace it with relative -- that should work.  

This is a classic example of where absolute positioning is a disaster.  If he sets his layout to % or floating, and someone views this on a different size screen and resizes the page, everything is thrown off alignment and nothing is centered any more.
Agreed. That is what I meant when I said using absolute positioning was overkill in this situation.
Added below is the fixed code for your problem. Please check it.

Regards

Sajay
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
	<title>leagues ahead : test</title>
	<link rel="stylesheet" type="text/css" href="test.css" />
<style>
<!--
body {	
	background-color: #EEEEEE;
}
 
#container {
	margin:0px auto;
	width:800px;
	height:100%;
}
 
#header {
  background: white;
  float:left;
  width:800px;
  height: 125px;
}
#leftcol {
  background: #106070;
  float:left;
  width: 150px;
  height: 450px;
}
#shortmenu {
	background: #88b050;
	float:left;
	width: 649px;
	height: 25px;
	border-left-width: 1px;
	border-left-style: solid;
	border-left-color: white;
}
#content {
  background: #fff;
  float:left;
  width: 644px;
  height: 425px;
  padding-left: 6px;
  padding-top: 3px; 
}
#footer {
	background: #106070;
	float:left;
	width:800px;
	height: 25px;
	border-top: 1px solid white;
}
-->
</style>
</head>
<body>
	<div id="container">	
		<div id="header"><img src="Logo_small.jpg" alt="Leagues Ahead"></div>
		<div id="leftcol">Left Section</div>
		<div id="shortmenu">Short menu</div>
		<div id="content">Content Section</div>
		<div id="footer">Footer Section</div>
	</div>
</body>
</html>

Open in new window

Avatar of Belazir

ASKER

Well, I didn't expect a deluge.  It will take me a while to digest all of this so apologies if I don't get back to you for a few days.

A question though codeQ: I am using absolute positioning in order to get the layout right; I've tried using floats but I get annoying seepage of the background around the top and bottom of the main page content.  Only with absolute positioning can I get this to work.  I did prefer not to use this but if I don't, how do I get the top of my header DIV to actually be at the top of the window rather than some 10 or 15 pixels lower?  If I can figure that, I can revert to using floats and then I gather from the above this all becomes rather easier.

thanks
B
you just assign to the DIV the same attributes as any other element --

DIV.center {  margin:0px; padding:0px overflow:visible; }  etc

<DIV class="center" >   etc/
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Belazir

ASKER

Hi all

Many of your answers had good suggestions but codeQuantum's was the most thorough and was the one that helped me work this one through, I hope you're happy for me to award the points there rather than trying to split them 6 different ways.

Thanks for the plethora of useful tips.
B