Alignment Mastery: Justify-Content, Align-Items, and Align-Self Explained
Alignment Mastery: Justify-Content, Align-Items, and Align-Self Explained
Think about a school bus picking up students in the morning.
- When the first three students board, do they sit packed together at the very front near the driver (
flex-start)? - Do they walk to the back seats (
flex-end)? - Or do they spread out evenly so one sits near the front, one in the middle, and one at the back with maximum breathing space between them (
space-between)?
Now look at how those students sit on their bench:
- Are their heads aligned to the ceiling, or are their feet resting on the floor?
- And what if three students are sitting upright, but one student bends down to pick up a fallen geometry box from the floor? That single student just applied
align-self: flex-end;!
+-------------------------------------------------------------------------+ | THE CORE FLEXBOX ALIGNMENT TRIO | | | | 1. justify-content (Parent Rule) | | Aligns and distributes space along the MAIN AXIS. | | | | 2. align-items (Parent Rule) | | Aligns ALL children collectively across the CROSS AXIS. | | | | 3. align-self (Child Rule) | | Overrides align-items for ONE specific individual item! | +-------------------------------------------------------------------------+
In this tutorial, you will master every alignment property, eliminate guesswork when centering elements, and understand exactly why and when each value is used.
1. justify-content: Governing the Main Axis
The justify-content property distributes spare unused space along the Main Axis (horizontal in row, vertical in column).
+-------------------------------------------------------------------------+ | `flex-start` (Default): Items packed at the start | | [ Item 1 ][ Item 2 ][ Item 3 ] | +-------------------------------------------------------------------------+ | `center`: Items clustered together in the exact middle | | [ Item 1 ][ Item 2 ][ Item 3 ] | +-------------------------------------------------------------------------+ | `flex-end`: Items packed at the end | | [ Item 1 ][ Item 2 ][ Item 3 ] | +-------------------------------------------------------------------------+ | `space-between`: First item flush start, last item flush end | | [ Item 1 ] [ Item 2 ] [ Item 3 ] | +-------------------------------------------------------------------------+ | `space-around`: Equal space on sides (outer spaces are half size) | | [ Item 1 ] [ Item 2 ] [ Item 3 ] | +-------------------------------------------------------------------------+ | `space-evenly`: Truly identical spacing between every edge & item | | [ Item 1 ] [ Item 2 ] [ Item 3 ] | +-------------------------------------------------------------------------+
The Difference: space-between vs space-around vs space-evenly
space-between: The first item touches the left wall, the last item touches the right wall. All extra space goes between items. This is the #1 choice for website navigation bars (Logo on far left, links on far right)!space-around: Each item gets an equal amount of padding around itself. Because two adjacent items combine their spaces, the space between two items is double the space at the edges.space-evenly: The browser divides the free space equally so that the distance from the wall to an item is 100% identical to the distance between items.
2. align-items: Governing the Cross Axis
While justify-content works on the Main Axis, align-items controls how items sit along the perpendicular Cross Axis.
+-------------------------------------------------------------------------+ | `align-items: stretch` (Default) | `align-items: flex-start` | | +------------------------------------+ | +----------------------------+ | | | [ 1 ] [ 2: Tall ] [ 3 ] | | | [ 1 ] [ 2: Tall ] [ 3 ] | | | | [ 1 ] [ 2: Tall ] [ 3 ] | | | [ 2: Tall ] | | | | [ 1 ] [ 2: Tall ] [ 3 ] | | | | | | +------------------------------------+ | +----------------------------+ | +----------------------------------------+--------------------------------+ | `align-items: center` | `align-items: baseline` | | +------------------------------------+ | +----------------------------+ | | | [ 2: Tall ] | | | | | | [ 1 ] [ 2: Tall ] [ 3 ] | | | [ 1: Big ] [ 2: sm ] [ 3 ] | | | | [ 2: Tall ] | | | (Text baselines align!) | | | +------------------------------------+ | +----------------------------+ | +-------------------------------------------------------------------------+
Why stretch is the Default
By default, if child items do not have an explicit height, align-items: stretch makes every card in a row expand to match the height of the tallest card in that row! This solves the centuries-old web design headache of "equal height columns" automatically.
What is baseline?
align-items: baseline aligns items so the bottom baseline of their inner text lines up on the same invisible notebook ruling, even if one item has a huge 32px font and another has a tiny 12px font.
3. The Holy Grail of Centering: Pure Dead Center
For nearly 20 years, perfectly centering an element horizontally and vertically in CSS was considered a difficult interview question. With Flexbox, it takes just three lines of code:
Whether the screen is an iPhone 12, an iPad, or a 4K monitor, your dialog card stays in the mathematical dead center of the screen!
4. align-self: The Individual Rulebreaker
What if you have five items neatly centered with align-items: center, but one specific item needs to stick to the top or stretch all the way to the bottom?
align-self is declared directly on the child item, overriding whatever align-items was set by the parent:
justify-self in Flexbox! Because items are packed and distributed along the main axis as a linked train, individual items cannot jump their horizontal track. To push an individual item along the main axis, use margin-left: auto; or margin-right: auto;.5. Common Mistakes to Avoid (Do's and Don'ts)
| Bad Practice (Don't) | Good Practice (Do) | Why? |
|---|---|---|
Trying to use justify-self on a flex item | Use align-self for cross axis, or margin-left: auto; for main axis | justify-self does not exist in the CSS Flexbox specification. |
Forgetting container height when using align-items: center | Ensure parent container has a height or min-height: 100vh; | If the container has no height, it hugs its children and there is no room to vertically center! |
Using float: right to push the last nav link to the far right | Use margin-left: auto; on that last flex item | margin: auto absorbs all remaining free space along the flex axis. |
Hardcoding margin: 0 auto; inside a flex container | Let justify-content: center handle the centering | Flex containers handle internal distribution natively. |
6. Quick Revision Summary (Cheat Sheet)
justify-content(Parent): Aligns along the Main Axis. Values:flex-start,flex-end,center,space-between,space-around,space-evenly.align-items(Parent): Aligns all children along the Cross Axis. Values:stretch(default),flex-start,flex-end,center,baseline.align-self(Child): Lets a specific child element override the parent'salign-items.- Navbar Shortcut:
justify-content: space-betweenplaces your logo on the left and navigation links on the right with zero manual margins. - Auto-Margins in Flexbox: Giving a flex item
margin-left: autopushes it (and everything after it) all the way to the right edge.
Multiple Choice Questions
1. Which CSS property distributes empty space between flex items along the Main Axis?
A. align-items B. justify-content C. align-content D. flex-direction Answer: B Explanation: justify-content distributes spare space along the main axis. align-items operates along the perpendicular cross axis.
2. Which value of justify-content pushes the first item flush against the left boundary and the last item flush against the right boundary?
A. space-around B. space-evenly C. space-between D. center Answer: C Explanation: space-between puts zero space at the outer ends, pushing the first and last items to the outer edges and distributing all remaining space in between.
3. What is the default value of align-items on a flex container?
A. flex-start B. center C. stretch D. baseline Answer: C Explanation: stretch is the default value, causing all flex items without explicit cross-axis sizes to expand to the full height of the container.
4. Why does justify-self: center; fail to work on an individual child flex item?
A. The browser requires an -ms- vendor prefix B. justify-self is not defined in the CSS Flexbox specification; items are distributed along the main axis as a collective sequence C. It only works if the container is an inline element D. It only works on images Answer: B Explanation: The CSS Flexbox specification does not include justify-self. To push a single flex item along the main axis, developers use margin auto properties.
5. If a parent flex container has align-items: center;, how can one specific child card stick to the bottom of the container?
A. Give that child align-self: flex-end; B. Give that child vertical-align: bottom; C. Give that child float: bottom; D. Give that child position: bottom; Answer: A Explanation: align-self is an item-level property that allows an individual child to override the parent container's align-items rule.
7. Hands-on Practice Challenge: School Exam Hall Supervisor Desk
Build a realistic School Examination Hall seating layout:
- 1An examination hall container with a defined height (
320px) andalign-items: center. - 2Three student desks arranged with
justify-content: space-between. - 3An individual Invigilator / Supervisor Desk that uses
align-self: flex-startto stay elevated at the teacher podium!
The Flex Sizing Equation: Flex-Grow, Flex-Shrink, and Flex-Basis Demystified
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Related Lessons
Practice Quiz
Test your understanding of this lesson with 5 questions. Each question has one correct answer.