Flexbox is more that a single property, it is a whole toolbox.
Some of its property belong on flex containers and other on flex items.
A flex container is an element that has the display: flex; on it.
Note: a element can be both a flex container and items
The flex property apply to an flex item.
It affect how flex items size themselves within their container.
It is a shorthand for the three property:
.item {
flex: 1;
}
A B C
+---------+ +----------+ +----------+
¦ ¦ ¦ ¦ ¦ ¦
¦ ◼ ◼ ◼ ¦ ¦ ▀▀ ▀▀ ▀▀ ¦ ¦▀▀ ▀▀ ▀▀▀▀¦
¦ ¦ ¦ ¦ ¦ ¦
¦ ¦ ¦ ¦ ¦ ¦
+---------+ +----------+ +----------+
no flex equal flex unequal flex
no flex property on the itemsflex: 1; on each itemsflex: 1; on the first two items, flex: 2; on the lastThe flexbox operate on two axis.
main axis or flex-direction: row;cross axis or flex-direction: column;By default, the main axis is horizontal.
The cross axis is vertical.
They are inverted if the flex-direction property is set to vertical on the container.
---main---> ---cross--->
+---------+ | +---------+ |
¦ ¦ | ¦ ¦ |
¦ ◼ ◼ ¦cross ¦ ◼ ¦ main
¦ ¦ | ¦ ◼ ¦ |
¦ ¦ | ¦ ¦ |
+---------+ ↓ +---------+ ↓
default flex-direction: vertical
Note: flex-direction: horizontal == default
Some apply to containers and other to items.
The justify-content property apply to the container.
It defines the horizontal alignments of its items.
+---------+ +---------+ +---------+
¦ ◼◼◼ ¦ ¦ ◼◼◼ ¦ ¦ ◼◼◼ ¦
+---------+ +---------+ +---------+
Flex-start center flex-end
+---------+ +---------+
¦ ◼ ◼ ◼ ¦ ¦◼ ◼ ◼¦
+---------+ +---------+
space-around space-between
The align-items property apply to the container.
It defines the vertical alignments of its items.
+---------+ +---------+ +---------+ +---------+
¦ ¦ ¦ ◼ ¦ ¦ ¦ ¦ █ ¦
¦ ¦ ¦ ◼◼◼ ¦ ¦ ◼ ¦ ¦ █ ¦
¦ ◼ ¦ ¦ ¦ ¦ ◼◼◼ ¦ ¦ █ █ █ ¦
¦ ◼◼◼ ¦ ¦ ¦ ¦ ¦ ¦ █ █ █ ¦
+---------+ +---------+ +---------+ +---------+
end start center strech
You can target a specific item with
align-selfproperty on the item.
Let the overflow item wrap and create a grid with the flex-wrap property.
It apply to the container.
+---------+ +---------+
¦ ◼ ◼ ◼ ◼ ¦◼ ◼ ◼ ¦ ◼ ◼ ◼ ◼ ¦
¦ ¦ ¦ ◼ ◼ ◼ ¦
¦ ¦ ¦ ¦
¦ ¦ ¦ ¦
+---------+ +---------+
nowrap wrap
The flex-direction property apply to the container.
It refers whether its items are render horizontally or vertically.
+---------+ +---------+
¦ ¦ ¦ ◼ ¦
¦ ◼ ◼ ◼ ◼ ¦ ¦ ◼ ¦
¦ ¦ ¦ ◼ ¦
¦ ¦ ¦ ◼ ¦
+---------+ +---------+
row column
Note: When rotating the direction of a container, the properties
justify-contentandalign-itemsare inverted More on this.
Flex containers only know the position of elements one level below. It means you can group flex items by wrapping them inside an extra div.
A B
+---------+ +---------+
¦◼ ◼ ◼¦ ¦◼ ◼ ◼¦
+---------+ +---------+
No grouping Grouped items
In example B, the two items on the right are group in a common div. The flex container treat this div as a single item and does not apply to the items inside.
Flex item can be placed more precisely inside their container with the margin property.
A B
+---------+ +---------+
¦◼◼◼ ¦ ¦◼ ◼◼¦
+---------+ +---------+
default place margin-left: auto; on the middle item
Applied on the flex container, set a gab between the items.
.container {
gap: 2px;
}