Aligning Text Horizontally

Traditionally, text is either aligned at its left margin or fully justified (often called newspaper style, in which text is aligned at both left and right margins). In addition, for emphasis or special effect, text can be centered on the screen or even right-justified. The text-align property (Table 4.6) gives you control of the text's alignment and justification.

Table 4.6. Text-Align Values




IE3, FF1, S1, O3.5, CSS1


IE3, FF1, S1, O3.5, CSS1


IE3, FF1, S1, O3.5, CSS1


IE4, FF1, S1, O3.5, CSS1

In this example (Figure 4.6), the level 1 heading and author class have been centered, while the level 2 heading has been right justified, and the copy has been fully justified.

Figure 4.6. The book title and author name are centered, while the chapter number and title are right aligned.

To define text alignment:



Type the text-align property name, followed by a colon (:), in the CSS definition list (Code 4.6).

Code 4.6. The h1 tag and author class will be centered, while the h2 tag is right justified, including the chapter Title class.

[View full width]

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" " xhtml1-strict.dtd"> <html xmlns=""> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>CSS, DHTML &amp; Ajax | Aligning Text Horizontally</title> <style type="text/css" media="screen"> <!-- body {      font-size: 1.2em;      font-family: Georgia, "Times New Roman", times, serif;      text-align: left; } h1 {      text-align: center; } h2 {      text-align: right; } .chapterTitle{      text-align: left; } .author {      text-align: center; } p.copy {      text-align: justify; } --> </style> </head> <body> <div >      <h1>Alice's Adventures in Wonderland</h1>      <p >Lewis Carroll</p>      <h2>CHAPTER VI<br />         <span >Pig and Pepper</span></h2> </div> <p >For a minute or two she stood looking at the house, and wondering what to  do next, when suddenly a footman in livery came running out of the wood...</p> </body> </html>



Set one of the following alignment styles:

  • left to align the text on the left margin

  • right to align the text on the right margin

  • center to center the text within its area

  • justify to align the text on both the left and right sides


  • Notice in the example that although the chapterTitle class has been set to left justify, it cannot override the justification of its parent element. Text alignment only applies to block level elements.

  • Fully justifying text may produce some strange results on the screen because spaces between words must be added to make each line the same length. In addition, there is considerable debate about whether full justification helps or hinders readability.

CSS, DHTML and Ajax. Visual QuickStart Guide
CSS, DHTML, and Ajax, Fourth Edition
ISBN: 032144325X
EAN: 2147483647
Year: 2006
Pages: 230

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: