Aligning a Cell's Contents
By default, a cell's contents are aligned two ways: horizontally to the left and vertically in the middle. When you're designing a liquid layout, in which cells should stretch out over an extended browser window, it's particularly important to specify where things should be aligned.
To align the contents of cells with (X)HTML:
1. | Place the cursor in the initial tag for the cell, row, or section, after the name of the tag but before the final >.
| 2. | If desired, type align="direction", where direction is left, center, right or justify.
| 3. | Type valign="direction", where direction is either top, middle, bottom, or baseline.
|
To align the contents of cells with CSS:
1. | In the desired rule, add text-align: direction, where direction is left, right, center, or justify.
| 2. | And/or add vertical-align: position, where position is baseline, top, bottom, or middle.
|
Tips
The default value for align is left. The default for valign is middle. Although the align attribute has been deprecated for other properties, it is still valid for all table elements (except table itself). The valign attribute is also still valid. Both are well supported. CSS properties text-align and vertical-align are also very well supported. You can align all of the cells in a row by applying the text-align or vertical-align property to (or inserting the align or valign attribute in) the tr tag.
Theoretically, you can align the contents with respect to any character you choose, for example to align monetary amounts with respect to the decimal point. In the (X)HTML, you'd use align="char" char="x", where x is the character around which to align. In CSS, use text-align:x, again, where x is the desired alignment character. Unfortunately, no browser I've seen supports either method.
The baseline value aligns the contents of each cell with the baseline of the first line of text that it contains. Baseline is the same as top when there are several lines of text and no images. Baseline is the same as bottom when the cells contain both images and text. The vertical-align property accepts a few other values (like text-top, text-bottom, sub, and sup) but these don't make sense with table cells, and so baseline is used in their place. For more information about text-align, see page 165. For more on vertical-align, see page 188.
|