Diseño CSS - Alineación horizontal y vertical
Centrar elementos
horizontal y verticalmente
Elementos de alineación central
Para centrar horizontalmente un elemento de bloque (como <div>), use margin: auto;
Establecer el ancho del elemento evitará que se extienda hasta los bordes de su contenedor.
Entonces, el elemento ocupará el ancho especificado y el espacio restante se dividirá en partes iguales entre los dos márgenes:
Este elemento div está centrado.
Ejemplo
.center
{
margin: auto;
width: 50%;
border: 3px solid green;
padding: 10px;
}
Nota: la alineación central no tiene efecto si la width
propiedad no está configurada (o configurada al 100 %).
Texto alineado al centro
Para centrar el texto dentro de un elemento, utilice text-align: center;
Este texto está centrado.
Ejemplo
.center {
text-align: center;
border: 3px solid green;
}
Sugerencia: Para obtener más ejemplos sobre cómo alinear texto, consulte el capítulo Texto CSS .
centrar una imagen
Para centrar una imagen, establezca el margen izquierdo y derecho auto
y conviértalo en un block
elemento:
Ejemplo
img
{
display: block;
margin-left: auto;
margin-right: auto;
width: 40%;
}
Alineación izquierda y derecha - Posición de uso
Un método para alinear elementos es usar position: absolute;
:
En mis años más jóvenes y vulnerables, mi padre me dio un consejo que desde entonces no he dejado de darle vueltas en la cabeza.
Ejemplo
.right
{
position: absolute;
right: 0px;
width: 300px;
border: 3px solid #73AD21;
padding: 10px;
}
Nota: Los elementos de posición absoluta se eliminan del flujo normal y pueden superponerse a los elementos.
Alineación izquierda y derecha: uso de flotador
Otro método para alinear elementos es usar la float
propiedad:
Ejemplo
.right
{
float: right;
width: 300px;
border: 3px solid #73AD21;
padding: 10px;
}
El truco del arreglo claro
Nota: si un elemento es más alto que el elemento que lo contiene y flota, se desbordará fuera de su contenedor. Puede usar el "truco clearfix" para arreglar esto (vea el ejemplo a continuación).
Sin Clearfix
Con Clearfix
Luego, podemos agregar el truco clearfix al elemento contenedor para solucionar este problema:
Ejemplo
.clearfix::after {
content: "";
clear: both;
display: table;
}
Centrar verticalmente: usar relleno
There are many ways to center an element vertically in CSS. A simple solution is to use top and bottom padding
:
I am vertically centered.
Example
.center {
padding: 70px 0;
border: 3px solid
green;
}
To center both vertically and horizontally, use padding
and text-align: center
:
I am vertically and horizontally centered.
Example
.center {
padding: 70px 0;
border: 3px solid
green;
text-align: center;
}
Center Vertically - Using line-height
Another trick is to use the line-height
property with a value that is equal
to the height
property:
I am vertically and horizontally centered.
Example
.center {
line-height: 200px;
height: 200px;
border: 3px solid green;
text-align: center;
}
/* If the text has multiple lines, add the
following: */
.center p {
line-height: 1.5;
display: inline-block;
vertical-align: middle;
}
Center Vertically - Using position & transform
If padding
and line-height
are not options, another solution is to use positioning and the transform
property:
I am vertically and horizontally centered.
Example
.center {
height: 200px;
position: relative;
border: 3px solid green;
}
.center p {
margin: 0;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
Tip: You will learn more about the transform property in our 2D Transforms Chapter.
Center Vertically - Using Flexbox
You can also use flexbox to center things. Just note that flexbox is not supported in IE10 and earlier versions:
Example
.center {
display: flex;
justify-content: center;
align-items: center;
height: 200px;
border: 3px solid
green;
}
Tip: You will learn more about Flexbox in our CSS Flexbox Chapter.