How to Center Elements Horizontally and Vertically Using Flexbox

Avatar

By squashlabs, Last Updated: October 6, 2023

How to Center Elements Horizontally and Vertically Using Flexbox

Flexbox is a useful CSS layout module that provides a flexible and efficient way to arrange and align elements within a container. One of the common use cases is to center elements both horizontally and vertically. In this guide, we will explore different approaches to achieve this using flexbox.

Method 1: Using flexbox properties

To center elements both horizontally and vertically using flexbox, you can make use of the following flexbox properties:

1. Set the parent container to display flex:

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

In the above example, the display: flex property is applied to the container element. This enables the flex layout for its child elements. By default, flex items are laid out in a row. To center them horizontally, we use the justify-content: center property. To center them vertically, we use the align-items: center property.

2. Apply width and height to the flex items:

.item {
  width: 200px;
  height: 200px;
}

In the above example, we set a fixed width and height for the flex items. Adjust these values based on your requirements.

Here’s a complete example:

<div class="container">
  <div class="item">Centered Element</div>
</div>
.container {
  display: flex;
  justify-content: center;
  align-items: center;
}

.item {
  width: 200px;
  height: 200px;
}

Related Article: How to Center an Absolutely Positioned Element in a Div

Method 2: Using margin:auto

Another approach to center elements both horizontally and vertically using flexbox is by leveraging the margin: auto property. This method works when you have a single element or a limited number of elements to center.

1. Set the parent container to display flex:

.container {
  display: flex;
}

2. Apply auto margin to the flex items:

.item {
  margin: auto;
}

Here’s a complete example:

<div class="container">
  <div class="item">Centered Element</div>
</div>
.container {
  display: flex;
}

.item {
  margin: auto;
}

Best Practices and Alternative Ideas

– Use the first method with justify-content: center and align-items: center when you have multiple elements to center within a container.
– Use the second method with margin: auto when you have a single element or a limited number of elements to center.
– Consider using a combination of flexbox and other CSS properties to achieve more complex centering requirements, such as centering elements only horizontally or only vertically.

Related Article: How to Vertically Align Text in a Div with CSS

More Articles from the Cascading Style Sheets (CSS) Guide series:

How to Horizontally Center a Div Element in CSS

Centering a div element horizontally in CSS can be achieved using various methods. One solution is to use the "margin: auto;" property, which automatically calculates... read more

How to Center a Div Vertically and Horizontally on a Page

Learn the simplest method to center a div both vertically and horizontally using CSS. This article covers different techniques such as Flexbox, Absolute Positioning, and... read more

How to Vertically Center Text with CSS

This article provides a guide on vertically centering text using CSS properties and techniques. You'll learn how to achieve this using flexbox, CSS grid, and alternative... read more

How to Center a Position Absolute Element in CSS

Centering an element with position absolute in CSS can be achieved using different techniques. Two commonly used methods are using the Transform Property and Flexbox. In... read more

How to Vertically Align Text Within A Div Using CSS

This guide provides steps on how to use CSS to vertically align text within a Div. The two chapters, "Flexbox" and "CSS Table Display," offer different methods to... read more

How to Right Align Div Elements in CSS

Setting Div elements to align right in CSS can be achieved using two different properties. The first method involves using the "float" property, while the second method... read more