473,395 Members | 1,677 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Centering Text in a Nested DIV

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.
Jul 20 '05 #1
3 7337
I am so foolish. In posting these now three times, I realized what the
problem is. One rule I forgot to mention in both of my previous posts was
the following:

#content p { text-align: justify; }

Since this rule binds more tightly than just the "center" class, it
obviously takes precedence. This was an example of poor markup and style
sheet authoring, as I thought it might be.

Can anyone explain to me, then, exactly how precedence works in CSS? Why
wouldn't a class override the rule mentioned above?

Jonah B.

"Jonah Bishop" <jg****@yahoo.com> wrote in message
news:qq******************@twister.southeast.rr.com ...
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.

Jul 20 '05 #2
I forgot to mention that my CSS also has the following rule in it:

body { text-align: center; }

This is in place to fool IE into centering the "container" DIV (since IE
wrongly uses that tag for such purposes).

Jonah B.

"Jonah Bishop" <jg****@yahoo.com> wrote in message
news:qq******************@twister.southeast.rr.com ...
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.

Jul 20 '05 #3
"Jonah Bishop" <jg****@yahoo.com> wrote in message
news:AJ*******************@twister.southeast.rr.co m...
I am so foolish. In posting these now three times, I realized what the
problem is. One rule I forgot to mention in both of my previous posts was the following:

#content p { text-align: justify; }

Since this rule binds more tightly than just the "center" class, it
obviously takes precedence. This was an example of poor markup and style sheet authoring, as I thought it might be.

Can anyone explain to me, then, exactly how precedence works in CSS? Why wouldn't a class override the rule mentioned above?


Jonah,

What you want to read is
http://www.w3.org/TR/REC-CSS2/cascade.html#cascade, especially the part
on selector precedence at
http://www.w3.org/TR/REC-CSS2/cascade.html#specificity.

I agree with Stan about specifying width of a text area in pixels. I
would use percent, which allows the size to grow or shrink as the user
resizes the window or changes screen resolutions. To prevent the text
lines from getting uncomfortably short or long, use max-width and
min-width, both of which should be specified in em units. [True, some
browsers don't honor these parameters, but at least you can say you
tried...] The latter is important because "studies show" that lines
longer than a certain number of characters become difficult to read
(your eye wanders up or down as it tries to traverse the line).

For the same reason, justified text is frowned upon by many folks.
Left-aligned text leaves a ragged right margin. When you combine this
with a reasonable length line, the irregularity of the right margin
gives the eye something to use as a vertical reference point, to keep it
scanning across the current line. When all lines end at the same point
(especially if they are very long), the general field of view is just a
grey rectangle with no distinguishing marks and your eye will drift
upward or downward from the line you are scanning.

Fonts, colors, and line-spacing also impact readability.

Chris Beall

Jul 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Jeff Thies | last post by:
I have a series of blocks that are float left that I need centered on the page. <div class="center" align="center"> <div style="width: 100 px;float: left">thumbnail 1</div> <div style="width:...
9
by: Faz | last post by:
Hi I'm having some real trouble with a header I have created within a container. I have two problems; firstly the menu (a horizontal UL) will not centre within the surrounding header, despite...
15
by: red | last post by:
How do I center two side by side divs ? I've been writing css pages for a while but there's one thing tha still eludes me. I can center a div with margin auto. I can place two divs side by side...
5
by: rengaw03 | last post by:
How do I center a HTML login form horizontally on a page? I can't find anything useful on the web -- no shortage of sites offering to sell me three-column layouts with a flexible center column, or...
3
by: John Pote | last post by:
1. Horizontal centering a <divin browser window. The current trend seems to be to place page content in a fixed width area in the middle of the browser window. How is this achieved? If I use a...
5
by: Markus Ernst | last post by:
Hello This is a test example: http://www.markusernst.ch/anthracite/ http://www.markusernst.ch/anthracite/living_divani.html After googling and experimenting for several hours, I ended up...
4
by: SAL | last post by:
Hello, is there a way to menu control dynamically center itself horizontally on a page? I have placed a menu control in a panel control (no ajax) and would like it to center itself. Depending on...
1
by: =?Utf-8?B?ZnJhbmt5?= | last post by:
Hello, I've created a table that has two rows that are span across three columns. The third row has three columns, each with an image. The last row is also span accross three columns. The span...
4
by: harryusa | last post by:
I am trying to center 2 images concentrically which are z-indexed to lay on top of each other making an image with a border from another image that has a transparent center. I need the images to be...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.