image from How to Vertically Center Content in a Div

How to Vertically Center Content in a Div

3 minute read

Overview

Do you need help to vertically centre content in a div? If so, you’re not alone. Many developers find it challenging to achieve perfect vertical alignment, especially when dealing with dynamic content. But don’t worry; we will cover some simple and effective techniques to help you vertically centre content in a div like a pro in this article.

Understanding the Challenge

Before we dive into the solutions, let’s first understand why it’s challenging to centre content vertically in a div. The reason is that divs have no inherent vertical alignment properties. Unlike horizontal alignment, which can be achieved using text-align, vertical alignment has no equivalent property.

Flexbox

One of the most popular and effective ways to vertically centre content in a div is by using Flexbox. With Flexbox, you can create a flexible container that automatically adjusts its height and width to fit its content. To use Flexbox, follow these steps: Set the display property of the parent div to flex. Set the align-items and justify-content properties to the centre. Add your content inside the parent div. Here’s an example of how to use Flexbox:

.parent {
	display: flex;
	align-items: center;
	justify-content: center;
}

Grid

Another modern and flexible way to vertically centre content in a div is by using CSS Grid. CSS Grid is a two-dimensional layout system that allows you to create complex layouts easily. To use a grid layout, follow these steps: Set the display property of the parent div to grid. Set the align-items and justify-items properties to the centre. Add your content inside the parent div. Here’s an example of how to use a grid layout:

.parent {
	display: grid;
	align-items: center;
	justify-items: center;
}

Positioning

If you’re working with older browsers that don’t support Flexbox or Grid, you can still vertically centre content in a div using positioning. To use the positioning, follow these steps: Set the position property of the parent div to relative. Set the position property of the child div to absolute. Set the top and bottom properties of the child div to 0. Set the margin property of the child div to auto. Here’s an example of how to use positioning:

.parent {
	position: relative;
}

.child {
	position: absolute;
	top: 0;
	bottom: 0;
	margin: auto;
}

Transform

Finally, you can vertically centre content in a div using CSS Transform. To use Transform, follow these steps: Set the position property of the parent div to relative. Set the top and left properties of the child div to 50%. Set the transform property of the child div to translate(-50%, -50%). Here’s an example of how to use Transform:

.parent {
	position: relative;
}

.child {
	position: absolute;
	top: 50%;
	left: 50%;
	transform: translate(-50%, -50%);
}

Conclusion

Vertically centring content in a div may seem daunting initially, but it can be achieved easily with the proper techniques. Whether you choose Flexbox, Grid, positioning, or Transform, always select the best method for your use case. Experiment with different approaches and find the one that best fits your needs.

Start Hosting Today

Host your site with BlokHost in under 30 seconds!

Get Started