Propiedad CSS text-align
La propiedad text-align se utiliza para alinear el contenido interno de un elemento de bloque.
Puedes usar la propiedad margin para centrar el elemento.
INFO
La alineación especificada con text-align no se refiere al bloque contenedor ni al viewport.
| Valor inicial | left si la dirección es "ltr", right si la dirección es "rtl" |
|---|---|
| Se aplica a | Contenedores de bloque. |
| Heredable | Sí |
| Animable | No. |
| Versión | CSS1 |
| Sintaxis DOM | object.style.textAlign = "right"; |
Sintaxis
Sintaxis de la propiedad CSS text-align
css
text-align: left | right | center | justify | initial | inherit;Ejemplo de la propiedad text-align con los valores "right" y "center":
Ejemplo de la propiedad CSS text-align con los valores right y center
html
<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
<style>
div {
text-align: right;
}
p {
text-align: center;
}
</style>
</head>
<body>
<h2>Text-align property example</h2>
<div>Example for the text-align property.</div>
<p>Some paragraph for example.</p>
</body>
</html>Resultado

Ejemplo de la propiedad text-align con los valores "center", "left" y "justify":
Ejemplo de la propiedad CSS text-align con los valores left, center y justify
html
<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
<style>
h2 {
text-align: center;
}
p.date {
text-align: left;
}
p.example {
text-align: justify;
}
</style>
</head>
<body>
<h2>Text-align property example</h2>
<p class="date">March, 2019</p>
<p class="example">
Lorem Ipsum is dummy text of the printing and typesetting industry. Lorem Ipsum h as been the industry's standard dummy text ever since the 1500s when an unknown pri nter took a galley of type and scrambled it to make a type specimen book. It has surviv ed not only five centuries, but also the leap into electronic typesetting, remaining essent ially unchanged. It was popularised in the 1960s with the release of Letraset sheets conta ining Lorem Ipsum passages, and more recently with desktop publishing software like Ald us PageMaker including versions of Lorem Ipsum.
</p>
</body>
</html>Valores
| Valor | Descripción | Pruébalo |
|---|---|---|
| left | Alinea el texto a la izquierda. Este es el valor predeterminado de esta propiedad. | Pruébalo » |
| right | Alinea el texto a la derecha. | Pruébalo » |
| center | Alinea el texto al centro. | Pruébalo » |
| justify | Extiende las líneas para que cada una tenga el mismo ancho. | Pruébalo » |
| initial | Hace que la propiedad use su valor predeterminado. | Pruébalo » |
| inherit | Hereda la propiedad de su elemento padre. |
Práctica
¿Cuáles de las siguientes son valores válidos para la propiedad 'text-align' en CSS?