CSS Flex—An Interactive Tutorial
Here’s a list of my best web development tutorials.
Complete CSS flex tutorial on Hashnode.
Ultimate CSS grid tutorial on Hashnode.
Higher-order functions .map, .filter & .reduce on Hashnode.
My Coding Books
I’ve gone over pretty much every single existing Flex tutorial from froggy to the official W3C documentation. But there is one thing you will never learn about Flex without an interactive example: the behavior of properties when the size of an *individual item changes. You can see what I mean below.
Check out CSS Visual Dictionary with all CSS properties explained visually.
I created this new Flex tool overnight and improved it over the course of the next few days. The rest of this tutorial will provide a few insights into what I discovered while playing around with it!
What I Learned About Flex While Designing The Flex Generator
There are a few interesting quirks about flex that may not always be obvious at first. For example, narrow items don’t stretch and behave like flex-start even if stretch is applied. Below I will walk you through the rest of them.
Here Are The Complete Visual Examples
Keep on reading if you want to consume this tutorial visually!
I’ll display all possible cases and briefly discuss the Flex quirks I found that you might want to know about (or, at some point will stumble into.)
The flex-start value is the default. There is really not much here other than it looks a lot like the slowly becoming obsolete float: left:
The flex-end value behaves similar to float: right; Here are two items:
Remember back in the day you instinctively typed float: center, but were disappointed at the fact that nothing happened in the browser? Flex to the rescue. The center value acts as you would expect:
The space-between, space-around (next one) and space-evenly seem to produce similar results, at first. But try changing the number of items. Then it becomes obvious that the three layout options have unique properties.
The stretch value is a bit quirky. It only stretches items if they are large enough. What I mean by “large enough” is usually at least 100% of the container. Try making them smaller. You will see that the items start to float left again, like with flex-start, even though it’s not present:
Finally, there is also a separate version of the flex editor. It contains only one row, but in addition, has shrink and expand buttons.
Because sometimes shrinking the width changes everything. Items might start behaving a lot like stretch, even though it isn’t set. But that’s natural working with Flex. Just something to be mindful of:
In the same way, this Flex generator helps you learn the behavior of flex layouts by interactively changing the justify-content property, modifying the number of items, and dragging and dropping (shrinking or stretching) the width of each individual item.
Limited Time Offer
The diagrams in this tutorial were influenced directly by the manuscript!
Medium Readers Only:
== grab a copy ==
Contains all CSS properties