Align Content by Adding Styling to a CSS Grid Layout

Share this video with your friends

Social Share Links

Send Tweet
Published 4 years ago
Updated 4 years ago

One thing you can't escape when styling is centering content. It's going to happen even if you use CSS Grid!

We can define grids within grids, which will allow us to align our items within an area. We'll use align-items and justify-items to get this done!

Lesson Challenge

Which axis does "justify" refer to? What is the keyword that refers to vertical position?

Instructor: [0:00] When we talk about web design, one thing we can never get away from is alignment of content. We want to center things, left align, right align, but mostly, we just want to center things. With CSS Grid, there's a way to center things.

[0:13] Let's start with the y-axis aligned. Officially, the vertical line is called the block access. For this, use align-item. Let's test this out in the content grid item. First, we have to turn this grid area into a grid. Yes, you can have grids inside other grids.

[0:30] We're basically making the content inside the content grid area into grid items themselves. Now, we can set the y-axis alignment, by using align-items. We'll start with align-items: end, which will bring the text to the bottom of the grid, align-items: center will center it, and align-items: start will bring it to the top. Let's keep the content text to the center.

[1:04] Now, let's look at aligning to the inline access, or the row access. For this, you use justify-items. Here, we can use start, center, and end as well. Keep in mind that there are quite a few other options, too. But to start, these three will serve you very well.

[1:21] Let's change the alignment of the header. We'll go to the header class and make this into a grid as well. Let's do justify-items: end, and that moves the items to the right, or to the end of the grid. When we do justify-items: center, the header is centered on the x-axis. Let's do justify-items: start, and that brings it to the start of the grid, or the left.

egghead
egghead
~ just now

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today