Centers a div horizontally and vertically using Flexbox.
.parent{
display: flex;
justify-content: center;
align-items: center;
}
Grid allows perfect centering using one line.
.parent{
display: grid;
place-items: center;
}
Used when you only want horizontal centering.
.box{
margin: auto;
}
Classic method using position and transform.
.box{
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
Uses top, bottom, left, right with margin auto.
.box{
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
margin: auto;
}
Used for text or inline elements.
.parent{
text-align: center;
line-height: 200px;
}