Grid: Alignment, Centering, & Reordering Content
Master CSS Grid alignment and content ordering
Core CSS Grid Alignment Concepts
Grid Item Alignment
Control how content is positioned within individual grid cells using justify-items and align-items properties.
Container Alignment
Position the entire grid within its container using justify-content and align-content for precise layout control.
Individual Control
Override default alignment for specific elements using justify-self and align-self properties.
Setting Up Your Grid Development Environment
Open Project Files
Navigate to the Grid Alignment and Order folder and open index.html to see the initial gallery structure with logo wrapper.
Preview in Browser
Open the file in Firefox to see the orange border gallery container and inline image display before grid implementation.
Enable Development Tools
Keep DevTools open with grid overlay enabled to visualize grid areas and alignment changes in real-time.
Using repeat(4,1fr) creates four equal-width columns that automatically distribute available space, forming the foundation for our 4x4 logo grid layout.
Grid Item Alignment Properties
| Feature | justify-items | align-items |
|---|---|---|
| Direction | Horizontal (row axis) | Vertical (column axis) |
| Default Value | stretch | stretch |
| Common Values | start, end, center | start, end, center |
| Effect on Content | Centers logos horizontally | Centers logos vertically |
Container Alignment Options
justify-content Values
Control horizontal positioning with start, end, center, space-around, space-between, and space-evenly for flexible layouts.
align-content Values
Manage vertical positioning using the same values as justify-content but along the column axis for complete control.
Container alignment properties only become visible when grid columns have fixed widths (like 175px) rather than flexible units (1fr), creating extra space to distribute.
Individual Item Control
Target Specific Items
Create CSS rules targeting individual grid items using class selectors to override default alignment behavior.
Apply Self Alignment
Use justify-self and align-self properties with values like start, end, center, or stretch for precise positioning.
Test and Refine
Experiment with different values in DevTools to achieve the desired positioning before finalizing your CSS rules.
Grid ordering works identically to flexbox, using integer values where negative numbers appear first, 0 is default, and positive numbers appear last in ascending order.
Order Value Hierarchy
Grid Alignment Property Comparison
| Feature | Items Properties | Content Properties |
|---|---|---|
| Scope | Individual grid cells | Entire grid container |
| Horizontal | justify-items | justify-content |
| Vertical | align-items | align-content |
| Memory Aid | Items = plural (many) | Content = singular (whole) |
The place-items shorthand property combines justify-items and align-items but currently lacks good browser support. Check MDN Web Docs for current compatibility before using in production.
CSS Trick's Complete Guide to Grid is a great reference with visuals for detailed grid properties
Key Takeaways
