Flexbox Flashcards
What is flexbox?
The Flexible Box Module, usually referred to as flexbox, was designed as a one-dimensional layout model, and as a method that could offer space distribution between items in an interface and powerful alignment capabilities.
When we describe flexbox as being one dimensional we are describing the fact that flexbox deals with layout in one dimension at a time — either as a row or as a column. This can be contrasted with the two-dimensional model of CSS Grid Layout, which controls columns and rows together.
Flexbox is unlike previous display values (inline
, inline-block
, and so on), which only affect the elements they are applied to. Instead, a flex container asserts control over the layout of the elements within.
Source MDN
What does display: flex; do to an element?
Applying display: flex;
to an element turns it into a flex container, and its direct children turn into flex items.
- The flex container fills the available
width
like a block element. - The flex items may not necessarily fill the
width
of their flex container. - The flex items are all the same
height
, determined naturally by their contents.
Source: Keith J. Grant (2018). CSS in Depth. Manning Publications.
What does display: inline-flex do to an element?
display: inline-flex
creates a flex container that behaves more like an inline-block
element rather than a block
. It flows inline with other inline elements, but it won’t automatically grow to 100% width. Flex items within it generally behave the same as with display: flex
.
Source: Keith J. Grant (2018). CSS in Depth. Manning Publications.
How can you create a flex container?
By applying
display: flex;
to the container element, it becomes a fex container
. Its child elements will become the same height by default.
Source: Keith J. Grant (2018). CSS in Depth. Manning Publications.
What are the two axes of flexbox?
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.
source MDN
What are the possible values of the flex-direction property?
-
row
- Main axis will run along the row in the inline direction (same as writing mode, left-to-right for english). -
row-reverse
- Main axis will run along the row in the reverse inline direction (reverse of writing mode, right-to-left for english). -
column
- Main axis will run from the top of the page to the bottom — in the block direction. -
column-reverse
- Main axis will run from the bottom of the page to the top — in the reverse block direction.
Source MDN
What is the flexbox cross axis if you define the main axis with flex-direction: row/row-reverse;
?
The cross axis runs perpendicular to the main axis, therefore if your flex-direction
(main axis) is set to row
or row-reverse
the cross axis runs down the columns.
Source: Keith J. Grant (2018). CSS in Depth. Manning Publications.
What is the flexbox cross axis if you define fine the main axis with flex-direction: column/column-reverse;
?
If your main axis is column
or column-reverse
then the cross axis runs along the rows.
Source: Keith J. Grant (2018). CSS in Depth. Manning Publications.
What is a flex item?
The direct children of a Flex Container (elements with display: flex
or display: inline-flex
set on them) become flex items.
Continuous runs of text inside flex containers will also become flex items.
Source MDN
What are the peculiarities of flexbox start/end of lines?
If the flex-direction is row and I am working in English, then the start edge of the main axis will be on the left, the end edge on the right.
If I were to work in Arabic, then the start edge of my main axis would be on the right and the end edge on the left.
After a while, thinking about start and end rather than left and right becomes natural, and will be useful to you when dealing with other layout methods such as CSS Grid Layout which follow the same patterns.
Source MDN
What is a flex container?
An area of a document laid out using flexbox is called a flex container. To create a flex container, we set the value of the area’s container’s display
property to flex
or Inline-flex
.
.container { display: flex; }
Source MDN
What does flex-wrap: wrap
do?
If you add flex-wrap
with a value of wrap
to the flex container. Should your items be too large to all display in one line, they will wrap onto another line.
NOTE: If the flex-direction
is column
or column-reverse
, then flex-wrap: wrap
will allow the flex items to overflow into a new column. However, this only happens if something constrains the height
of the container; otherwise, it grows to contain its flex items.
Source MDN
What does flex-wrap: nowrap
do?
If flex-wrap
is set to nowrap
, which is also the initial value, and the flex-items will shrink to fit the container because they are using initial flexbox values that allows items to shrink.
NOTE: Using nowrap
would cause an overflow if the items were not able to shrink, or could not shrink small enough to fit.
Source MDN
What are the possible values of flex-wrap?
flex-wrap
accepts the following values:
-
nowrap
- The flex items are laid out in a single line which may cause the flex container to overflow. This is the default value. -
wrap
- The flex items break into multiple lines. -
wrap-reverse
- Behaves the same as wrap butcross-start
andcross-end
are permuted.
Source MDN