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 : 843  
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

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…

A roadmap to becoming a web developer in 2019

There are plenty of tutorials online, which won't cost you a cent. If you are sufficiently self-driven and interested, you have no difficulty training yourself. The point to learn coding…

The Absolute Beginner's Guide to Sass

You've probably heard about CSS preprocessors before, whether it's Sass, LESS or Stylus, and they're all great tools to maintain your CSS, especially when you work with large codebases. For…

How To Build A Presentation Using HTML, CSS, & JavaScript

The process of conceiving and constructing a presentation is often hard. Sometimes we're stuck with Keynote or PowerPoint, and the templates are extremely limited and generic. Today, we're going to learn how…

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