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
 

TRY THE CODE

 
<section>
<div class="flex row-decoration">
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
</div>
<div class="flex row-decoration">
<div class="cell"></div>
<div class="cell"></div>
<div class="cell" style="margin-left: auto"></div>
</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> 
<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>
<div class="flex row-decoration" style="height: 120px">
<div class="cell" style="margin: auto"></div>
</div>  
</section>
 

THE CSS

 
.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 : 3335  
 
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 AND DEVELOPMENT.

 
 
 

Related Posts

New graphic window units - CSS

''Intercop 2022' is a project announced by Google, Microsoft, Apple and the Mozzilla Foundation to make all browsers offer the same web experience. The project has 15 focus areas and one…

Let's create a Color Picker from scratch with HTML5 Canvas, Javascript and CSS3

HTML5 Canvas is a technology that allows developers to generate real-time graphics and animations using JavaScript. It provides a blank canvas on which graphical elements, such as lines, shapes, images…

Why shouldn't we use black?

Nowadays it is becoming more and more common for web pages to have the option to set them in dark mode, or to base their aesthetics directly on black or…

How to make your own custom cursor for your website

When I started browsing different and original websites to learn from them, one of the first things that caught my attention was that some of them had their own cursors,…

Nesting: future proofing CSS

Although not currently supported by browsers, there is a proposal for CSS nesting to support a feature that would provide better readability to native CSS, so in the future it…

Use the SRCSET attribute to improve your SEO

There is a new standard HTML attribute that can be used in conjunction with IMG called SRCSET. It is new and important as it allows webmasters to display different images…

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…