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 05-03-2009, 11:30 PM   PM User | #1
hog_mus_stream
New Coder

 
Join Date: May 2009
Posts: 20
Thanks: 3
Thanked 0 Times in 0 Posts
hog_mus_stream is an unknown quantity at this point
centered div layer(s)

i understand that this question came maybe million times
but i assure you all that i googled like idiot
compared my code with others and i dont have luck at all...
i started to learn about div layers because i wanted to throw away tabled layout...

so my problem is:
i have 3 DIV's, 1st is main that contain other 2
2nd is on the left side with content
3rd is on right side of 2nd
---
i just want that those div layers all be centered on any resolution
bigger than 1024x768

i made pictures of what i want if i sound stupid :P

- orange is main layer containing other 2
- blue & green are other 2


my wish:


---------------

this is my humble code, i tried to put some logic in it but no luck...
Code:
<style type="text/css">
body {
	background-color: #CCCCCC;
	margin-left: 0px;
	margin-top: 0px;
}


.main_frame {
	position: absolute;
	top: 0px;
	left: 0px;
	width:100%;
}
</style>
Code:
<div class="main_frame">
  <div style="position: absolute; top: 7px; left: 7px; width: 270px;">
       <p>some text/link</p>
       <p>another text/link</p>
       <p>yet another text/link</p>
  </div>
  <div style="position: absolute; top: 7px; left: 280px; width: 600px;">
       <p>some text/link</p>
       <p>another text/link</p>
       <p>yet another text/link</p>
  </div>
</div>
some site recommended to put in body: text-align: center;
and for rest layers: text-align: left;
this didnt work

some site recommended to put layer(s) 50% from left
but this would only "push" my content far on right and align still would
not be centered...

then i tried on stupid way:
make table with 1 row, set its align to centered and put all div's inside
but then i had trouble with positioning left and right layers where i wanted
(since everything inside table is i guess under tabular rules and is placed in center of table...)

any help would be apreciated

Last edited by hog_mus_stream; 05-03-2009 at 11:33 PM.. Reason: blah
hog_mus_stream is offline   Reply With Quote
Old 05-04-2009, 12:32 AM   PM User | #2
hog_mus_stream
New Coder

 
Join Date: May 2009
Posts: 20
Thanks: 3
Thanked 0 Times in 0 Posts
hog_mus_stream is an unknown quantity at this point
no luck :/
it still resizes toward left upper corner
hog_mus_stream is offline   Reply With Quote
Old 05-04-2009, 02:09 AM   PM User | #3
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 hog_mus_tream,
Putting margin:0 auto; on the body isn't going to do much for you.
To center an element we need 3 things:
  1. a DocType
  2. an element with a width
  3. that elements left and right margins set to auto
like I've done with your #main_frame here -
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style type="text/css">
html, body {
	font: 14px "Comic Sans MS";
	background: #ccc;
	text-align: center;
}
* { /*this zeros out default margin and padding*/
	margin: 0;
	padding: 0;
	border: none;
}
#main_frame {
	width: 880px; /*this is the total width of left and right columns*/
	margin: 0 auto; /*centers #main_frame*/
}
#left_col {
	width: 270px;
	float: left;
}
#right_col {
	margin: 0 0 0 280px;
}
</style>
</head>
<body>
	<div id="main_frame">
		<div id="left_col">
			<p>some text/link</p>
			<p>another text/link</p>
			<p>yet another text/link</p>
		<!--end left_col--></div>
			<div id="right_col">
				<p>some text/link</p>
				<p>another text/link</p>
				<p>yet another text/link</p>
			<!--end right_col--></div>
	<!--end main_frame--></div>
</body>
</html>
Notice I've also done away with your inline styles and absolute positioning.
Now you can experiment with text-align:left; on your columns.
__________________
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
Users who have thanked Excavator for this post:
hog_mus_stream (05-04-2009)
Old 05-04-2009, 08:15 AM   PM User | #4
hog_mus_stream
New Coder

 
Join Date: May 2009
Posts: 20
Thanks: 3
Thanked 0 Times in 0 Posts
hog_mus_stream is an unknown quantity at this point
wow

thank you so much, it works very nice !

i must admit that it uses (div positioning) a bit weird logic
too bad that w3c can't implement something more simple (like align: center; ) tag for divs :P

thanks again

Last edited by hog_mus_stream; 05-04-2009 at 08:28 AM.. Reason: -
hog_mus_stream is offline   Reply With Quote
Old 05-04-2009, 09:11 AM   PM User | #5
abduraooft
Supreme Master coder!

 
abduraooft's Avatar
 
Join Date: Mar 2007
Location: N/A
Posts: 14,689
Thanks: 158
Thanked 2,184 Times in 2,171 Posts
abduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really nice
Code:
html, body {
	font: 14px "Comic Sans MS";
	background: #ccc;
	text-align: center;
}
FYI, the above text-align:center is not required, if you are not concerned about the very old browsers like IE5 and previous versions.
__________________
Quote:
The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)
abduraooft 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 11:45 AM.


Advertisement
Log in to turn off these ads.