How to Center Elements Horizontally and Vertically Using Flexbox

Avatar

By squashlabs, Last Updated: Oct. 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 Vertically Center Text with CSS

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

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

- 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.

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

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 differe… read more

How to Use Ng Deep in AngularJS

A guide on using ng-deep in CSS within the AngularJS framework. Understand the ng-deep directive, use it in your AngularJS application, follow best p… read more

How to Implement an Onclick Effect in CSS

Adding an onclick effect in CSS is a simple process that can enhance the interactivity of your web pages. This article provides a guide on how to imp… read more

How To Troubleshoot CSS Position Sticky Not Working

CSS position sticky is a powerful property that allows elements to stick to a specific position as the user scrolls. However, it may not always work … read more

How To Set the CSS Background Opacity Property

Adjusting the opacity of CSS backgrounds is made easy with these two methods. Learn how to use RGBA color values or the CSS opacity property to achie… read more

How To Add a Border Inside a Div: CSS Border Inside

Table of Contents Solution 1: Using Padding and Box ShadowSolution 2: Using Border and Background ColorWhy is this question asked and potential reas… read more

Centering Text with CSS: Tricks and Techniques

Centering text in CSS can be a challenge, but this article will equip you with the tricks and techniques to achieve perfect alignment effortlessly. F… read more

How to Make the First Character Uppercase in CSS

CSS offers different approaches to capitalize the first letter of a text. You can use the ::first-letter pseudo-element or the text-transform propert… read more

How to Style a Disabled Button with CSS

Styling disabled buttons using CSS can greatly enhance the user interface of your web application. This article provides a step-by-step guide on two … read more

How to Hide Scroll Bar in CSS While Maintaining Scroll Functionality

A simple guide on hiding the scroll bar in CSS while maintaining scroll functionality intact. This article covers different methods, including using … read more