text-align
property in CSS is used to control the alignment of text within an element. This property can be applied to block-level elements, such as <div>, <code><p></p>
, or <h1></h1>
to <h6></h6>
, to change the alignment of the text within these elements. To use the text-align
property, you specify it within a CSS rule and set it to one of its available values. For example, to center the text within a <p></p>
element, you could use the following CSS rule:p { text-align: center; }
<p>
elements on the page. You can apply the text-align
property to a specific element by using an ID or a class selector, or to multiple elements by using an element selector as shown above.text-align
property accepts several values that determine how the text within an element is aligned. The most commonly used values are:text-align
property also accepts the following less common values:left
for left-to-right languages and right
for right-to-left languages.right
for left-to-right languages and left
for right-to-left languages.text-align
property affects the layout of text within block-level HTML elements by adjusting the horizontal position of the text lines relative to the edges of the element's content box. For example:<p>
element with text-align: left
, the text will align along the left edge of the paragraph, leaving the right side potentially uneven.text-align: right
is used, the text will align along the right edge, leaving the left side potentially uneven.text-align: center
will position the text such that there is an equal amount of space on both the left and right sides of the text block within the element.text-align: justify
is set, the browser attempts to adjust the spacing between words so that each line of text within the element stretches to fill the full width of the element, except for the last line, which is typically left-aligned by default.text-align
is limited to text and inline elements within the block-level element it's applied to. It does not affect the positioning of block-level elements themselves or any absolutely positioned elements within the block.text-align
property is specifically designed to control the horizontal alignment of text within an element. It does not have any effect on the vertical alignment of text. For vertical alignment, other CSS properties must be used.<p>To achieve vertical alignment of text, you might use properties like line-height
for single-line text elements, or flexbox properties such as align-items
and justify-content
when using a flex container. For example, to vertically center text in a <div>
using flexbox, you could use:div { display: flex; align-items: center; justify-content: center; height: 200px; /* Height must be set for vertical alignment to work */ }
text-align
is powerful for managing the horizontal layout of text, other CSS techniques are required for vertical alignment.
The above is the detailed content of How do you control the text alignment using the text-align property?. For more information, please follow other related articles on the PHP Chinese website!