...

View Full Version : Centering Page in Browser



Mike521266
11-22-2010, 08:57 PM
Hi, I'm not really a coder, but I am trying to do something in dreamweaver that I can't figure out how to do. I have spent plenty of time trying to figure it out, and I've posted my code below. Basically, I just want my page to center in any browser, but at this point, it is aligning on the left side in both Firefox and IE8. Could anyone tell me what I'm doing wrong?

Here is the code:



<!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" />
<title>Untitled Document</title>
<style type="text/css">
body {
text-align: center;
}
#container {
margin: 0 auto 0 auto;
width: 1000px;
height:1000px;
text-align: left;
}
</style>
</head>
<style type="text/css">


#Layer1 {
position:absolute;
width:17px;
height:12px;
z-index:1;
left: 492px;
top: 22px;
}
#Layer2 {
position:absolute;
width:409px;
height:263px;
z-index:2;
left: 20px;
top: 21px;
}
#Layer3 {
position:absolute;
width:16px;
height:11px;
z-index:3;
left: 979px;
top: 22px;
}
</style>
<body>
<div id="container">
<div id="Layer1">
<p>Center</p>
</div>
<div id="Layer2"><img src="imagesunder_construction.jpg" width="451" height="335" /></div>
<div id="Layer3">Right</div>

</div>
</body>
</html>



Here is the page that shows up:

http://heritagelandscaping.ca/12345.html



Thanks in advance for your help. Mike

teedoff
11-22-2010, 09:03 PM
Your container div is centered....give it a thin solid red border and you will see.

I think you probably closed the container div too soon. It should be closed right before the closing body tag.

Mike521266
11-22-2010, 09:19 PM
Okay, I see what you're saying... I added a border, and I see the container is centered. However, I don't understand why the layers aren't within the container. I assume it's something to do with the code below, but I can't seem to find the problem. I did close the container div right before the closing body tag. Can you see anything wrong with this?


<body>
<div id="container">
<div id="Layer1">
<p>Center</p>
</div>
<div id="Layer2"><img src="imagesunder_construction.jpg" width="451" height="335" /></div>
<div id="Layer3">Right</div>

</div>
</body>
</html>

Excavator
11-22-2010, 09:25 PM
Hello Mike521266,
Your containing div, #container, needs to be position: relative; so your absolute positioned elements stay with their container.

You also have #Layer2 set at width:409px;height:263px; but the image in it is much bigger at width:451px;height:335px;

I prefer putting a background color on divs for testing. If you use a border you stand a good chance of inadvertantly wrecking your box model (http://www.w3.org/TR/CSS2/box.html).

Try some of these changes to your CSS -
body {
text-align: center;
background: #fc6;
}
#container {
margin: 0 auto 0 auto;
width: 1000px;
height:1000px;
text-align: left;
background: #ff0;
position: relative;
}

#Layer1 {
position:absolute;
width:17px;
height:12px;
z-index:1;
left: 492px;
top: 22px;
background: #00f;
}
#Layer2 {
position:absolute;
width:451px;
height:335px;
z-index:2;
left: 20px;
top: 21px;
}
#Layer3 {
position:absolute;
width:16px;
height:11px;
z-index:3;
left: 979px;
top: 22px;
background: #ccc;
}


It's really too bad DreamWeaver seems to steer new coders toward using absolute positioning. Have a quick read on the Pitfalls of AP (http://www.tyssendesign.com.au/articles/css/absolute-positioning-pitfalls/).
It would be much better to float all your divs instead.

Like this -
body {
text-align: center;
background: #fc6;
}
#container {
margin: 0 auto 0 auto;
width: 1000px;
/*height:1000px;*/
overflow: auto; /*to clear the floats*/
text-align: left;
background: #ff0;
}

#Layer1 {
width: 150px;
/*height:12px;*/
float: left;
background: #00f;
}
#Layer2 {
width:451px;
float: left;
}
#Layer3 {
width:250px;
float: left;
background: #ccc;
}


And last, when posting code in the forum, please use the code tags, &#91;code&#93;&#91;/code&#93; - available with the # button in the post edit window.
This will wrap your code in a scroll box which greatly helps the readability of your post.

Mike521266
11-22-2010, 09:34 PM
Great, thanks a lot, that worked. I am still working through figuring out all that you are trying to tell me in the second part of the reply, so if I have any questions, I may ask, but you did help me fix the problem I had, so thank you very much. Mike

Excavator
11-22-2010, 10:01 PM
Awesome. Always glad to help :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum