How To Center a div Vertically and Horizontally Using CSS

To center a div vertically and horizontally, you can use the CSS flexbox layout. To do this, first, add the following code to the CSS stylesheet of your website:

.container { 
    display: flex; 
    justify-content: center; 
    align-items: center; 
    height: 100%; 
}

Replace “container” with the class or ID of the div that you want to center. This code will set the div to use the flexbox layout and center its content horizontally and vertically within the parent container.

To use this code, you also need to set the height of the parent container to 100%. This will ensure that the div is centered vertically within the entire height of the container. You can add the following code to the CSS stylesheet to set the height of the parent container:

.parent { height: 100%; }

Replace “parent” with the class or ID of the parent container.

In conclusion, using the CSS flexbox layout, you can easily center a div vertically and horizontally on your website. This can be useful for creating clean and organized layouts, and it can make your website more visually appealing to users.

In: