Using CSS to center text
Most people are aware that you can center text in CSS using the text-align property like so:
p { text-align: center; }
The only down-side to this is that the centering occurs within the full width of the container element. For example, the paragraph element defined above will center the text across the entire page if this paragraph is in the main body of the page.
You could add divs, spans, and the like to constrain the size of the element in which it is centered, but it is also possible to do this purely in CSS. To do so, define the width of the element and set the margins to auto, like so:
p { margin-left: auto; margin-right: auto; width: 10em; }
Using this, you can get text that it centered in the page at whatever width you desire.
p { text-align: center; }
The only down-side to this is that the centering occurs within the full width of the container element. For example, the paragraph element defined above will center the text across the entire page if this paragraph is in the main body of the page.
You could add divs, spans, and the like to constrain the size of the element in which it is centered, but it is also possible to do this purely in CSS. To do so, define the width of the element and set the margins to auto, like so:
p { margin-left: auto; margin-right: auto; width: 10em; }
Using this, you can get text that it centered in the page at whatever width you desire.
| Rating: | no ratings, 0 total Votes |
| Categories: | CSS web |
| Added: | on Apr 04, 2007 at 7:48 am |
| Added By: | Marcos84 |

