CSS - Center both vertically and horizontally

drozerah

Drozerah

Posted on July 24, 2019

CSS - Center both vertically and horizontally

Say you have a HTML <body></body> tag and you want its child <div></div> to be centered both vertically and horizontally with modern CSS : how to achieve that ?

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <link rel="stylesheet" href="/css/style.css">
</head>
<body>
    <div>
        <h1 class="title">Hello DEV!</h1>
    </div>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

Flexbox says :
style.css
image

CSS Grid says :
style.css
image

Voilà!

See you next time!

Drozerah

💖 💪 🙅 🚩
drozerah
Drozerah

Posted on July 24, 2019

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related