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;
}
Next: Learn how to center a div using CSS Grid.
Method 2: Using CSS Grid
CSS Grid is another excellent way to center a div:
.parent {
display: grid;
place-items: center;
}
.child {
width: 100px;
height: 100px;
}
Next:
Learn how to center a div using Traditional CSS Methods.
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%);
}
Next: Get in touch with us for more tips.
Contact Us