DEV Community

Agbedo Ugochukwu Victor Future
Agbedo Ugochukwu Victor Future

Posted on

How to Center a div.

Assuming you have an HTML file created and a CSS file, create a div. Give it a class or an ID.
Style the display property to "flex." Then define the align-items and justify-content property to “center.”

<!DOCTYPE html>
<html>
<head>
    <title>TO CENTER A DIV </title>
</head>
<style>
    .center{
        display: flex;
        align-items: center;
        justify-content: center;
    }
</style>

<h2>center a div</h2> 



<div class="center"><button>center the div</button></div>

</body>

</html>
Enter fullscreen mode Exit fullscreen mode

Top comments (2)

Collapse
 
kevincp17 profile image
kevincp17

Doesn't matter who we are, we must know how to center a div. Therefore, all web developers must be thankful to you.

Collapse
 
itsfz1 profile image
Fahim Zada

One of legendary question xD.