This project is a sample of how to use the flexbox layout module.
The Flexbox Layout provides an efficient way to lay out, align and distribute space among items in a container, even if their size is unknown or dynamic.
The main goal of a flex layout is to give the container the ability to alter its items width and height to best fill the available space. The container expands items to fill the space or shrinks them to avoid overflow.
We can separate the flexbox system in two main groups, called container
and item(s)
, which will be
explained below.
A flex container is a container with dislay
property sets to flex
.
.container {
display: flex; /* or inline-flex */
}
We can see some important container's properties below:
-
flex-direction
Defines the direction flex items are placed in the flex container. It can has the following values:- row (default): left to right
- row-reverse: right to left
- column: top to bottom
- column-reverse: bottom to top
-
flex-wrap
Allow the items to wrap as needed. It can has the following values:- nowrap (default): all flex items will be on one line
- wrap: flex items will wrap onto multiple lines, from top to bottom
- wrap-reverse: flex items will wrap onto multiple lines from bottom to top
-
flex-flow
This is a shorthand for the flex-direction and flex-wrap properties.flex-flow: <‘flex-direction’> || <‘flex-wrap’>
-
justify-content
It distributes extra free space leftover when either all the flex items on a line are inflexible or are flexible but have reached their maximum size. It can has the following values:flex-start
(default): items are packed toward the start of the flex-directionflex-end
: items are packed toward the end of the flex-directionstart
: items are packed toward the start of the writing-mode directionend
: tems are packed toward the end of the writing-mode directionleft
: items are packed toward left edge of the container, unless that doesn't make sense with the flex-direction, then it behaves like startright: items
are packed toward right edge of the container, unless that doesn't make sense with the flex-direction, then it behaves like startcenter
: items are centered along the linespace-between
: items are evenly distributed in the line. First item is on the start line, last item on the end linespace-around
: items are evenly distributed in the line with equal space around them. Note that visually the spaces aren't equal, since all the items have equal space on both sides. 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. It can has the following values: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-axisbaseline
: items are aligned such as their baselines align
-
align-content
This aligns a flex container's lines within when there is extra space in the cross-axis, similar to how justify-content aligns individual items within the main-axis. It can has the following values:flex-start / start
: items packed to the start of the container. The 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 flex-end honors the flex-direction while end honors the writing-mode direction.center
: items centered in the containerspace-between
: items evenly distributed. The first line is at the start of the container while the last one is at the endspace-around
: items evenly distributed with equal space around each linespace-evenly
: items are evenly distributed with equal space around themstretch (default)
: lines stretch to take up the remaining space
The items are the contents packaged inside a container. We can see some important properties below.
-
order
The order property controls the order in which they appear in the flex container. -
flex-grow
This defines the ability for a flex item to grow if necessary. It dictates what amount of the available space inside the flex container the item should take up. -
flex-shrink
This defines the ability for a flex item to shrink if necessary. -
flex-basis
This defines the default size of an element before the remaining space is distributed. It can be a length (e.g. 20%, 5rem, etc.) or a keyword. The auto keyword means "look at my width or height property" (which was temporarily done by the main-size keyword until deprecated). The content keyword means "size it based on the item's content" - this keyword isn't well supported yet, so it's hard to test and harder to know what its brethren max-content, min-content, and fit-content do. -
flex
This is the shorthand for flex-grow, flex-shrink and flex-basis combined. The second and third parameters (flex-shrink and flex-basis) are optional. Default is 0 1 auto. -
align-self
This allows the default alignment (or the one specified by align-items) to be overridden for individual flex items. Its values are equals to thealign-items
property of containers.
These README explanations were got from CSS Tricks.