I have a puzzling problem with centering text, and I'm hoping that someone
here can help me out. First of all, let me state that I am using XHTML 1.0
Strict and CSS for all layout purposes (no tables). My website consists of a
few, simple parts: a "container" DIV which is centered and 750 pixels wide,
a "sidebar" DIV which can be found along the left side of the container, and
a "content" DIV which fills up what's left of the right part of the
container. This simple layout in XHTML markup is as follows:
<div id="container">
<div id="sidebar"> (... Some Sidebar Stuff ...) </div>
<div id="content"> (... Some Content ...) </div>
</div>
The corresponding, simple CSS that goes with this is as follows:
#container {
margin: 0 auto;
padding: 10px;
text-align: left;
width: 730px;
}
#sidebar {
float: left;
margin: 0;
padding: 10px;
text-align: center;
width: 125px;
}
#content {
width: 560px;
margin-left: 155px;
padding: 5px;
}
Here is the problem: if I place a paragraph tag within the "content" DIV,
and give it a class name of "center" (this class consists of one rule only:
text-align:center; ), the text is not centered as I would like it to be.
Rather, it is left justified. What's going on here? Does this have something
to do with the nesting of the DIV elements?
If I change the "center" class to the following code, the text is aligned
properly:
#content p.center { text-align: center; }
I don't have a problem with changing the code, but I don't understand why
the previous "center" class (without the #content and p selectors) doesn't
work. Can anyone shed some light on why this might work the way it does? I
would greatly appreciate any wisdom on the subject.
Thanks,
Jonah B.