Click to See Complete Forum and Search --> : Margin: Auto;


matt1776
05-31-2006, 12:43 PM
I recently picked up a book entitled CSS: Cascading Style Sheets for Web Design by Richard York.

In the section entitled margins there is a line that reads:

Documented in CSS 2.1 and supported in IE 5.5, IE 6.0, Mozilla 1.7, Opera 7.5, and Safari 1.2

i am looking at my webpage in IE 6. He describes a property called margin: auto; that is supposed to center the element in the browser. However the element is not centered in IE 6. It works fine in FF but not IE. How can this be right? Did I pick up the wrong CSS book? Ive had this problem with CSS everytime ive made a serious effort to learn it, which is why i used/use tables for my layouts. im trying to do the right thing by learning css but noone has made it very easy or straightforward.

the page can be found at http://www.doublethinkdesigns.com/test.php

The style code is in the source.

Charles
05-31-2006, 12:49 PM
It only works in valid documents. That's not a valid document.

http://validator.w3.org/

The Little Guy
05-31-2006, 12:53 PM
change this:
margin: 25px auto;
To this
margin:auto;

WebJoel
05-31-2006, 01:10 PM
You must be changing your html online, live. -The page chaged twice while I was looking at it... (refreshed, and saw big differences...)

The <li>s need <ul> at the beginning, and </ul> at the end. This would look good too, if you added style="list-style-type:none; display:inline;" to the UL (no little black bullets, and it would display horizontally not vertically) :)


edit:re: display:inline;

Disregard. -Looks as if you want to keep the navigation at the left-side of the page, not centered under banner. :)

matt1776
05-31-2006, 01:11 PM
ok so ive specified the doctype and the char encoding

why is my navigation element over to the right hand side of the page? again ive followed the instructions in the book.

matt1776
05-31-2006, 01:13 PM
and another thing, in FF the header image is transparent, as it should be cause thats how i saved it. in IE it has some grey haze in the background, why is that?

WebJoel
05-31-2006, 01:16 PM
ok so ive specified the doctype and the char encoding

why is my navigation element over to the right hand side of the page? again ive followed the instructions in the book.

Get rid of the 'nested divitis'. Although not in error, you do not need to do:

<div>
<div id="left">
stuff stuff stuff
</div>
</div>
(example, -not your actual code)

because if you are styling the id "left", it is ineffectual as written, because it is inside of another DIV. Elminate that outer DIV to let the inner DIV obey the CSS.

Also:

<div>

<img src="pics/header.png" alt="Header: Double Think Designs">

<div class="navigation">

<ul>
<li><a href="">Home</a>
<li><a href="">About</a>
<li><a href="">Services</a>
<li><a href="">Contact</a>

</ul>

</div>
</div>

You have your banner image (across top of page) and navigation (left-side, below banner) in the SAME div.
Make your banner be by itself, start a new DIV for navigation stuff.

matt1776
05-31-2006, 01:20 PM
but if i dont nest the divs how will i have the left side of the box match up with the left side of the header div?

im so used to thinking in terms of table layouts.. :((

WebJoel
05-31-2006, 01:26 PM
but if i dont nest the divs how will i have the left side of the box match up with the left side of the header div?

im so used to thinking in terms of table layouts.. :((

Correctly. That's how. :) Andyeah, ditch the TABLE way of thinking. heheee

Give the navigation DIV the SAME margin-left value as the banner. That way the two will always be the same distance from left-side of user's screen. :)

matt1776
05-31-2006, 01:29 PM
im sorry but could you give me an example?

im using margin auto to center the header element, i wanted to avoid specifing the distance from the outer edge of the browser because i wanted it dynamically spaced.

WebJoel
05-31-2006, 01:57 PM
but if i dont nest the divs how will i have the left side of the box match up with the left side of the header div?

im so used to thinking in terms of table layouts.. :((

Correctly. That's how. :) Andyeah, ditch the TABLE way of thinking. heheee

Give the navigation DIV the SAME margin-left value as the banner. That way the two will always be the same distance from left-side of user's screen. :)

The Little Guy
05-31-2006, 02:04 PM
Preview: http://d-top.org/a/thing.html

<style type="text/css">
body{
margin:0;
padding:0;
}
.container{
width:950px;
border:none;
background-color:none;
margin:auto;
margin-top:50px;
}
div{
border:1px solid #000000;
}
.headimg {
Background-color: #66CC99;
width: 950px;
}
.navigation {
Background-color: #66CC99;
font: italic small-caps bold 14px sans-serif;
line-height: 25px;
letter-spacing: 3px;
margin-top:20px;
margin-right: auto;
width: 225px;
padding: 10px;
border-bottom-width: 1px;
margin-bottom:20px;
}

.rightSide {
Background-color: #66CC99;
font: italic small-caps bold 14px sans-serif;
width: 225px;
padding: 10px;
border-bottom-width: 1px;
}
</style>

<div class="container">
<div class="headimg">
<img src="pics/header.png" alt="Double Think Designs">
</div>
<div class="navigation">
Home<br>
About<br>
Services<br>
Contact<br>
</div>
<div class="rightSide">
Side content goes here. This should be positioned directly below the navigation pane. If not then i did not configure the stylesheet tag correctly. Either way, I will figure it out sooner than later.
</div>
</div>

WebJoel
05-31-2006, 02:15 PM
Nicer!

I think I'd make the branding a bit larger (the company name), and maybe use a nice stylish font in <h1>header text</h1>, add some letter-spacing:1.3ex; to the <h1 style=""> or something (this spaces the lettering horizontally, adding impact). Maybe make the banner div a bit 'taller'. But yeah, -coming along nicely.

You could also condense your code a bit, a few examples:

.container{
width:950px;
border:none;
background-color:none;
margin:auto;
margin-top:50px;
}

could also be written as margin:50px auto; and say the same thing,

and farther down:

.navigation {
Background-color: #66CC99;
font: italic small-caps bold 14px sans-serif;
line-height: 25px;
letter-spacing: 3px;
margin-top:20px;
margin-right: auto;
width: 225px;
padding: 10px;
border-bottom-width: 1px;
margin-bottom:20px;
}

could be written as "margin:20px auto 20px;" and also say the same thing.
This one, I wonder if you'd wanted to auto-adjust the left side too? If so, then it's "margin:20px auto 20px auto;" (tht's TOP-RIGHT-BOTTOM-LEFT), and you've just centered your page 20-pixels from the top and the bottom, and auto-centered the right and the left margins as well (centering the page).
-CSS is the bomb! ;)

matt1776
06-04-2006, 08:20 PM
could be written as "margin:20px auto 20px;" and also say the same thing.
This one, I wonder if you'd wanted to auto-adjust the left side too? If so, then it's "margin:20px auto 20px auto;" (tht's TOP-RIGHT-BOTTOM-LEFT), and you've just centered your page 20-pixels from the top and the bottom, and auto-centered the right and the left margins as well (centering the page).
-CSS is the bomb! ;)

Joel, this does not work. I checked with the book im using and it seems to be the correct syntax, but it is not working correctly in either IE or FF

ray326
06-04-2006, 08:38 PM
I was going to look at it but it's got that freakin' meta refresh on it.

WebJoel
06-05-2006, 08:01 AM
This works. It could be 'cleaner' (I have some redundant coding in here and could flesh it down some), but it is how I think you're trying to do this.
I GOT RID OF that doggone meta-refresh tag... anyone whom sees this stuff long enough does not need the mind-phreak, and "10" (seconds) is a good value for meta-refresh... it's about as long as I'd stay on a URL that 'refreshes'... :(

It is also difficult to understand what you're trying to do, you have a DIV called "rightSide" (so one assumes that you want to 'to the right'??), but the text says that it's the "...Side content goes here. This should be positioned directly below the navigation pane...", which to the LEFT, and besides, -you'd want your "content" to be in the middle and/or right, yes?
Anyway, that is how I interpret your intent. :)

Here is the revised code (with STRICT !DOCTYPE), and it works equally well in IE and Firefox:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<title>Test Page</title>
<head>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=ISO-8859-1" />

<style type="text/css">
body, html {margin: 0px;
padding: 0px;
border:0;}
body {margin-bottom:25px;}/* required: gives 25-px space below footer */
a {text-decoration: none;
color: #000000;}
a:hover {color: #ffffff;}

li {list-style-type: circle;}
div {border: 1px solid black;}
div#container {width:98%; margin:0 auto; padding:0;}
div#content {padding:40px 30px 40px 15px; height:100%; text-align:left; border:none;}
div.header {width:98%; height:79px;
margin:25px auto 0 auto;
background-color: #99CC66;}
div.navigation {
background-color: #99CC66;
font: italic small-caps bold 14px sans-serif;
float:left;
clear:left;
margin:0 15px auto 0;
line-height: 1.5em;
letter-spacing: 0.3ex;
width: 215px;
padding: 20px;
}
div#rightSide {background-color: #99CC66;
font: italic small-caps bold 14px sans-serif;
margin:0 15px auto 0;
float:left;
clear:left;
width: 225px;
padding: 15px;
}

</style>

</head>
<body>
<div class="header">
<img src="http://www.doublethinkdesigns.com/pics/header.png" alt="Double Think Designs" style="float:left; margin:0;">
</div>

<div id="container"><!-- wraps everything below header div -->
<div class="navigation">
<ul>
<li><a href="#" title="Home">Home</a>
<li><a href="#" title="About">About</a>
<li><a href="#" title="Services">Services</a>
<li><a href="#" title="Contact">Contact</a>
</ul>
</div>
<div id="rightSide">
Side content goes here. This should be positioned directly below the navigation pane. If not then i did not configure the stylesheet tag correctly.
Either way, I will figure it out sooner than later.
</div>

<div id="content"><h1>Header tag here</h1>
<p>Some text goes in here. This is the CONTENT part of the layout.</p>
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
Suspendisse egestas ultricies pede.
<img src="images/this_pic.gif" alt="left-hand image"
style="width:85px; height:75px; margin:6px; border: 1px dotted red; float:left;" />
Phasellus suscipit blandit risus.
Praesent nonummy. In erat. Duis nibh pede, accumsan eu, pulvinar et,
volutpat vel, elit. Curabitur nec dui sed nunc congue tempus. Nulla ac
dui ac libero fringilla nonummy. Maecenas ullamcorper sodales risus.
Vivamus pretium dolor. Proin eu turpis. Phasellus ut mauris non nulla
mattis luctus.
<img src="images/this_pic.gif" alt="right-hand image"
style="width:85px; height:75px; margin:6px; border: 1px dotted red; float:right;" />
Nunc porttitor dapibus sapien. In malesuada fermentum
metus. Nulla egestas, tellus a vestibulum pharetra, nunc purus auctor
lacus, ut semper purus ipsum eu velit. Praesent dui. Nulla accumsan
turpis at erat. Phasellus suscipit blandit risus.
Praesent nonummy. In erat. Duis nibh pede, accumsan eu, pulvinar et,
volutpat vel, elit. Curabitur nec dui sed nunc congue tempus. Nulla ac
dui ac libero fringilla nonummy. Maecenas ullamcorper sodales risus.
Vivamus pretium dolor. Proin eu turpis. Phasellus ut mauris non nulla
mattis luctus.</p>

<blockquote>
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
Suspendisse egestas ultricies pede. Phasellus suscipit blandit risus.
Praesent nonummy. In erat. Duis nibh pede, accumsan eu, pulvinar et,
volutpat vel, elit. </p>
</blockquote>

<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
Suspendisse egestas ultricies pede. Phasellus suscipit blandit risus.
Praesent nonummy. In erat. Duis nibh pede, accumsan eu, pulvinar et,
volutpat vel, elit. </p>

</div>
<div id="footer" style="border: 1px dotted blue; background-color:#c7e19e;
width:88%; margin:0 auto 10px auto; height:20px; padding:10px 45px 10px 45px;">
Here is a place for your footer data, text or whatever.
</div>
</div><!-- end CONTENT -->
</body>
</html>

I hope that it is something closer to what you're after. :)
-Joel

matt1776
06-05-2006, 11:37 AM
yes this is sort of what i envisioned, i wanted to get an idea of how css positioning works more than anything else, this is just a test page, the real page will look nothing like this at all. but i do appreciate the help, css seems to rely hevily on recursive notation. this is something that might take me some getting used to.

i can keep track.. to an extent. and the meta refresh, i program in the shell, why the hell would i want to go grab my mouse evertime i needed to see my changes?

WebJoel
06-05-2006, 12:44 PM
cool. ;)

re: "...i program in the shell, why the hell would i want to go grab my mouse evertime i needed to see my changes?"

True enough. It's your site and I should have also said "feel free to put that back in if you want it) and I could have merely !commented the meta-tag out, too.

For 'practice' working with CSS and DHTML templates, check out http://www.strangebanana.com
:)
-Joel