To create columns, we use Flexbox.
By default, when setting display: flex
on an element, all the children will become columns.
.container {
display: flex;
}
display: inline-flex
allows multiple flex containers to appear inline with each other.
Flex containers can be nested inside each other by declaring display: flex
or display: inline-flex
for children of flex containers.
We use a justify-content
property to position the items from left to right.
.container {
display: flex;
justify-content: flex-end;
}
There are five values for the justify-content property:
flex-start
: all items are positioned from the left of the parent container, with no extra space between or before them.flex-end
: all items are positioned with the last item starting on the right side of the parent container, with no extra space between or after them.center
: all items are positioned in the center of the parent container with no extra space before, between, or after them.space-around
: items are positioned with equal space before and after each item, double the space between elements.space-between
: items are positioned with equal space between them, but no extra space before or after the last elements.#flexstart {
justify-content: flex-start;
}
#flexend {
justify-content: flex-end;
}
#center {
justify-content: center;
}
#spacearound {
justify-content: space-around;
}
#spacebetween {
justify-content: space-between;
}
align-items
align flex items vertically within the container.
flex-start
: all elements are at the top of the parent container.flex-end
: all elements are at the bottom of the parent container.center
: the center of all elements is positioned halfway between the top and bottom of the parent container.baseline
: the bottom of the content of all items are aligned with each other.stretch
: the items will stretch from top to bottom of the container if possible.#flexstart {
align-items: flex-start;
}
#flexend {
align-items: flex-end;
}
#center {
align-items: center;
}
#baseline {
align-items: baseline;
}
align-items can align elements within a single row.
If a flex container has multiple rows of content, align-content
can space the rows from top to bottom.
align-content accepts six values:
flex-start
: all rows of elements are positioned at the top of the parent container with no extra space between.flex-end
: all rows of elements are positioned at the bottom of the parent container with no extra space between.center
: all rows of elements are positioned at the center of the parent element with no extra space between.space-between
: all rows of elements are spaced evenly from the top to the bottom of the container with no space above the first or below the last.space-around
: all rows of elements are spaced evenly from the top to the bottom of the container with the same amount of space at the top and bottom and between each element.stretch
: if a minimum height or no height is specified, the rows of elements stretch to fill the parent container from top to bottom (default value).#flexstart {
align-content: flex-start;
}
#flexend {
align-content: flex-end;
}
#center {
align-content: center;
}
#between {
align-content: space-between;
}
#around {
align-content: space-around;
}
flex-grow
specifies if items should grow to fill a container and which ones should grow proportionally more or less than others.
.container {
display: flex;
}
.side {
width: 100px;
flex-grow: 1;
}
.center {
width: 100px;
flex-grow: 2;
}
flex-shrink
specifies which elements will shrink and in what proportions.
The default value of flex-shrink
is 1.
flex-basis
specifies the width of an item before it stretches or shrinks.
.side {
flex-grow: 1;
flex-basis: 150px;
}
The flex property can declare flex-grow, flex-shrink, and flex-basis all in one line.
.side {
flex: 2 1 150px;
}
flex-wrap
moves flex items to the next line when necessary.
wrap-reverse
: similar to wrap, but in reverse order.nowrap
: prevents items from wrapping. This is the default value.#wrap {
flex-wrap: wrap;
}
#nowrap {
flex-wrap: nowrap;
}
#reverse {
flex-wrap: wrap-reverse;
}
Flex containers have two axes: a major axis and a cross axis.
The major axis has these properties:
The cross axis has these properties:
We can switch the major axis and cross axis with flex-direction
. For example, if we add flex-direction: column;
, the flex items will be ordered vertically, not horizontally.
The flex-direction
accepts these values:
#row {
flex-direction: row;
}
#row-reverse {
flex-direction: row-reverse;
}
#column {
flex-direction: column;
}
#column-reverse {
flex-direction: column-reverse;
}
flex-flow
declares both the flex-wrap
and flex-direction
.
#row-reverse {
flex-flow: wrap row-reverse;
}
#column {
flex-flow: wrap column;
}