Forum Moderators: not2easy

Message Too Old, No Replies

Centering in Opera/FF. OK in IE

Newby to all of this!

         

melaniew

9:38 pm on Jun 15, 2007 (gmt 0)

10+ Year Member



I'm new to CSS. Am trying to centre my page within Opera and FF. Works fine in IE. Here is my code from CSS- if anyone can point out what I've done wrong I'll be SOOOOO grateful - it's driving me MAD! As I say, I'm new to this so my code is probably laughable!

Thanks a million, in advance!

Melanie

/* CSS layout */
#masthead {
width:720px;
background-color:white:
}
#top_nav {
position:relative;
width:720px;
background-color:white
}
#container {
position: relative;
width: 710px;
padding-left: 0px;
padding-right: 10px;
background-color: white;
}
#left_col {
width: 150px;
position: absolute;
left: 0px;
top: 0px;
}
#page_content {
margin-left: 200px;
}
#footer {
width: 720px;
position: relative;
left: 0px;
background-color:white
}
.h2 {
font-family:Arial, Helvetica, sans-serif;
font-size:large;
font-style:oblique
}
.p {
font-family:Arial, Helvetica, sans-serif;
font-size:small;
font-style:normal;
text-align:justify;
color:gray
}
body {
margin: 50px 0px;
padding: 0px;
text-align: center;
background-image: url('images/darkbluebgrd.gif');
background-repeat: repeat-x;
}
#Content {
width:720px;
margin:0 auto;
text-align:left;
padding:15px;
border:1px dashed #333;
}

coyoteRick

10:00 pm on Jun 15, 2007 (gmt 0)

10+ Year Member



Congratulations on diving into the falls Cascading Style Sheet coding!

It's usually standard practice to post both HTML/XHTML and CSS markup so helpful people can determine if it's one or the other or both that may be causing problems.

However, just glancing at your CSS seems to show me what you're up against.

You're lacking a "wrapper". Basically, THE parent to all content on your page. Make a new division and id it as wrapper. Make sure the wrapper division encompasses everything... it should begin after <body> and close right before </body>.

Then give it some CSS markup like the following:

#wrapper {
margin:0 auto; /*this is the shorthand code that gets FF to correctly center an element by automatically configuring the left/right margins in correspondance to your width. If no width is set, this won't work*/
padding:0;
width:720px; /*Consider making this a bit bigger than your widest elements if necessary */
}

Fortunately, making the margin property have left/right automatically configured should work with FF. However, IE doesn't like it. Do counter this I usually put "text-align:center;" within the CSS definition for BODY. I then "reset" the text-align in the wrapper by using "text-align:left;".

Wow, that was a bit more verbose than I had intended... hope it helps! If it doesn't help, post your XHTML markup along with the CSS.

Cheers!

[edited by: coyoteRick at 10:02 pm (utc) on June 15, 2007]

melaniew

5:24 pm on Jun 16, 2007 (gmt 0)

10+ Year Member



Thank you so much for your reply. It worked! I'm actually getting quite hooked on all this.... Frustrating though it can be. I just wish all browsers would display everything the same...<sigh>

Melanie

Xapti

3:51 am on Jun 17, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I don't get it though. Isn't #Content the wrapper that has the margin:auto?
And isn't it's parent the body, which also has the text-align:center?

I guess not though. I guess it just goes to show how important it is to post your HTML with CSS.

Setek

6:56 am on Jun 18, 2007 (gmt 0)

10+ Year Member



I don't get it though. Isn't #Content the wrapper that has the margin:auto?
And isn't it's parent the body, which also has the text-align:center?

I guess not though. I guess it just goes to show how important it is to post your HTML with CSS.

It’s because the OP is asking for an entire page centered, not just the content area. In the CSS if you read through it all, the

#left_col
is positioned absolutely, so unless it’s within
#content
, it will stay left aligned.

I just want to extend on what coyoteRick said:

Fortunately, making the margin property have left/right automatically configured should work with FF. However, IE doesn't like it. Do counter this I usually put "text-align:center;" within the CSS definition for BODY. I then "reset" the text-align in the wrapper by using "text-align:left;".

This workaround is only necessary on IE 6 upwards if the page is being sent into IE’s infamous quirks mode; for more information on what’s the difference, you can check out this post on quirks mode [webmasterworld.com].

The good thing about making sure that you don’t send IE into quirks mode, is that you will have less problems and things to make workarounds for. You could eliminate the need for the

text-align
, along with table font sizes, most of the box model problems, and other things I can’t remember :)

Good luck, all the same :)