Display Flex Align Items: Tutorial (Examples)
See the illustration to understand how to work with Align Items in Flexbox
You need to master both Flexbox and CSS Grid in order to professionally build modern websites & web apps. If you haven't mastered both of them yet, I highly recommend going through my CSS Course.
To start using Flexbox, you only have to use display: flex
on an element. That will give you this default layout:
The element with display: flex
will become the so-called flex-container, and its direct child elements become the flex-items.
We have now 'unlocked' Flexbox functionalities.
We can then use align-items
to determine where along the vertical axis all the flex-items should be positioned.
For example, to position them in the center:
Or at the bottom:
Note that we used end
here, but flex-end
has better browser support. However, flex-end
is being phased out, because the Flexbox & CSS Grid properties are being harmonized (so we can use the same properties in both systems). So the 'flex-' and 'grid-' prefixes get cut.
When you use flex-direction: column
to change the direction that the flex-items should follow, things change.
Now 'align-items' doesn't determine where flex-items should be positioned along the vertical axis, but along the horizontal axis. And the justify-content
property, that we haven't discussed here yet but is what you would use for horizontal positioning in the default situation, is now used for vertical alignment.
So things 'flip' when you use flex-direction: column
.
If we now use align-items: center
, we get this:
If you just want to align an individual flex-item, you can use align-self
. An example:
Remember, when flex-direction: column
, things switch:
By the way, I think CSS is the 'bottleneck' to most websites & web apps. I believe it's the highest-ROI skill you can master.
Before I mastered CSS, I lost a ton of time & energy fiddling around with CSS.
I was learning about advanced JavaScript topics when I couldn't even implement basic layouts in CSS...
So I created a CSS course to help you avoid the same mistake. Check it out here if you're interested.
Would you like to be notified when I release a new course? You'll get early bird discounts. Sign up for my newsletter here.