[advanced search]
 

Go Back   NamePros.com > Discussion > Web Design & Development > Programming

Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics.


Closed Thread
 
LinkBack Thread Tools
Old 12-19-2006, 05:16 PM   #1 (permalink)
New Member
 
Join Date: Nov 2006
Posts: 12
0.00 NP$ (Donate)

VaporAction is an unknown quantity at this point


Centering with margin: auto

I'm new to HTML and CSS...I'm trying to center content on the page so that it will remain centered even when the window is resized...I've tried using margin: auto but that isn't working...
I'm using Firefox, not IE so it's not the browser...

the code runs something like this...
HTML....

<div id="centeredContent">blahblahblah</div>

CSS....

#centeredContent {position:fixed; top: 30px; width: 300px; margin: auto;}
VaporAction is offline  
Old 12-19-2006, 05:24 PM   #2 (permalink)
Dan
Buy my domains.
 
Dan's Avatar
 
Join Date: Feb 2006
Posts: 2,801
56.00 NP$ (Donate)

Dan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant future

Autism Autism Autism Autism Autism Autism Autism
Do you have a doctype set?

If not, use
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 profile="http://gmpg.org/xfn/11">
for the first 3 lines of your code.
Dan is offline  
Old 12-19-2006, 06:12 PM   #3 (permalink)
New Member
 
Join Date: Nov 2006
Posts: 12
0.00 NP$ (Donate)

VaporAction is an unknown quantity at this point


yep...

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
VaporAction is offline  
Old 12-19-2006, 07:05 PM   #4 (permalink)
A Wealth of Knowledge
 
stscac's Avatar
 
Join Date: Aug 2004
Posts: 3,794
47.60 NP$ (Donate)

stscac has much to be proud ofstscac has much to be proud ofstscac has much to be proud ofstscac has much to be proud ofstscac has much to be proud ofstscac has much to be proud ofstscac has much to be proud ofstscac has much to be proud of


CSS is below
HTML Code:
margin-left: auto; margin-right: auto
-Steve
__________________
Follow Me on Twitter:
stscac is offline  
Old 12-19-2006, 08:09 PM   #5 (permalink)
Dan
Buy my domains.
 
Dan's Avatar
 
Join Date: Feb 2006
Posts: 2,801
56.00 NP$ (Donate)

Dan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant future

Autism Autism Autism Autism Autism Autism Autism
margin: auto; sets all sides to margin: auto; and since the top and bottom can't be, it's the same as just doing what you did or margin: 0 auto;

VaporAction, can you show us the page?
Dan is offline  
Old 12-20-2006, 06:17 AM   #6 (permalink)
New Member
 
Join Date: Nov 2006
Posts: 12
0.00 NP$ (Donate)

VaporAction is an unknown quantity at this point


here's the link to the page...

http://www.karlyoder.com/wiredchemist/index.html
VaporAction is offline  
Old 12-20-2006, 07:06 AM   #7 (permalink)
Munky Designs
 
Join Date: May 2005
Posts: 997
417.00 NP$ (Donate)

Albino is a jewel in the roughAlbino is a jewel in the roughAlbino is a jewel in the rough


im not sure if this will help, but:

body{
text-align:center;
}

#centeredcontent{
margin-left:auto;
margin-right:auto;
}


that will make the content be centered, although it may not be exactly what you want.
Albino is offline  
Old 12-20-2006, 07:42 AM   #8 (permalink)
Gaz
NamePros Member
 
Join Date: Dec 2006
Location: Plymouth
Posts: 27
5.00 NP$ (Donate)

Gaz is an unknown quantity at this point


Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>

<head><title>Wired Chemist</title>
<style type="text/css">

body {
	padding: 0;
	margin: 0;
} 

#navText {
	text-align: center;
	font-family: Arial, Helvetica, sans-serif;
	color: #4682B4;
	width: 100%;	
	background: #b0c4de;
	margin-top: -30px;
	left: 0;
}

#navH3 {
	font-size: 30px;
	top: 20px;
}
</style>
</head>
<body>
	<div id="navText">
		<h3 id="navH3">Wired Chemist</h3>
		<p id="navP">Chemistry | Mineralogy | Environmental | NMR<br><br>Instructional | Data Tables | Links</p>
	</div>
</body>
</html>
this works better imo.
Gaz is offline  
Old 12-20-2006, 07:49 AM   #9 (permalink)
New Member
 
Join Date: Nov 2006
Posts: 12
0.00 NP$ (Donate)

VaporAction is an unknown quantity at this point


shouldn't margin: auto simply work?
I'm not using IE so I don't need to do the text-align: center...

why the $#@$% isn't margin: auto working????
VaporAction is offline  
Old 12-20-2006, 07:54 AM   #10 (permalink)
Gaz
NamePros Member
 
Join Date: Dec 2006
Location: Plymouth
Posts: 27
5.00 NP$ (Donate)

Gaz is an unknown quantity at this point


text-align: center; is aligning your text in the centre of the div which is at 100% width. Margin: auto, will assign the left and right margins an equal ammount, but also the top and bottom one, margin: 0px auto; will assign your top and bottom margin's 0 and your left and right ones auto.

Before you can use these techniques, you must first understand how they work.
Gaz is offline  
Old 12-20-2006, 08:33 AM   #11 (permalink)
New Member
 
Join Date: Nov 2006
Posts: 12
0.00 NP$ (Donate)

VaporAction is an unknown quantity at this point


finally figured out the problem...
margin: auto doesn't work with positioning...
which ***** cause I want to have an element whose positioning is fixed vertically (a top navigation bar that will stay fixed as the user scrolls down) but whose width stays centered during resize....anybody know how to do this without resorting to frames?

I tried using width: 100% and text-align: center but multiple lines of text get unaligned with each other if the window is resized...
VaporAction is offline  
Old 12-20-2006, 08:48 AM   #12 (permalink)
Gaz
NamePros Member
 
Join Date: Dec 2006
Location: Plymouth
Posts: 27
5.00 NP$ (Donate)

Gaz is an unknown quantity at this point


Quote:
Originally Posted by VaporAction
finally figured out the problem...
margin: auto doesn't work with positioning...
which ***** cause I want to have an element whose positioning is fixed vertically (a top navigation bar that will stay fixed as the user scrolls down) but whose width stays centered during resize....anybody know how to do this without resorting to frames?

I tried using width: 100% and text-align: center but multiple lines of text get unaligned with each other if the window is resized...
Place all your content in #content
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>

<head><title>Wired Chemist</title>
<style type="text/css">

body {
	padding: 0;
	margin: 0;
} 

#navText {
	text-align: center;
	font-family: Arial, Helvetica, sans-serif;
	color: #4682B4;
	width: 100%;	
	background: #b0c4de;
	margin-top: -30px;
	left: 0;
	position: fixed;
}

#navH3 {
	font-size: 30px;
	top: 20px;
}

#content {
	width: 800px;
	padding-top: 140px;
	margin-left: auto;
	margin-right: auto;
}
</style>
</head>
<body>
	<div id="navText">
		<h3 id="navH3">Wired Chemist</h3>
		<p id="navP">Chemistry | Mineralogy | Environmental | NMR<br><br>Instructional | Data Tables | Links</p>
	</div>
	<div id="content">
	blah
	<br>
	<br>
	<br>
	<br>
	<br><br>
	<br><br><br>
	<br><br><br>blah
	</div>
</body>
</html>
thought u said it doesn't work
Gaz is offline  
Old 12-21-2006, 06:21 AM   #13 (permalink)
New Member
 
Join Date: Nov 2006
Posts: 12
0.00 NP$ (Donate)

VaporAction is an unknown quantity at this point


I said the nav bar doesn't work with margin: auto because it needs to have fixed positioning...the content works fine with margin: auto because it isn't fixed...
VaporAction is offline  
Old 12-21-2006, 03:31 PM   #14 (permalink)
Gaz
NamePros Member
 
Join Date: Dec 2006
Location: Plymouth
Posts: 27
5.00 NP$ (Donate)

Gaz is an unknown quantity at this point


can you not see i have fixed your problem?
you tried to over complicate something. which is now fixed...
Gaz is offline  
Closed Thread


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

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
Trackbacks are On
Pingbacks are On
Refbacks are On


Site Sponsors
Advertise your business at NamePros

All times are GMT -7. The time now is 04:09 PM.


Powered by: vBulletin® Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0
Template-Modifications by TMS
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85