Step-by-Step Explanation of justify-content, align-items, and align-content in CSS Flexbox.
1. justify-content (Main Axis Alignment)
The justify-content property is used to align flex items along the main axis of the container. By default, the main axis is horizontal (row), but it can be vertical if flex-direction: column is set. This property helps control the distribution of space between items. The possible values for justify-content are:
flex-start: This aligns all items at the start of the container, which, in a row layout, means they align to the left. In a column layout, they align at the top.
flex-end: Items align at the end of the container. In a row layout, this means items align to the right, and in a column layout, they align at the bottom.
center: This centers the items along the main axis. In a row layout, they will be horizontally centered, while in a column layout, they will be vertically centered.
space-between: Items are spread out, with the first item at the start and the last item at the end, leaving equal space between all other items.
space-around: Items are spaced evenly with equal space around them. This means there is twice the amount of space between items as there is at the edges.
space-evenly: Items are spaced evenly with equal space between and around all of them, making the layout balanced.
For example, when you set justify-content: space-between;, all items will have equal gaps between them, but the first and last item will align at the start and end of the container respectively.
2. align-items (Cross-Axis Alignment for Single Row)
The align-items property is used to align flex items along the cross axis, which is perpendicular to the main axis. In a default row layout, the cross axis is vertical, and in a column layout, it’s horizontal. The values for align-items are:
flex-start: Items align at the start of the cross axis. In a row layout, they align to the top, and in a column layout, they align to the left.
flex-end: Items align at the end of the cross axis. In a row layout, they align to the bottom, and in a column layout, they align to the right.
center: Items are centered along the cross axis, vertically in a row layout and horizontally in a column layout.
baseline: Items align based on their text baseline, which is particularly useful when you have text elements with different font sizes.
stretch (default): Items stretch to fill the container’s height (for row layout) or width (for column layout). This is useful when you want the items to fill the container but maintain consistent spacing.
For example, if you set align-items: center; in a row layout, all the items will be vertically centered within the container.
3. align-content (Multi-Line Cross-Axis Alignment)
The align-content property comes into play when there are multiple rows or columns of items, which occurs when flex-wrap: wrap; is used. It aligns the flex lines (groups of items) along the cross axis, similar to how justify-content works for the main axis. The possible values for align-content are:
flex-start: Flex lines are aligned at the start of the container, which means they will pack toward the top (in row layout) or left (in column layout).
flex-end: Flex lines are aligned at the end of the container, packing towards the bottom or right.
center: Flex lines are centered within the container.
space-between: The first flex line aligns at the start, the last at the end, and the other lines are evenly spaced in between.
space-around: Flex lines are evenly spaced with equal space around them. This results in double the space between lines as at the container’s edges.
space-evenly: Flex lines are evenly spaced with equal space between and around all lines.
stretch (default): Flex lines stretch to fill the available space in the container, making use of the extra space between rows or columns.
For example, if you set align-content: center; with multiple rows of items, the rows will be grouped together in the center of the container.
---
Summary
justify-content controls the alignment of items along the main axis (horizontal in a row layout, vertical in a column layout), managing how space is distributed between them.
align-items controls the alignment of items along the cross axis (vertical in a row layout, horizontal in a column layout), affecting how individual items are positioned.
align-content controls the alignment of multiple rows or columns of items along the cross axis when wrapping is enabled.
Each of these properties helps to create flexible, responsive layouts by offering different ways to distribute space and align items within a flex container.