Flexbox Module: Axis Alignment

Henna Singh
4 min readFeb 16, 2022

Ok, I am back on Flexbox Layout :D I have started to get the hang of it and I am liking it. Only If magically I can get some design skills as well …👽

Today will see some alignment properties on the Flexbox container. These appear much easier than the complex flex-grow and flex-shrink and much comfortable to use.

Justify-content

By default, flex-items are grouped together at the main-start. This property allows distributing the remaining space between the items by setting different gaps and margins instead of changing the item’s size. The possible values are:

.container {  
justify-content: flex-start | flex-end | center | space-between | space-around | space-evenly | start | end | left | right
}
  • flex-start (default): items are packed toward the start of the flex-direction.
  • flex-end: items are packed toward the end of the flex-direction.
  • start: items are packed toward the start of the writing-mode direction.
  • end: items are packed toward the end of the writing-mode direction.
  • left: items are packed toward the left edge of the container, unless that doesn’t make sense with the flex-direction, then it behaves like start.
  • right: items are packed toward the right edge of the container unless that doesn’t make sense with the flex-direction, then it behaves like end.
  • center: items are centered along the line.
  • space-between: items are evenly distributed in the line; the first item is on the start line, the last item is on the end line.
  • space-around: items are evenly distributed in the line with equal space around them. The first item will have one unit of space against the container edge, but two units of space between the next item because that next item has its own spacing that applies.
  • space-evenly: items are distributed so that the spacing between any two items (and the space to the edges) is equal.

Align-items

This defines the default behavior for how flex items are laid out along the cross axis on the current line. Think of it as the justify-content version for the cross-axis (perpendicular to the main-axis). The popular values are

  • stretch (default): stretch to fill the container (still respect min-width/max-width)
  • flex-start / start / self-start: items are placed at the start of the cross axis. The difference between these is subtle and is about respecting the flex-direction rules or the writing-mode rules.
  • flex-end / end / self-end: items are placed at the end of the cross axis. The difference again is subtle and is about respecting flex-direction rules vs. writing-mode rules.
  • center: items are centered in the cross-axis
  • baseline: items are aligned such as their baselines align

Align-self

While align-items declares the behavior of all the items, align-self defines the alignment of the single item it's applied to. The values of this property are the same except for one external value:

  • align-self: auto; makes the item inherit align-items value from the parent or makes it stretch if the parent doesn't have one.

Notice that even if align-items is applied to the flex container, align-self applied to the element will be counted anyway.

<div class="flex-container">
<div class="flex-item item1">item1</div>
<div class="flex-item item2">item2</div>
<div class="flex-item item3">item3</div>
<div class="flex-item item4">item4</div>
</div>
CSS
.flex-container{
color: brown;
display: flex;
font-family: 'Montserrat', sans-serif;
padding: .5em 0 .5em 0;
width: 40em;
align-items: flex-start;
}

.flex-item{
border: 1px solid brown;
border-radius: 10px;
text-align: center;
height:2em;
line-height: 2em;
margin-right: 0.5em;
margin-left: 0.5em;
flex-basis: 6em;
flex-grow: initial;
}

.item2{
align-self: flex-end;
}

Align-content

This property defines the distribution of the free space between the lines located along the main axis if there is enough free space on the cross axis. Okay, once again: if there are several lines in one flex container and there are some available pixels between them — align-content is to distribute those pixels between those lines. It works similar to justify-content, but aligns lines, not items. The possible values are:

  • normal (default): items are packed in their default position as if no value was set.
  • flex-start / start: items packed to the start of the container. The (more supported) flex-start honors the flex-direction while start honors the writing-mode direction.
  • flex-end / end: items packed to the end of the container. The (more support) flex-end honors the flex-direction while end honors the writing-mode direction.
  • center: items centered in the container
  • space-between: items evenly distributed; the first line is at the start of the container while the last one is at the end
  • space-around: items evenly distributed with equal space around each line
  • space-evenly: items are evenly distributed with equal space around them
  • stretch: lines stretch to take up the remaining space

Note: This property only takes effect on multi-line flexible containers, where flex-wrap is set to either wrap or wrap-reverse). A single-line flexible container (i.e. where flex-wrap is set to its default value, no-wrap) will not reflect align-content.

Summary

How to operate with free space inside of the flex container in any possible case:justify-content is for the in-line free space, align-self and align-items are for the free space on the cross axis, and align-content is for the free space in case there are several lines.

With this, I am finished with Flexbox Module. It was a fun ride. 🚘

--

--

Henna Singh

Technical Speaker and an aspiring writer with avid addiction to gadgets, meet-up groups, and paper crafts. Currently pursuing Full Stack Bootcamp from Nucamp