Solved

Centering website using DIV absolute layout

Posted on 2008-06-24
17
1,806 Views
Last Modified: 2010-05-18
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

0
Comment
Question by:Belazir
  • 7
  • 3
  • 2
  • +4
17 Comments
 
LVL 4

Expert Comment

by:FSIFM
Comment Utility
add into the containing div's

margin:0px auto;
0
 
LVL 17

Expert Comment

by:ram_0218
Comment Utility
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!
0
 
LVL 5

Accepted Solution

by:
codeQuantum earned 250 total points
Comment Utility
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.

There are many problems with your code... like @CHARSET "ISO-8859-1"; that is not well placed, and the div positionning that is flawed, and you are using XHTML style tag closing while you declared HTML 4... However, since this question is about centering, I will concentrate on solving this specific problem.

1) You won't be able to center things if you use absolute anywhere in your code. Use relative or don't specify a style at all.
2) check this link for the best way to center a page : http://www.maxdesign.com.au/presentation/center/
0
 
LVL 5

Expert Comment

by:codeQuantum
Comment Utility
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!)
0
 
LVL 17

Expert Comment

by:ram_0218
Comment Utility
>>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?

0
 
LVL 5

Expert Comment

by:codeQuantum
Comment Utility
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

0
 
LVL 5

Expert Comment

by:codeQuantum
Comment Utility
Oh my bad. Just noticed you absolutely positioned the container in your code... yeah that would work!!

Mea culpa, sorry ram_0218.
0
 

Expert Comment

by:icedesignstudios
Comment Utility
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

0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 44

Expert Comment

by:scrathcyboy
Comment Utility
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 ?
0
 
LVL 5

Expert Comment

by:codeQuantum
Comment Utility
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.)
0
 
LVL 44

Expert Comment

by:scrathcyboy
Comment Utility
" 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.
0
 
LVL 5

Expert Comment

by:codeQuantum
Comment Utility
Agreed. That is what I meant when I said using absolute positioning was overkill in this situation.
0
 
LVL 4

Expert Comment

by:sajay_j
Comment Utility
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

0
 

Author Comment

by:Belazir
Comment Utility
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
0
 
LVL 44

Expert Comment

by:scrathcyboy
Comment Utility
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/
0
 
LVL 5

Assisted Solution

by:codeQuantum
codeQuantum earned 250 total points
Comment Utility
Afaik you need to remove margins from your page. Try this code in your "body" along with your float placement and tell us if it works. (This is the usual suspect, but there are other possibilities.)
body {

        background-position: center;    

        background-color: #EEEEEE;

  margin: 0;

  padding: 0;

}

Open in new window

0
 

Author Comment

by:Belazir
Comment Utility
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
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…
In this tutorial viewers will learn how to embed custom externally-hosted Google Fonts using the Google Font API in CSS Go to the Google Fonts website at google.com/fonts: Browse or search based on font properties or name to find a suitable font for…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now