- Introduction
- Flexbox Basic Concepts
- flex-direction
- justify-content
- align-content
- align-items
- flex-wrap
- flex-flow
- order
- align-self
- flex-grow
- flex-shrink
- flex-basis
- flex
- flexbox (Flexible Box Module) is one of the two modern layouts systems, along with CSS Grid
- CSS Grid is bi-dimensional, flexbox is one-dimensional layout model, which controls the layout based on a row or on a column, but not together at the same time
- it's main goal is to allow items to fill the whole space offered by their container, depending on some rules we set
- to support older browsers like IE8 and IE9, use:
- table layouts
- floats
- clearfix hacks
display: tablehacks
display: flex;is used to apply flexbox layout to a container- content inside the container will be aligned using flexbox
- some flexbox properties apply to the container, which sets the general rules for its items:
flex-directionjustify-contentalign-itemsalign-contentflex-wrapflex-flow
- some other properties apply to each individual item inside the container:
orderalign-selfflex-growflex-shrinkflex-basisflex
flex-directiondetermines if the container should align its items as row, or as columnrow(default) places items in a row, in the text direction (left-to-right)row-reverseplaces items just like row but in the opposite directioncolumnplaces items in a column, ordering top to bottomcolumn-reverseplaces items in a column, just like column but in the opposite direction
justify-contentchange the horizontal alignmentflex-startalign to the left side of the containerflex-endalign to the right side of the containercenteralign at the center of the containerspace-betweendisplay with equal spacing between themspace-arounddisplay with equal spacing around them
align-contentchange the vertical alignment when it spans over multiple linesflex-startalign to the top of the containerflex-endalign to the bottom of the containercenteralign at the vertical center of the containerspace-betweendisplay with equal spacing between themspace-arounddisplay with equal spacing around themstretchitems are stretched to fit the container
- codepen demo of
align-contentused in practice
align-itemslets us dispose elements on the cross axis instead than on the main axisflex-startalign to the top of the containerflex-endalign to the bottom of the containercenteralign at the vertical center of the containerbaselinedisplay at the baseline of the containerstretchitems are stretched to fit the container
- by default, items in a flexbox container are kept on a single line, shrinking them to fit in the container
flex-wrap: wrapforce the items to spread across multiple lines- distribute the items according to the order set in
flex-direction
- distribute the items according to the order set in
wrap-reversereverses this order
flex-flowis a shorthand property that allows us to specifyflex-directionandflex-wrapin a single lineflex-directionvalue first, followed byflex-wrapvalue, e.g.,flex-flow: row wrap
- items are ordered based on a order they are assigned
- by default every item has order
0and the appearance in the HTML determines the final order
- by default every item has order
orderproperty overrides the order set on each item- set on the item, not the container
- negative value makes the item appears before all the other items
align-selfcan override the containeralign-itemssetting for an itemflex-startalign to the top of the containerflex-endalign to the bottom of the containercenteralign at the vertical center of the containerbaselinedisplay at the baseline of the containerstretchitems are stretched to fit the container
- default for any item is 0
- if all items are defined as 1 and one is defined as 2, the bigger element will take the space of two "1" items
- default for any item is 1
- if all items are defined as 1 and one is defined as 3, the bigger element will shrink 3x the other ones
- when less space is available, it will take 3x less space
- if set to
auto, it sizes an item according to its width or height, and adds extra space based on theflex-growproperty - if set to 0, it does not add any extra space for the item when calculating the layout
- if specify a pixel number value, it will use that as the length value (width or height depends if it's a row or a column item)
flexis a shorthand property that combines the 3 properties:flex-growflex-shrinkflex-basis- e.g.,
flex: 0 1 auto