Creating a flex container
Turn on Flexbox layout with the display
CSS property
Create flex containers with display: flex
and display: inline-flex
.
The contents are laid out with Flexbox, not the element whose display
value you changed
It’s the contents of an element with display: flex
or display: inline-flex
that will be laid out with Flexbox layout — not the element on which you set display
. These contents are referred to as flex items, and the element with display: flex
or display: inline-flex
is referred to as the flex container.
Outer display type of a flex container
display
lets you set not just how the contents of an element should be laid out, but also how the element’s box should behave if placed in Flow layout by its ancestor.
flex
has an outside display type of block
.
inline-flex
has an outside display type of inline
.
Replaced elements can’t lay out their contents with Flexbox
The contents of elements like svg
or img
can’t be laid out with Flexbox. These elements are so called replaced elements, which means what goes inside their content box is dictated by something else than CSS. You can technically set display: flex
or display: inline-flex
on an svg
element, but its contents will not be treated as flex items.
Initial CSS property values for Flexbox-related properties
Remember that the browser calculates a computed value of every single CSS property for every single element node in the DOM tree. Every CSS property starts with an initial value, so there’s always a value even if you don’t set one explicitly. Some CSS properties apply only when Flexbox layout is applied. For example, flex-basis
or flex-grow
. So when you change display
and turn on Flexbox for an element’s contents, all these property values will start having an effect.
Change the display
of the container element, not the element whose box you want to grow or shrink
Don’t set display: flex
on the element you want to grow or shrink. Set it on the element that contains this element instead.
Escaping Flexbox layout
Can a child element avoid being laid out with Flexbox if a parent element has display: flex
? Yes, with position: absolute
and position: fixed
. These elements are considered out-of-flow, as in their boxes are not considered flex items.