With this we control how an element is rendered:
The flexbox layout aims to provide an efficient way to layout items in a container, even when their size is unknown.
The container that is styled with display: flex or display: inline-flex.
.flex-container {
display: flex;
}
Any element that is a direct child of a flex container.
.flex-container {
display: flex;
}
Flex Item 1
Flex Item 2
Flex Item 3
.parent {
display: flex;
}
<div class="parent">
<div>Item 1</div>
<div>Item 2</div>
<div>Item 3</div>
</div>
main axis from left to right
Flexbox has some properties for the parent container and some for the individual items
Controls how elements are layouted along the main axis
.parent {
display: flex;
justify-content: flex-end;
}
<div class="parent">
<div>Item 1</div>
<div>Item 2</div>
<div>Item 3</div>
</div>
Controls how elements are layouted along the cross axis
.parent {
display: flex;
align-items: flex-start;
}
<div class="parent">
<div>Item 1</div>
<div>Item 2</div>
<div>Item 3</div>
</div>
What if we want to layout vertically instead of horizontally?
We can switch the main and cross axis with flex-direction!
Controls which axis is the main axis (horizontal/vertical)
It's also possible to reverse the direction
hint: flex-start and flex-end are also reversed
.parent {
display: flex;
flex-direction: row-reverse;
justify-content: flex-end;
}
What if we don't want to layout all items along the cross axis the same way?
controls alignment along cross axis for individual element (similar to align-items)
.parent { align-items: flex-start; }
.child-end { align-self: flex-end; }
<div class="parent">
<div>Item 1</div>
<div class="child-end">Item 2</div>
<div>Item 3</div>
</div>
Sets the initial size of a flex item along the main axis - useful in combination with flex-grow (next)
What if we want to make sure that the items use up all the available space?
What if we change the order in which individual elements appear?
<div>
<div style="order: 1">order: 1</div>
<div style="order: 5">order: 5</div>
<div style="order: -1">order: -1</div>
<div style="order: 3">order: 3</div>
<div style="order: 2">order: 2</div>
</div>
What if we have too many child items for one line?
Controls if and how elements are wrapped on new lines
Control the gaps between rows and columns
.parent {
display: flex;
align-items: flex-start;
flex-wrap: wrap;
row-gap: 30px;
column-gap: 10px;
}
Control how extra space on the cross axis is allocated
Member since 2015
4600 Olten
Vintage Jacket
CHF 65
Blazer Black
CHF 115
Nike Air Force
CHF 95
Go to the code sandbox and try to build the person tile after the design from figma
Member since 2015
4600 Olten
Vintage Jacket
CHF 65
Blazer Black
CHF 115
Nike Air Force
CHF 95
.personal-header {
display: flex;
align-items: center;
gap: 16px;
}
.product {
display: flex;
align-items: center;
gap: 16px;
}
.actions-container {
display: flex;
justify-content: space-between;
margin-top: 24px;
}