| |||||||
| Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics. |
![]() |
| | LinkBack | Thread Tools |
| | #1 (permalink) |
| New Member | 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;} |
| |
| | #2 (permalink) |
| Buy my domains. | 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"> |
| |
| | #4 (permalink) |
| A Wealth of Knowledge | CSS is below HTML Code: margin-left: auto; margin-right: auto
__________________ Follow Me on Twitter: |
| |
| | #8 (permalink) |
| NamePros Member | 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>
__________________ dramaticRED - My Online Portfolio |
| |
| | #10 (permalink) |
| NamePros Member | 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.
__________________ dramaticRED - My Online Portfolio |
| |
| | #11 (permalink) |
| New Member | 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... |
| |
| | #12 (permalink) | |
| NamePros Member | Quote:
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>
__________________ dramaticRED - My Online Portfolio | |
| |
| | #14 (permalink) |
| NamePros Member | can you not see i have fixed your problem? you tried to over complicate something. which is now fixed...
__________________ dramaticRED - My Online Portfolio |
| |
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| |