Day 13. Relearning Flexbox
October 15, 2019
Notes
- The differences between
inline-flex
andflex
:display:flex
elements will span the 100% of available space whileinline-flex
will span the culmulative width of the children. - The default value of
flex-direction
isrow
. Other values arecolumn
,row-reverse
andcolumn-reverse
. - The
main axis
in flexbox goes from left to right. While the cross axis goes from top to bottom. This is the reverse when you change theflex-direction
to column. - If the
flex-direction
property of an element is set torow
and theflex-wrap
property is set towrap
, the height of the flex items always sum to the height of the flex container. - Unlike CSS Grids, flexbox doesn’t provide a flexible way to add gaps or gutters to layout items. I normally make use of a percentage margin like so.
.flex {
display: flex;
}
.flex__item {
flex: 33%;
margin: 1%;
}
This mostly gets the job done for me, but I learned of a more flexible way to do so with the calc()
function.
.flex {
display: flex;
}
.flex__item {
flex: calc(33.33% - 20px);
}
- How the
order
property works in flexbox: All flex items have a default order of0
. Setting theorder
property on a flex item automatically takes it to the end of all the flex parent. You have to explicitly set the order of all the elements you want to reorder. Justify-content
specifies how the elements are aligned on themain axis
. The options areflex-state
(default),flex-end
(All items are flexed to the end).center
(All elements at the center).space-between
,space-around
.align-items
enables positioning items in the cross axis (y-axis whenflex-direction
is set torow
. The default value isstretch
. It shares the same values with thejustify-content
property. Except that it adds thebaseline
value that’ll align items according to the base of the text-content of the flex children.- Differences between
align-content
andalign-items
. align-items will position flex items in relation to their rows, while align-content will do so in relation to the whole flex-container. Here’s a perfect example…
<div class="div">
<li class="item">Something</li>
<li class="item">Something</li>
<li class="item">Something</li>
<li class="item">Something</li>
<li class="item">Something</li>
<li class="item">Something</li>
<li class="item">Something</li>
<li class="item">Something</li>
<li class="item">Something</li>
<li class="item">Something</li>
<li class="item">Something</li>
<li class="item">Something</li>
</div>
and CSS
<style>
li {
list-style: none;
}
.div {
display: flex;
flex-wrap: wrap;
/* align-items will position elements along the cross-axis of their individual rows */
align-items: center;
height: 80vh;
}
.item {
flex-basis: 33%;
border: 1px solid red;
}
</style>
Here’s what the code will result in
When we change the align-items
property to align-content
, here’s what we get.
That’s all for today. See you!