Skip to main content
  1. Web Development Course/
  2. Intermediate CSS/

How to center a div

Alejandro AO
Author
Alejandro AO
I’m a software engineer building AI applications. I publish weekly video tutorials where I show you how to build real-world projects. Feel free to visit my YouTube channel or Discord and join the community.
Intermediate CSS - This article is part of a series.
Part 7: This Article

Centering a div element in CSS has become a meme that developers joke about. It is a necessary part of your web development journey going to Google (orChatGPT) and searching for “how to center a div” at least once. In this article, we’ll show you how to center a div element in CSS using several different techniques and which one is best for your use case.

Technique 1: Using margin:auto
#

One of the easiest ways to center a div element is by using the margin:auto property. This technique is simple and effective, and it works by setting equal margins on the left and right sides of the element, which centers it horizontally.

However, remember that this technique only works if the width of the element is set to a fixed value. If the width of the element is not set, the element will not be centered.

.center {
  width: 50%;
  margin: auto;
}

In this code, we have created a div element with a class of “center” and set its width to 50%. We have then set the margin property to auto, which centers the element horizontally within its parent container.

Technique 2: Using Flexbox
#

Flexbox is a powerful layout tool in CSS that allows you to create complex layouts and align elements in various ways, including centering (we will talk about layouts soon). To center a div element using Flexbox, you can use the display:flex property on the parent container and the justify-content:center and align-items:center properties on the child element.

Here’s an example:

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

.center {
  width: 50%;
}

In this code, we have created a container element with a class of “container” and set its display property to flex. We have then set the justify-content and align-items properties to center, which centers the child element vertically and horizontally within the container. Finally, we have created a div element with a class of “center” and set its width to 50%.

Technique 3: Using Grid
#

Another powerful layout tool in CSS is Grid, which allows you to create complex layouts and align elements in various ways, including centering. To center a div element using Grid, you can use the display:grid property on the parent container and the place-items:center property on the child element.

Here’s an example:

.container {
  display: grid;
  place-items: center;
}

.center {
  width: 50%;
}

In this code, we have created a container element with a class of “container” and set its display property to grid. We have then set the place-items property to center, which centers the child element vertically and horizontally within the container. Finally, we have created a div element with a class of “center” and set its width to 50%.

When to use each technique
#

Each of these techniques has its unique features and use cases. In general, I would suggest that you go for the least complex solution that works for your use case.

Margin:auto is the easiest and most straightforward way to center a div element. If you are not using a layout tool like Flexbox or Grid, this is the best technique to use.

I would not recommend adding a layout tool to your project just to center a div element since it is not worth the extra complexity and extra processing that the browser has to do. However, if you are already using a layout tool, go for it!

Conclusion
#

In conclusion, centering a div element in CSS can be achieved using several techniques, including margin:auto, Flexbox, and Grid. Each technique has its unique features and use cases, and understanding how to use them effectively can help you create visually appealing and functional web pages. By using one of these techniques, you can easily center a div element in your web page and create a balanced and aesthetically pleasing layout.

Intermediate CSS - This article is part of a series.
Part 7: This Article