Go Back   CodingForums.com > :: Client side development > HTML & CSS

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-22-2010, 07:57 PM   PM User | #1
Mike521266
New to the CF scene

 
Join Date: Nov 2010
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Mike521266 is an unknown quantity at this point
Centering Page in Browser

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
Mike521266 is offline   Reply With Quote
Old 11-22-2010, 08:03 PM   PM User | #2
teedoff
Senior Coder

 
Join Date: Aug 2010
Location: High Point, NC
Posts: 3,325
Thanks: 5
Thanked 363 Times in 360 Posts
teedoff is on a distinguished road
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.
teedoff is offline   Reply With Quote
Old 11-22-2010, 08:19 PM   PM User | #3
Mike521266
New to the CF scene

 
Join Date: Nov 2010
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Mike521266 is an unknown quantity at this point
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>
Mike521266 is offline   Reply With Quote
Old 11-22-2010, 08:25 PM   PM User | #4
Excavator
Master Coder


 
Excavator's Avatar
 
Join Date: Dec 2006
Location: Alaska
Posts: 9,410
Thanks: 22
Thanked 1,765 Times in 1,749 Posts
Excavator has a spectacular aura aboutExcavator has a spectacular aura aboutExcavator has a spectacular aura about
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.

Try some of these changes to your CSS -
Code:
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.
It would be much better to float all your divs instead.

Like this -
Code:
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, [code][/code] - 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.
__________________
Validate often DURING development - Use it like a splelchecker | Debug during Development |Write it for FireFox, ignore IE
Use the right DocType | Validate your markup | Validate your CSS | Why validating is good | Why tables are bad
Excavator is offline   Reply With Quote
Old 11-22-2010, 08:34 PM   PM User | #5
Mike521266
New to the CF scene

 
Join Date: Nov 2010
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Mike521266 is an unknown quantity at this point
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
Mike521266 is offline   Reply With Quote
Old 11-22-2010, 09:01 PM   PM User | #6
Excavator
Master Coder


 
Excavator's Avatar
 
Join Date: Dec 2006
Location: Alaska
Posts: 9,410
Thanks: 22
Thanked 1,765 Times in 1,749 Posts
Excavator has a spectacular aura aboutExcavator has a spectacular aura aboutExcavator has a spectacular aura about
Awesome. Always glad to help
__________________
Validate often DURING development - Use it like a splelchecker | Debug during Development |Write it for FireFox, ignore IE
Use the right DocType | Validate your markup | Validate your CSS | Why validating is good | Why tables are bad
Excavator is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:08 PM.


Advertisement
Log in to turn off these ads.