Learn how to align and order grid items in web development, as well as aligning within the grid container, through an in-depth, step-by-step tutorial.
This exercise is excerpted from Noble Desktop’s HTML & CSS training materials and is compatible with Flexbox, Grid, and Bootstrap updates through 2023. To learn current skills in HTML & CSS with hands-on training, check out our Front-End Web Development Certificate, Full-Stack Web Development Certificate, and coding classes in-person and live online.
Topics covered in this Web Development tutorial:
Aligning Grid Items, Aligning Within the Grid Container, Aligning Individual Grid Items, Ordering Grid Items
Exercise Preview
Exercise Overview
In this exercise you’ll learn about aligning grid items and ordering content (which are both similar to flexbox).
Getting Started
- In your code editor, close any files you may have open.
- For this exercise we’ll be working with the Grid Alignment and Order folder located in Desktop > Class Files > yourname-Flexbox Grid Class. You may want to open that folder in your code editor if it allows you to (like Visual Studio Code does).
-
Open index.html from the Grid Alignment and Order folder.
- Notice we have a .gallery div that contains images.
- Notice the first image is wrapped in .logo-wrapper div. In the css file we’ve styled this div with a background color so you’ll be able to see some things about sizing easier than if all these items were only images.
-
Preview index.html in Firefox.
- The .gallery div has an orange border (so you can more easily see its size).
- The J.Crew logo is in a div with a background color, so it’s currently filling up the entire width of the gallery.
- The remaining logos are displayed inline like images normally are.
Leave index.html open in Firefox as you work, so you can reload the page to see the code changes you’ll make.
Starting the Grid
- Switch back to your code editor.
- Open main.css from the css folder (in the Grid Alignment and Order folder).
-
In the .gallery rule add the following bold code:
gallery { border: 8px solid orange; display: grid; gap: 30px; grid-template-columns: repeat(4, 1fr); }
-
Save the file, and reload the page in Firefox.
- The logos should now be in a 4 by 4 grid.
- The logos would look better aligned to the center (horizontally and vertically).
- Ctrl–click (Mac) or Right–click (Windows) anywhere in the grid and choose Inspect Element.
- In the DevTools, to the right of
<div class="gallery">
click the grid button to show the grid overlay. - Notice the colored div that contains the J.Crew logo fills its grid area.
Leave the DevTools open with the grid overlay on.
Aligning Grid Items
- Switch back to your code editor.
-
In the .gallery rule add the following bold code:
gallery {
Code Omitted To Save Space
grid-template-columns: repeat(4, 1fr); justify-items: center; }
-
Save the file, and reload the page in Firefox.
- Notice the logos are now horizontally centered within their grid areas.
- Notice the colored div that contains the J.Crew logo has collapsed to be the size of its content (it previously had stretched to fill the grid area).
- Switch back to your code editor.
-
In the .gallery rule add the following bold code:
gallery {
Code Omitted To Save Space
justify-items: center; align-items: center; }
-
Save the file, and reload the page in Firefox.
- Notice the logos are also vertically centered within their grid areas.
Aligning Within the Grid Container
- Switch back to your code editor.
-
In the .gallery rule change 1fr to 175px as shown below in bold:
gallery { border: 8px solid orange; display: grid; gap: 30px; grid-template-columns: repeat(4, 175px); justify-items: center; align-items: center; }
-
Save the file, and reload the page in Firefox.
- Resize the window so it’s wide enough that when the logos do not fill the gallery div, you can see there’s now empty white space to the right of the logos (inside the orange border).
- Switch back to your code editor.
-
In the .gallery rule add the following bold code:
gallery {
Code Omitted To Save Space
justify-items: center; align-items: center; justify-content: center; }
-
Save the file, and reload the page in Firefox.
- Notice the entire section of grid columns is now centered (so the empty space is evenly split between the left and right sides).
-
In DevTools find the .gallery rule’s justify-content property.
- Try out the following values: start, end, center, space-around, space-between, and space-evenly (stretch is the default).
- Switch back to your code editor.
-
In the .gallery rule add the following bold code:
gallery {
Code Omitted To Save Space
justify-content: center; height: 700px; }
-
Save the file, and reload the page in Firefox.
- The grid is now taller.
- We needed to set a height to see how align-content works.
- Switch back to your code editor.
-
In the .gallery rule add the following bold code:
gallery {
Code Omitted To Save Space
justify-items: center; align-items: center; justify-content: center; align-content: center; height: 700px; }
-
Save the file, and reload the page in Firefox.
- Notice the entire section of grid rows is now vertically centered (so the empty space is evenly split between the top and bottom).
-
In DevTools find the .gallery rule’s align-content property.
- Try out the following values: start, end, center, space-around, space-between, and space-evenly (stretch is the default).
- These work the same as justify-content, but vertically instead of horizontally.
Reload the page to get rid of any changes you were experimenting with.
Aligning Individual Grid Items
In this layout we don’t need to align individual elements, but let’s take a look at how that works for when you will need it.
- Switch back to your code editor.
-
Below the .gallery rule add the following new rule:
.gallery .zara { justify-self: end; align-self: start; }
-
Save the file, and reload the page in Firefox.
- Notice the Zara logo is now at the top right of its grid area.
- Ctrl–click (Mac) or Right–click (Windows) on the Zara logo and choose Inspect Element.
- In DevTools find the .gallery .zara rule and experiment with changing justify-self and align-self to any of the following values: start, end, and center (stretch is the default, which is appropriate for divs, but not images).
- Switch back to your code editor.
Delete the .gallery .zara rule (we were only seeing how this works, and didn’t actually want to change it).
Ordering Grid Items
Ordering works the same in grid and flexbox.
-
Below the .gallery rule add the following new rule:
.gallery .nike { order: -1; }
-
Save the file, and reload the page in Firefox.
- The Nike logo should now be first (because the default order is 0).
- Switch back to your code editor.
-
In the .gallery .nike rule change the order as shown below:
.gallery .nike { order: 1; }
-
Save the file, and reload the page in Firefox.
- The Nike logo should now be last.
- Switch back to your code editor.
- Switch to index.html.
- Delete the .logo-wrapper div that wraps the first image (be sure to delete the div’s start and end tags).
- Save the file.
- Switch back to main.css.
-
Above the .gallery .nike rule add the following new rule:
.gallery img { order: 3; }
-
Below the .gallery .nike rule add the following new rule:
.gallery .hermes { order: 2; }
-
Save the file, and reload the page in Firefox.
- The Nike logo should be first, followed by Hermes, J.Crew, and then the rest of the logos.
Optional Bonus: Cleaning Up the Appearance
- Switch back to your code editor.
- Remove the height from the .gallery rule.
- Remove the border from the .gallery rule.
-
Save the file, and reload the page in Firefox.
- In the DevTools, to the right of
<div class="gallery">
click the grid button to hide the grid overlay. - That’s a clean looking grid of logos!
- In the DevTools, to the right of
Aligning in CSS Grid
Here’s a quick comparison of grid’s alignment properties:
Justify vs. Align
- justify- aligns along the row axis (x or inline axis)
- align- aligns along the column axis (y or block axis)
- Unlike Flexbox, the axes don’t switch.
Items vs. Content
- -items aligns what is inside each grid item/cell.
- -content defines the alignment/spacing of grid items within the grid container (assuming the grid is smaller than the size of the grid container).
How to Remember Items vs. Content
- -items: Items is plural. There are many items in the grid, so justify-items and align-items apply individually to every one of the items.
- -content: Content is singular. This refers to the grid container’s content as a whole (the grid’s content).
For a detailed look at grid’s various properties, CSS Trick’s Complete Guide to Grid tinyurl.com/csst-grid is a great reference with visuals. If you ever forget something, or are confused by the terminology, this should help.
Place Items Shorthand
The shorthand for justify-items and align-items is place-items but at the time of this writing does not have good browser support. You can check browser compatibility and learn more about this property on the MDN Web Docs at tinyurl.com/grid-pi