Align items helps you control how things line up inside a box. It makes your page look neat and organized.
Align items in CSS
Start learning this pattern below
Jump into concepts and practice - no test required
.container { display: flex; align-items: value; }
The align-items property works only if the container uses display: flex or display: grid.
The value can be flex-start, flex-end, center, baseline, or stretch.
.container { display: flex; align-items: center; }
.container { display: flex; align-items: flex-start; }
.container { display: flex; align-items: flex-end; }
.container { display: flex; align-items: stretch; }
This example shows three colored boxes inside a container. The container uses align-items: center to line up the boxes vertically in the middle, even though they have different heights.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Align Items Example</title> <style> .container { display: flex; height: 10rem; border: 2px solid #333; align-items: center; background-color: #f0f0f0; gap: 1rem; padding: 1rem; } .box { width: 4rem; background-color: #4a90e2; color: white; display: flex; justify-content: center; align-items: center; font-weight: bold; border-radius: 0.5rem; } </style> </head> <body> <main> <section class="container" aria-label="Example of align-items center"> <div class="box">A</div> <div class="box" style="height: 6rem;">B</div> <div class="box" style="height: 3rem;">C</div> </section> </main> </body> </html>
If you want to align items horizontally, use justify-content instead.
Using align-items: stretch makes items fill the container's height if they don't have a fixed height.
Try changing the align-items value in the example to see how the boxes move.
Align items controls vertical alignment inside flex or grid containers.
Common values are center, flex-start, flex-end, and stretch.
It helps make layouts look neat and balanced.
Practice
align-items do in a flex container?Solution
Step 1: Understand the role of
Thealign-itemsalign-itemsproperty is used inside flex or grid containers to control how items align vertically.Step 2: Compare with other options
Options B, C, and D describe unrelated CSS properties or effects, so they are incorrect.Final Answer:
It controls the vertical alignment of items inside the container. -> Option AQuick Check:
Vertical alignment = align-items [OK]
- Confusing align-items with justify-content
- Thinking it changes colors or fonts
- Mixing horizontal and vertical alignment
align-items?Solution
Step 1: Recall valid
Valid values includealign-itemsvaluescenter,flex-start,flex-end, andstretch.centercenters items vertically.Step 2: Check each option
Options A, B, and C are not valid CSS values foralign-items.Final Answer:
align-items: center; -> Option DQuick Check:
Center vertically = align-items: center [OK]
- Using 'middle' instead of 'center'
- Confusing justify-content with align-items
- Adding invalid values like 'vertical-center'
div.container {
display: flex;
height: 200px;
align-items: flex-end;
}
<div class="container"> <div>Item 1</div> <div>Item 2</div> </div>
Solution
Step 1: Understand
This value aligns flex items to the bottom edge of the container vertically.align-items: flex-end;Step 2: Apply to container height
The container is 200px tall, so items will appear at the bottom inside that space.Final Answer:
Items will be aligned at the bottom of the container. -> Option AQuick Check:
flex-end = bottom alignment [OK]
- Thinking flex-end means top alignment
- Confusing align-items with justify-content
- Assuming items stretch by default
div.container {
display: flex;
align-items: center
height: 150px;
}Solution
Step 1: Check CSS syntax
Each CSS property must end with a semicolon. The linealign-items: centeris missing a semicolon.Step 2: Validate other properties
display: flex;is correct,heightis valid, andalign-itemsworks withoutjustify-content.Final Answer:
Missing semicolon after align-items: center. -> Option BQuick Check:
CSS lines need semicolons [OK]
- Forgetting semicolon after property
- Thinking height is invalid in flex
- Believing justify-content is required with align-items
div.grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
align-items: stretch;
}
div.grid > div.special {
/* What to add here? */
}Solution
Step 1: Understand container and item alignment
The container usesalign-items: stretch;to stretch all items vertically by default.Step 2: Override alignment for one item
To override vertical alignment for a single grid item, usealign-self. The correct value for top alignment isstart, notflex-start(which is for flexbox).Final Answer:
align-self: start; -> Option CQuick Check:
Grid item top align = align-self: start [OK]
- Using flex-start instead of start in grid
- Applying align-items on item instead of align-self
- Confusing justify-self with vertical alignment
