An Easy Way to Center a DIV both Vertically and Horizontally

Search for a command to run...

No comments yet. Be the first to comment.
You might need to ask yourself too

Let's talk about usability and 3 common mistakes you might be making when building that product. Confusing 'Account Setup' with 'Onboarding': Your onboarding flow is meant to serve a primary purpose,

Last year during Google's Built-In AI hackathon, I built an experimental Chrome extension called Clarity Lens. The product was inspired by thinking about the 'hidden 16%', the portion of the world's p

If you've used Youtube long enough, you'd realize it gradually became better in understanding your needs. I'm sure you can relate to this - you think of something, next thing, you open Youtube and it's being advertised, or you come across it on your ...

Trade-offs, accessibility concerns, and what CSS alone can’t solve

This is a common problem that beginners face which seems to have no particular solution because there are many ways to achieve this. For almost every project I did, when starting out, I was searching the internet whenever I wanted to centre a div both horizontally and/or vertically. I came across one really easy way to achieve this while I was doing an online course, and I have since decided to stick to this method(using the flexbox) except in distinct cases.
In this article, I would describe how a div which is on a page, inside another element or div can be:
To center a div vertically, you do this:
<!-- I want to center the div with a class of shape-->
<html>
<head>
<style>
.shape{
width: 100px;
height: 100px;
background: green;
}
</style>
</head>
<body class="container"><!--This is the parent element-->
<div class="shape"></div><!-- This is the div to center-->
</body>
</html>
The code above gives this as an output:
From the image above, we see that the 'shape' div is not centered vertically, to achieve this:
<head>
<style>
.shape{
background: green;
}
.container{
display: flex;
align-items: center;
}
/* The properties are added to the 'container' parent element */
</style>
</head>
<body class="container"><!--This is the parent element-->
<div class="shape"></div><!-- This is the div to center-->
</body>
This is the output:

The 'shape' div has been centered vertically. Next, we horizontally center the div.
We follow the same steps above to horizontally center the div except that we now use 'justify-content: center' instead of 'align-items: center'
This is shown in the code below:
<head>
<style>
.shape{
background: green;
}
.container{
display: flex;
justify-content: center;
}
/* The properties are added to the 'container' parent element */
</style>
</head>
<body class="container"><!--This is the parent element-->
<div class="shape"></div><!-- This is the div to center-->
</body>
The output below shows a div which has been centered horizontally:

Next, we center the div both horizontally and vertically
This is a combination of the two initial steps, we achieve this by:
This is shown below:
<head>
<style>
.shape{
background: green;
}
.container{
display: flex;
align-items: center;
justify-content: center;
}
/* The properties are added to the 'container' parent element */
</style>
</head>
<body class="container"><!--This is the parent element-->
<div class="shape"></div><!-- This is the div to center-->
</body>
The output is shown here:

We see that the div is now centered both vertically and horizontally.
Thanks for reading this! I hope it was really helpful, I would love to hear your comments.