Topics Covered in This Web Development Tutorial:
Master CSS Grid alignment and ordering techniques: aligning grid items within their containers, positioning individual elements, and controlling content flow through strategic ordering.
Exercise Preview

Exercise Overview
This hands-on exercise explores CSS Grid's sophisticated alignment and ordering capabilities. While these concepts parallel Flexbox functionality, Grid's two-dimensional nature provides distinct advantages for complex layouts. You'll learn to control both individual item positioning and overall content distribution—essential skills for creating polished, professional interfaces that adapt gracefully across devices and screen sizes.
Getting Started
- Close any open files in your code editor to maintain a clean workspace.
- Navigate to the Grid Alignment and Order folder located in Desktop > Class Files > yourname-Flexbox Grid Class. For optimal workflow, open this entire folder in your code editor (Visual Studio Code, Sublime Text, or similar editors support folder-based project management).
Open index.html from the Grid Alignment and Order folder.
- Examine the .gallery div containing brand logo images—this will serve as our grid container.
- Note that the first image is wrapped in a .logo-wrapper div with custom background styling. This visual aid will help you understand how grid items behave differently from their content, particularly regarding sizing and positioning.
Preview index.html in Firefox (or Chrome with DevTools).
- The .gallery div displays with an orange border for easy identification of container boundaries.
- The J.Crew logo, wrapped in its styled div, currently spans the full container width due to default block-level behavior.
- Remaining logos display inline, following standard image flow patterns.
Keep index.html open in your browser throughout this exercise—you'll refresh frequently to observe your code changes in real-time.
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.
Starting the Grid
Now we'll transform this basic layout into a structured grid system, establishing the foundation for advanced alignment techniques.
- Return to your code editor.
- Open main.css from the css folder within the Grid Alignment and Order directory.
Add the following properties to the .gallery rule:
gallery { border: 8px solid orange; display: grid; gap: 30px; grid-template-columns: repeat(4,1fr); }Save and refresh your browser.
- Your logos now arrange in a clean 4×4 grid structure with consistent 30px spacing.
- Notice how the grid automatically handles overflow to new rows—this responsive behavior is one of Grid's key advantages over traditional layout methods.
- Right-click anywhere within the grid and select Inspect Element to access Developer Tools.
- In DevTools, click the grid button next to
<div class="gallery">to enable the grid overlay visualization. - Observe how the colored J.Crew logo container fills its entire grid area—this default "stretch" behavior will change as we apply alignment properties.
Keep DevTools open with the grid overlay active for the next section.
Using repeat(4,1fr) creates four equal-width columns that automatically distribute available space, forming the foundation for our 4x4 logo grid layout.
Aligning Grid Items
Grid alignment operates on two axes: the inline axis (horizontal) and block axis (vertical). Understanding this distinction is crucial for precise layout control.
- Return to your code editor.
Add horizontal centering to the .gallery rule:
gallery {Code Omitted To Save Space
grid-template-columns: repeat(4,1fr); justify-items: center; }Save and refresh your browser.
- All logos now center horizontally within their grid areas.
- The J.Crew logo's colored container shrinks to content size rather than stretching—this demonstrates how alignment properties affect sizing behavior.
- Return to your code editor.
Add vertical centering to complete the alignment:
gallery {Code Omitted To Save Space
justify-items: center; align-items: center; }Save and refresh your browser.
- Logos are now perfectly centered both horizontally and vertically within their grid areas, creating a balanced, professional appearance.
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 |
Aligning Within the Grid Container
While item alignment controls positioning within individual grid areas, content alignment manages the entire grid's position within its container—essential for responsive design scenarios.
- Return to your code editor.
Change the flexible grid columns to fixed widths:
gallery { border: 8px solid orange; display: grid; gap: 30px; grid-template-columns: repeat(4, 175px); justify-items: center; align-items: center; }Save and refresh your browser.
- Widen your browser window until you see empty space to the right of the logos within the orange border. This space occurs because our grid is now smaller than its container.
- Return to your code editor.
Center the entire grid horizontally:
gallery {Code Omitted To Save Space
justify-items: center; align-items: center; justify-content: center; }Save and refresh your browser.
- The grid columns now center within the container, distributing empty space evenly on both sides.
Experiment with different justify-content values in DevTools:
- start, end, center, space-around, space-between, and space-evenly. Each provides different spacing strategies for responsive layouts.
- Return to your code editor.
Add height to demonstrate vertical content alignment:
gallery {Code Omitted To Save Space
justify-content: center; height: 700px; }Save and refresh your browser.
- The increased height creates vertical space, setting up our next alignment demonstration.
- Return to your code editor.
Center the grid vertically within its container:
gallery {Code Omitted To Save Space
justify-items: center; align-items: center; justify-content: center; align-content: center; height: 700px; }Save and refresh your browser.
- The entire grid now centers vertically, with equal space above and below.
In DevTools, experiment with align-content values:
- start, end, center, space-around, space-between, and space-evenly. These mirror justify-content behavior but operate on the vertical axis.
- Understanding both axes gives you complete control over grid positioning in any layout scenario.
Refresh the page to reset any experimental changes.
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.
Aligning Individual Grid Items
Sometimes specific grid items need unique positioning while others follow the default alignment. This granular control distinguishes professional layouts from basic grid implementations.
- Return to your code editor.
Add a rule targeting the Zara logo specifically:
.gallery.zara { justify-self: end; align-self: start; }Save and refresh your browser.
- The Zara logo now positions at the top-right of its grid area, overriding the global centering rules.
- Right-click the Zara logo and select Inspect Element.
- In DevTools, experiment with different justify-self and align-self values: start, end, center, and stretch. This technique is invaluable for creating visual hierarchy and emphasis in complex layouts.
- Return to your code editor.
Remove the .gallery.zara rule—this was purely educational to understand the capability.
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.
Ordering Grid Items
Visual order independence from source order is a powerful feature shared between Grid and Flexbox, enabling accessibility-friendly markup while achieving any desired visual presentation.
Add ordering to move Nike to the beginning:
.gallery.nike { order: -1; }Save and refresh your browser.
- Nike now appears first because negative order values precede the default order of 0.
- Return to your code editor.
Change Nike's order to move it to the end:
.gallery.nike { order: 1; }Save and refresh your browser.
- Nike now appears last, demonstrating how positive order values work.
- Return to your code editor.
- Switch to index.html.
- Remove the .logo-wrapper div around the first image, deleting both opening and closing tags while preserving the image itself.
- Save the HTML file.
- Switch back to main.css.
Create a comprehensive ordering system:
.gallery img { order: 3; }Add specific ordering for Hermes:
.gallery.hermes { order: 2; }Save and refresh your browser.
- Your logos now display in the custom order: Nike (order: 1), Hermes (order: 2), J.Crew (default: 0), then remaining images (order: 3). This demonstrates how to create sophisticated content hierarchies that differ from markup order.
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
Optional Bonus: Cleaning up the Appearance
Professional implementations require clean, production-ready code. Let's remove our development aids and polish the final result.
- Return to your code editor.
- Remove the height property from the .gallery rule.
- Remove the border property from the .gallery rule.
Save and refresh your browser.
- In DevTools, click the grid button to hide the overlay.
- You now have a clean, professional logo grid that demonstrates advanced CSS Grid capabilities while maintaining visual elegance.
Aligning in CSS Grid
Mastering Grid alignment requires understanding these fundamental distinctions:
Justify Vs. Align
- justify- controls alignment along the row axis (X or inline axis)
- align- controls alignment along the column axis (Y or block axis)
- Unlike Flexbox, Grid's axes remain consistent regardless of direction—this predictability simplifies complex layout development.
Items Vs. Content
- -items aligns content within each individual grid cell/area.
- -content controls the alignment and spacing of the entire grid within its container (when the grid is smaller than the container).
How to Remember Items Vs. Content
- -items: Think "items is plural"—it affects many individual items throughout the grid.
- -content: Think "content is singular"—it treats the entire grid as one unit of content within its container.
For comprehensive Grid reference with visual examples, CSS-Tricks' Complete Guide to Grid (tinyurl.com/csst-grid) remains the industry standard. Bookmark it for quick property lookups and conceptual clarification during development.
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) |
Place Items Shorthand
The place-items shorthand combines justify-items and align-items into a single property. As of 2026, browser support has significantly improved, with all modern browsers supporting this efficient syntax. Check current compatibility at MDN Web Docs (tinyurl.com/grid-pi) and consider implementing it in projects targeting contemporary browser environments. This shorthand reduces CSS verbosity while maintaining the same functionality.
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