It won't center!?

I'm learning XHTML and CSS on my own. I have bought several books and I'm validating everything on the W3C website.

All I want to do is is center my H3 headers. Because I'm validating with Strict, it won't allow me to do a simple <h3 align="center>, so I have to find a way to center the text in CSS. I thought the h3 {text-align:center;} would work, but it's not working.

I checked to see if there is any other language that may be causing it but I can't find anything.

Any ideas?

tgtips2008-01-20T18:42:38Z

Favorite Answer

As I cannot see your code it is hard to say whether you just have a simple typing error in your code somewhere.

Here is a basic example of CSS alignment that hopefully will help you.

<html>
<head>
<style type="text/css">
h1 {text-align: center}
h2 {text-align: left}
h3 {text-align: right}
</style>
</head>

<body>
<h1>This is header 1</h1>
<h2>This is header 2</h2>
<h3>This is header 3</h3>
</body>

</html>

If you ran that then you ought to see the three headings from largest to smallest, beginning with being centered, then left aligned, then right aligned.

As you can see your assumption that h3 {text-align: center} was correct, so my guess is you have a typo further down the code.

One thing I did note was your use of the semi-colon. As there is no further information inside those {braces} you do not really need it there, although I would not have thought it'd cause you any real issues either.

Now the other thing you could look at is how you declared your webpage. If you declared it an HTML page then you must use valid HTML tags, but if you declared it an xHTML page then you must use xHTML standards.

For example:-

The main things you'll notice about xHTML are:

1. Tags must be lowercase
2. All parameters in tags must be in quotes
3. Self-closing (standalone) tags must end with a forward-slash (note: which must be preceded with a space!)

Hence:-

The following would be permissible in HTML 4, but invalid in xHTML:

<IMG SRC=background.gif WIDTH=40 HEIGHT=20 ALT="Hello">

For valid xHTML, you'd have to put:

<img src="background.gif" width="40" height="20" alt="Hello" />