The Easiest Way to Align Items Using Flexbox

How to align Items in a Flex container

by Janeth Kent Date: 17-11-2020 css flexbox webdesign

With the release of flexbox in CSS, it has become an essential tool when placing elements next to each other, since, by default, the children of a display: flex are stacked on the left side taking up the minimum space according to their content.

The easiest way to align items using flexbox

If we want to modify the behavior of the children, making them distributed throughout the available space, for example, you can use the properties associated with Flex called justify-content..

If we want to modify the behavior on the vertical axis you can use align-items .

There are also other properties that we can apply to the children and modify their default behavior.

But in CSS there are many ways to achieve the same result, and usually the best option is the simplest.

Well, I'll show you a super simple way: I use to align elements within a container with display: flex and is using margin:auto. 

Display: flex + Margin: auto

If we apply an automatic margin to an element within a 'flex' it will push it in the opposite direction, as shown in the following image:

The easiest way to align items using flexbox

But this not only works in the X-axis, we can also use it to align in the Y-axis

The easiest way to align items using flexbox

And if you have come this far, surely you will not be surprised when you see that to center all the axes you only need a container with flex display and auto margin for the child, remember that for this to work the parent element has to have height.

The easiest way to align items using flexbox

Margin-auto within a grid

This behavior also happens inside a display:grid with the difference that the children have a delimited space, in the following image you can see the result:

The easiest way to align items using flexbox


<div class="flex row-decoration">
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
<div class="flex row-decoration">
<div class="cell"></div>
<div class="cell"></div>
<div class="cell" style="margin-left: auto"></div>
<div class="flex row-decoration">
<div class="cell"></div>
<div class="cell"></div>
<div class="cell" style="margin-left: auto; margin-right: auto"></div>
<div class="flex row-decoration">
<div class="cell"></div>
<div class="cell"></div>
<div class="cell" style=" height: 25px; margin-left: auto; margin-top: auto"></div>
<div class="flex row-decoration" style="height: 120px">
<div class="cell" style="margin: auto"></div>


.flex {
display: flex;
.cell {
  width: 50px;
  height: 50px;
  border-radius: 2px;
  background-color: #317cd8;
  &:not(:last-child) {
  margin-right: 6px;
.row-decoration {
  padding: 6px;
  border: 2px solid #317cd8;
  border-radius: 1px;
  margin-bottom: 6px;
section {
  max-width: 680px;
  margin: auto;
by Janeth Kent Date: 17-11-2020 css flexbox webdesign hits : 1088  
Janeth Kent

Janeth Kent

Licenciada en Bellas Artes y programadora por pasión. Cuando tengo un rato retoco fotos, edito vídeos y diseño cosas. El resto del tiempo escribo en MA-NO WEB DESIGN END DEVELOPMENT.


Related Posts

Starting with Bootstrap-Vue step by step

Today we will show you how to use BootstrapVue, describe the installation process and show basic functionality. The project’s based on the world's most popular CSS framework - Bootstrap, for building…

Bootstrap 5 beta2. What offers?

Since the release of the Bootstrap 4 is three years, in this article we will present what is new in the world’s most popular framework for building responsive, mobile-first sites.…

Creating simple CSS spinner-loader

In today's article we will show you how to animate a basic loader that spins when some predefined action is defined, such as loading an image. That can be used…

How to use Parallax.js effect on your website

Today, we're going to write about the parallax effect, similar to parallax scrolling, and how to implement it to improve your landing page. In webdev, they say mobile first -…

How to make the website's dark mode persistent with Local Storage, CSS and JS

Recently we wrote about how to do a switchable alternative color mode or theme, a very useful and popular feature to websites. Today’s article is going to be about how…

Dark Mode on website using CSS and JavaScript

In today’s article we are going to learn how to build pretty much standard these days on the web pages and that is the alternative color mode and switching between…

How To Add Filter Effects to Images with CSS

To achieve interesting effects on your images, learn about the 'filter' and 'Backdrop-Filter' properties of CSS. CSS filters are a very attractive feature of CSS that allows you to apply certain…

Introduction to BEM (Block Element Modifier)

Problems with naming CSS classes I think I might not be the only one with this experience: after finally grasping all the important concepts regarding CSS I wanted to start giving…

Styling React components: recommendations and suggestions

With CSS we can set the presentation of a document through the rules that control the formatting of an element on a web page. Using CSS techniques, we can make…

How To Create a Logo SVG Animation Using Only CSS

Web animations are a joy. They enhance the user experience by providing visual feedback, guiding tasks, and vitalize a website. Web animations can be created in several ways, including JavaScript libraries,…

CSS in JavaScript (CSS-in-JS) Vs CSS in CSS

What actually is CSS-in-JS? You've certainly heard or read words like CSS-in-JS, Styled Components, Radium, Aphrodite and you're and you're stuck in limbo there, "why is it? I'm completely happy about CSS…

Best 5 Frontend Development Topics in 2019

Ah, a new year. A time of positivity, a time to restart. For new objectives and proposals. And in the world of developers, to look at the scenery and decide…

We use our own and third-party cookies to improve our services, compile statistical information and analyze your browsing habits. This allows us to personalize the content we offer and to show you advertisements related to your preferences. By clicking "Accept all" you agree to the storage of cookies on your device to improve website navigation, analyse traffic and assist our marketing activities. You can also select "System Cookies Only" to accept only the cookies required for the website to function, or you can select the cookies you wish to activate by clicking on "settings".

Accept All Only sistem cookies Configuration