Method 1: Using Flexbox

Flexbox is a powerful layout tool in CSS. To center a div using Flexbox:

.parent {
    display: flex;
    justify-content: center;
    align-items: center;
}
.child {
    width: 100px;
    height: 100px;
}
Centered

Next: Learn how to center a div using CSS Grid.

Method 3: Using Traditional CSS Methods

Centering a div can also be achieved using traditional CSS methods such as margin auto:

.parent {
    position: relative;
}
.child {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
}
Centered

Next: Get in touch with us for more tips.

Contact Us