Ovidiu Olea
The main idea behind the flex layout is to give the container the ability to alter its items' width/height (and order) to best fill the available space
When working with flexbox you need to think in terms of two axes, the main axis and the cross axis.
The main axis is defined by the flex-direction property, and the cross axis runs perpendicular to it.
The main axis is defined by flex-direction, which has four possible values:
If your main axis is column or column-reverse then the cross axis runs along the rows.
Parent Properties
Children Properties
flex-direction property, defines the direction items are placed in the container, and accepts the following values:
.flex-container {
display: flex;
flex-direction: row | column;
background-color: blue;
}
.flex-container > div {
background-color:#f1f1f1;
width: 100px;
margin:10px;
text-align: center;
line-height: 75px;
font-size: 30px;
}
row
row-reverse
column
column-reverse
flex-wrap property, specifies whether the flex items should wrap or not and accepts the following values:
.flex-container {
display: flex;
flex-wrap: wrap;
background-color: blue;
}
.flex-container > div {
background-color:#f1f1f1;
width: 100px;
margin:10px;
text-align: center;
line-height: 75px;
font-size: 30px;
}
wrap
nowrap
wrap-reverse
flex-flow property is a shorthand property for setting both the flex-direction and flex-wrap properties.
.flex-container {
flex-flow: flex-direction flex-wrap;
}
.flex-container {
display: flex;
flex-flow: row wrap;
background-color: Blue;
}
.flex-container > div {
background-color: #f1f1f1;
width: 100px;
margin: 10px;
text-align: center;
line-height: 75px;
font-size: 30px;
}
justify-content property, let you align items horizontally and accepts the following values:
<div class="flex-container">
<div>1</div>
<div>2</div>
<div>3</div>
</div>
.flex-container {
display: flex;
background-color: blue;
justify-content: center;
}
.flex-container > div {
background-color:#f1f1f1;
margin:10px;
padding:20px;
font-size:30px;
}
center
flex-end
flex-start
space-around
space-between
align-items property, let you align items vertically and accepts the following values:
.flex-container {
display: flex;
background-color: dodgerblue;
align-items:center;
}
.flex-container > div {
background-color:#f1f1f1;
margin:10px;
padding:20px;
font-size:30px;
}
center
flex-start
flex-end
stretch
order property specifies the order of the flex items.
.item {
order: <number>;
}
<div class="flex-container">
<div style="order: 3">1</div>
<div style="order: 2">2</div>
<div style="order: 4">3</div>
<div style="order: 1">4</div>
</div>
order
flex-grow property specifies how much a flex item will grow relative to the rest of the flex items.
.item {
flex-grow: <number>;
}
<div class="flex-container">
<div>7</div>
<div>6</div>
<div style="flex-grow:2">8</div>
</div>
flex-shrink property specifies how much a flex item will shrink relative to the rest of the flex items.
.item {
flex-shrink: <number>;
}
<div class="flex-container">
<div>1</div>
<div>2</div>
<div style="flex-shrink:2">3</div>
<div>4</div>
<div>5</div>
<div>6</div>
<div>7</div>
<div>8</div>
<div>9</div>
<div>10</div>
</div>
flex-basis property specifies the initial length of a flex item.
.item {
flex-basis: <number>;
}
<div class="flex-container">
<div>1</div>
<div>2</div>
<div style="flex-basis: 400px">3</div>
<div>4</div>
</div>
flex property is a shorthand property for the flex-grow, flex-shrink, and flex-basis properties.
.item {
flex: flex-grow flex-shrink flex-basis;
}
<div class="flex-container">
<div>1</div>
<div>2</div>
<div style="flex: 0 0 200px">3</div>
<div>4</div>
</div>
Third flex item not growable (0), not shrinkable (0), and with an initial length of 200 pixels:
align-self property specifies the alignment for the selected item inside the flexible container.
<div class="flex-container">
<div>1</div>
<div>2</div>
<div style="align-self: center">3</div>
<div>4</div>
</div>