top of page
Writer's pictureTaylor Quade

How to Use Flexbox for Centering Elements Horizontally and Vertically




Flexbox, or the Flexible Box Layout is an effective CSS layout created specifically for developers to build versatile layouts efficiently. One of its key capabilities is that it helps align the elements along the horizontal and vertical axes.


This capability is very useful now in today’s web design, where the alignment of the content elements can significantly improve user experience. In this guide, we will dive into the details of Flexbox implementation and how you can use it to center elements with practical scenarios.


Understanding Flexbox Basics


Before diving into centering techniques, it's essential to grasp the core concepts of Flexbox. At its core, Flexbox is based on two primary components: the flex container and the flex items.


  • Flex Container: This is the parent element that holds the flex items. It is defined using display: flex; or display: inline-flex;.

  • Flex Items: These are the children of the flex container. They are automatically aligned according to the flex container's properties.


Here’s a basic example of a flex container and its items:


html


<div class="flex-container">
    <div class="flex-item">Item 1</div>
    <div class="flex-item">Item 2</div>
    <div class="flex-item">Item 3</div>
</div>

And the CSS to make this a flex container:


css


.flex-container {
    display: flex;
}

Centering Elements Horizontally and Vertically


Crafting a responsive layout is a challenging process in web app development. Flexbox simplifies this one of its critical tasks by center-aligning elements both horizontally and vertically with its powerful layout mechanism.


So whether you are a web developer or a designer transitioning from tools like Figma to HTML, Flexbox is an essential skill to master. Flexbox allows you to easily center-align the container elements both horizontally and vertically. Let’s find out how to achieve this.


Centering Horizontally


When you want to center container elements horizontally, simply use the justify-content property. This property automatically aligns all the flex items along the main axis (which is horizontal by default).


Here are some common values for justify-content:


  • center: Centers the flex items in the container along the main axis.

  • flex-start: Aligns the flex items in the container to the start of the container by default.

  • flex-end: Align the the flex items to the end of the container.

  • space-between: Distributes flex items evenly with the first item at the start and the last item at the end.

  • space-around: Distribute flex items evenly with equal space around each item.

  • space-evenly: Distributes flex items with equal space between them.


For horizontal centering, you can use the code below:


css


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

Centering Vertically


For the vertical alignment of children within the flex container, use the align-items property. This property again makes the flex items in the orientation of the cross axis which is by default in the vertical direction.


Here are some common values for align-items:


  • center: Centers the flex items along the cross-axis.

  • flex-start: Aligns items to the start of the cross-axis.

  • flex-end: Aligns items to the end of the cross-axis.

  • baseline: Aligns items along their baseline.

  • stretch: Stretches items to fill the container by default.


For vertical centering, you can use the below code:


css

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

Centering Both Horizontally and Vertically


If you want to center container elements both horizontally and vertically, you need to combine the justify-content and align-items properties. Here’s the code sample explaining how you can achieve this:


css


.flex-container {
    display: flex;
    justify-content: center; /* Horizontal centering */
    align-items: center;     /* Vertical centering */
    height: 100vh;           /* Make sure the container has a height */
}

In the above code example, the height: 100vh; ensures that the flex container takes up the full viewport height, thus allowing vertical centering to be visible.


Practical Code Implementation for Various Scenarios


Let’s apply these principles in various practical scenarios.


Example 1: Centering a Single Element

Imagine you have a single element that you want to center in the middle of the screen:


html


<div class="flex-container">
    <div class="centered-item">Centered Item</div>
</div>

css


.flex-container {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
    border: 2px solid #ddd;
}

.centered-item {
    background-color: #f39c12;
    padding: 20px;
    color: #fff;
    border-radius: 5px;
}

Hurry! your item is perfectly centered both horizontally and vertically.


Example 2: Centering Multiple Items in Container

If you have multiple items and want to center them in a row:


html


<div class="flex-container">
    <div class="flex-item">Item 1</div>
    <div class="flex-item">Item 2</div>
    <div class="flex-item">Item 3</div>
</div>

css


.flex-container {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
    border: 2px solid #ddd;
}

.flex-item {
    background-color: #3498db;
    padding: 20px;
    color: #fff;
    margin: 10px;
    border-radius: 5px;
}

Here, in this case, all items in the container are centered horizontally within the container, and the container itself is centered vertically within the viewport.


Example 3: Centering with Dynamic Content

Sometimes, you might need to center content with dynamic height:


html


<div class="flex-container">
    <div class="dynamic-content">
        <p>This content height can change.</p>
    </div>
</div>

css


.flex-container {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
    border: 2px solid #ddd;
}

.dynamic-content {
    background-color: #2ecc71;
    padding: 20px;
    color: #fff;
    border-radius: 5px;
}

The dynamic-content div will be centered in the container regardless of its height.


Flexbox Alignment for Nested Containers


While working with complex layouts with nested flex containers, you can apply similar techniques for center alignment. However, remember that the alignment properties are relative to their parent containers.


html


<div class="outer-container">
    <div class="inner-container">
        <div class="nested-item">Nested Item</div>
    </div>
</div>

css


.outer-container {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
    border: 2px solid #ddd;
}

.inner-container {
    display: flex;
    justify-content: center;
    align-items: center;
    width: 50%;
    height: 50%;
    background-color: #ecf0f1;
}

.nested-item {
    background-color: #e74c3c;
    padding: 20px;
    color: #fff;
    border-radius: 5px;
}

In this example, the nested-item will be centered within the inner-container, which is itself centered within the outer-container.


Best Practices for Using Flexbox


  1. Start with a Clear Layout:

    It is necessary to have an idea of the layout that is to be created before implementing Flexbox. This is particularly important with regard to the number of columns, rows, and the alignment characteristics.


  2. Use the Right Flex Properties:

    Try out the various values of flex-direction, justify-content, align-items, as well as flex-wrap in order to get the preferred layout.


  3. Leverage Flexbox for Responsive Design:

    There you can use media queries to define Flexbox attributes that change with the screen size or orientation. Make it a point that your layout is responsive to the various devices.


  4. Avoid Overly Complex Layouts:

    Despite of Flexbox’s usefulness, it is not supposed to be the ultimate solution for the creation of very complex layouts. It should be noted that such a design is better implemented using other techniques, such as grids or absolute positioning.


  5. Prioritize Readability and Maintainability:

    Use proper indentation and commenting techniques in the CSS and while naming classes make sure they are meaningful. Nesting should be kept to a minimum and code complexity should be kept to an absolute bare minimum.


  6. Test Thoroughly:

    It is advisable to conduct a browser compatibility check on those layouts using the Flexbox model. I would like to advise always to use developer tools to inspect and debug CSS/-html.


  7. Consider Browser Compatibility:

    Although Flexbox is relatively new, different browsers have varying levels of support for specific Flexbox features, particularly older browsers. It is recommended to use CSS fallbacks or some techniques that would detect features on a specific browser.


Learn from Others:


With the above tips and tricks, you will be in a better position to make good use of Flexbox in achieving responsive and aesthetically appealing web designs.


Conclusion


Flexbox can be used as an excellent tool to center the elements horizontally and vertically and is a must-have skill for developers and designers. However, if you are planning to build complex layouts for the web it is a good choice to hire HTML developers.


This article showed the application of justify-content and align-items properties and how you can perfectly align your web page and create appealing UI designs.


Regardless of the fact that it is applied to a single element or complicated nested structures, Flexbox allows you to achieve the necessary flexibility and control to solve any particular task.



Happy coding!

22 views
bottom of page