CSS Flexbox is a powerful layout module that provides an easy and efficient way to arrange items within a container. The properties are divided into those that apply to the flex container and those that apply to the flex items.
Flex Container Properties
display
- Values:
flex,inline-flex - Description: Defines a flex container and enables flexbox layout for its children.
- Values:
flex-direction
- Values:
row,row-reverse,column,column-reverse - Description: Specifies the direction of the flex items within the flex container.
- Values:
justify-content
- Values:
flex-start,flex-end,center,space-between,space-around,space-evenly - Description: Aligns the flex items along the main axis.
- Values:
align-items
- Values:
stretch,flex-start,flex-end,center,baseline - Description: Aligns flex items along the cross axis.
- Values:
flex-wrap
- Values:
nowrap,wrap,wrap-reverse - Description: Controls whether the flex container is single-line or multi-line, and the direction of the cross axis.
- Values:
align-content
- Values:
stretch,flex-start,flex-end,center,space-between,space-around - Description: Aligns the flex lines when there is extra space on the cross axis.
- Values:
gap
- Values:
length(e.g.,10px,1em) - Description: Sets the gap (gutters) between the flex items.
- Values:
Flex Item Properties
flex
- Values:
none,[ <'flex-grow'> <'flex-shrink'>? || <'flex-basis'> ] - Description: A shorthand for setting
flex-grow,flex-shrink, andflex-basistogether.
- Values:
align-self
- Values:
auto,flex-start,flex-end,center,baseline,stretch - Description: Overrides the
align-itemsproperty for individual flex items.
- Values:
order
- Values:
<integer> - Description: Controls the order of the flex items within the flex container.
- Values:
flex-grow
- Values:
<number>(default is0) - Description: Defines the ability of a flex item to grow if necessary.
- Values:
flex-shrink
- Values:
<number>(default is1) - Description: Defines the ability of a flex item to shrink if necessary.
- Values:
flex-basis
- Values:
auto,<length>(e.g.,20%,10px) - Description: Specifies the initial size of a flex item before the remaining space is distributed.
- Values:
Explanation of Usefulness
Flex Container Properties
- display: Without this, the flex container and its children will not be able to use any flex properties.
- flex-direction: Essential for determining the main axis along which items are laid out.
- justify-content: Crucial for defining how extra space is distributed along the main axis.
- align-items: Important for controlling alignment on the cross axis.
- flex-wrap: Useful for handling items when the container cannot fit them in a single line.
- align-content: Only relevant when there are multiple lines; controls spacing between lines.
- gap: Simplifies spacing between items without needing margins.
Flex Item Properties
- flex: A shorthand that often suffices for most use cases involving item flexibility.
- align-self: Provides item-level control over alignment.
- order: Simple yet powerful for reordering items without changing the HTML.
- flex-grow: Important for defining how items grow relative to each other.
- flex-shrink: Controls how items shrink relative to each other when space is tight.
- flex-basis: Allows explicit control over the initial size of items before space distribution.