Flexbox
CSS
Web Design
Layout Design
CSS Centering Techniques

Flexbox center horizontally and vertically

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Flexbox, or the Flexible Box Layout, presents an efficient way to layout, align, and distribute space among items in a container, even when their size is unknown or dynamic. A common task in web design is to center content both horizontally and vertically, which can be elegantly executed using Flexbox.

Understanding Flexbox

Flexbox operates on the principles of a flexible container (flex container) and flexible items (flex items). The main idea is to give the container the ability to alter its items’ width/height (and order) to best fill the available space on different screen sizes.

Setting Up Flexbox

To use Flexbox, you first need to define a container as a flex container with the display property:

css
.container {
  display: flex;
}

This transformation allows you to utilize all the powerful properties associated with flex layouts such as align-items, justify-content, flex-direction, etc.

Horizontal and Vertical Centering

Centering items horizontally and vertically is straightforward with Flexbox. We can achieve this by combining align-items and justify-content properties. Here are the steps:

  1. Set display property: Define the display mode as flex to make the parent container a flex container.
css
    .container {
      display: flex;
    }
  1. Align items vertically: Use the align-items property set to center. This vertically centers the flex items inside the container.
css
1    .container {
2      display: flex;
3      align-items: center; /* centers items vertically in the container */
4    }
  1. Justify content horizontally: Set the justify-content property to center. This horizontally centers the flex items.
css
1    .container {
2      display: flex;
3      align-items: center;
4      justify-content: center; /* centers items horizontally in the container */
5    }

The combination of these two properties (align-items and justify-content) acts on the cross axis and main axis respectively, thus centering the child elements exactly at the middle of the parent container.

Practical Example

Consider a simple HTML structure with a flex container and one child item:

html
<div class="container">
  <div class="item">Center me!</div>
</div>

Apply the previously discussed styling to ensure the item is centered:

css
1.container {
2  display: flex;
3  align-items: center;
4  justify-content: center;
5  height: 100vh; /* Full view height */
6  border: 1px solid #ccc; /* Optional: to see the container edges */
7}
8
9.item {
10  padding: 20px;
11  background-color: lightcoral;
12}

This CSS not only centers the .item inside .container but also demonstrates how to make the container take the full height of the viewport (useful in many modern web design scenarios).

Useful Properties in Flexbox

Here's a quick reference table for properties commonly used when working with Flexbox for centering and other alignments:

PropertyPurposeValues Example
displayEstablishes a Flexbox contextflex, inline-flex
flex-directionDefines the main axisrow, column
justify-contentAligns items along the main axiscenter, flex-start, flex-end
align-itemsAligns items along the cross axiscenter, flex-start, flex-end
align-contentAligns multiple linesstretch, center, space-between

Conclusion

Flexbox simplifies alignment challenges the earlier CSS box model struggled with, particularly with centering items both vertically and horizontally. It offers a robust, responsive layout solution that adjusts efficiently to screen sizes and resolutions, a staple in modern web design. Utilizing its properties can significantly enhance the user interface design process, making it a critical tool in any web developer's repertoire.

This detailed yet straightforward methodology ensures that as a developer, you have a clear, concise understanding of implementing central alignment using Flexbox, thereby increasing productivity in creating aesthetically pleasing and functionally superior web applications.


Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.