My CSS GRID

Interactive flashcards for learning vocabulary

Englishhardby MaximNova2
Flashcards: 2003 / 5

Save words from this page and practise them with flashcards in DuoCards. How does it work?

I will teach you how to use the long awaited, brand new edition to the CSS language, which is the CSS Grids
I hope you're just as excited as they are.
We will build yet another really cool project
So, what are the CSS grids that everyone talks about so much these days?
The CSS Grid Layout is a brand new module that brings a two-dimensional grid system.
And there really has never been anything like this before.
So this is a real game-changer, because it changes everything.
It all just works a bit different than the way we've been doing it for so long now with float layouts and other hacks.
These are really exciting times to be a CSS Developer.
We create a new grid container by setting the display property of an element to grid.
We also have axes like in Flexbox.
So what are the grid rows and columns actually?
Well, the space between two grid lines is called a track, no matter if it's vertical or horizontal.
With time, this will all come naturally to you, especially the grid lines, gutters, tracks, rows, and columns.
Because we actually use these terms all the time when we start working with grids.
This is just a quick overview of all the CSS Grid related properties.
I will not go into explaining all of these properties at this point, because we're going to use them throughout the rest of the grid part of this course.
Believe me, this comes in very handy when we first start using grid.
Alright! So, that's a quick intro to what CSS grids are.
Now let's move on to setting up our tools, and finally start coding with CSS grids.
So yet another really cool section, and I can't wait to show you all of this.
Now, what's new is that we're actually gonna use a new browser, instead of Google Chrome,
And this makes it a lot easier to visualize what's going on.
Let's now, finally, get our hands dirty with CSS Grid for the very first time.
And so I'm using that right now to write our markup.
But we don't want that, and so we actually can do better.
Because here in Emmet, there's another trick that we can use.
And you see that I am actually logged into my account
Okay, and with that, we're ready to start using grid.
So let's say, we want these six elements here to be divided in two rows and three columns.
So what we do here is to define these two rows.
We define, basically, the height of our rows. And now let's do the same thing for the columns.
And alright, so we see here that they're actually now already laid out as a grid here.
And so we can construct our selectors like we've been using all the time in the project.
This one is royalblue, and then goldenrod or something.
We can see that our grid is really laid out in a three per two fashion here, with these squares.
Maybe it's not really visible right now but you are gonna see it a bit better in a second.
We can, of course, also put a gap in the columns.
You see how easy it was to put these six elements here in this very simple grid layout?
No floats, no weird HTML tricks here, because imagine if you wanted to do this very same layout.
We would have to have just a wrapper for these three items here, and then a wrapper for the second three items here.
So a row gap, and a column gap, which can be different but if we want them to be the same, we can simply use the grid-gap property.
So just change this one here to two and then we can add whatever we want here.
So what we do here is to create two tracks for the rows and three tracks for the columns, right.
It would basically expand and occupy all the possible space
Now here in Grid we don't have a property like that, but we have a similar functionality.
So what we're going to use is the brand new unit that was introduced, which is called a fractional unit.
So in this case it occupies all the remaining space.
Okay, does that make sense?
So let's take a look again at what happens here.
The fractional unit represents a fraction of the available space and so it takes all of that up.
In this case each of them, each of these three column tracks has the same width, right.
So with this we can divide the entire space in three parts.
Maybe to understand it even better let me show it to you in another way yet.
So all the empty space here is divided and then two of the fractions are assigned to the second column.
We can of course take this even further.
Of course it's going to get even bigger, because now the available space is basically divided into five parts.
Now you can play around with this if you want, so to become a bit more familiar with it.
So let's inspect the width of this element here.
Basically it takes 50% off the entire width and then the rest is for the other columns.
They are the ones that become a bit narrower.
I want to make the three columns all with the same width.
Especially for defining the column tracks, because this is really handy.
Let me just very quickly show it to you.
I think it's way more useful there.
Fractional unit here made a lot of sense to you.
Make sure that you understand what the fractional units really represents.
We're going to learn how to actually position these grid items exactly where you want them on the grid.
But now imagine that you actually have to place a grid item somewhere else.
Let's define a couple of properties.
So without even doing anything else.
And then the other items move up in the same order in which they were before.
So that's again, the automatic placement algorithm here.
Now it's exactly in the cell that we wanted it to.
And so there's actually a bit easier way to do it.
And so that's why I was saying that it's so important and so useful to having these line numbers here right with the dev tools.
And that's just something that Google Chrome doesn't do at the moment.
It probably will do sometime in the future. But right now, only Firefox does this.
You can actually do it in also another way.
But there's actually a shorthand even for these two.
So I think this is a bit confusing actually.
I personally do prefer this way here of writing it out.
There are actually more ways of positioning items.
But this one is the most straightforward and the most easy to use.
Please just know that this is the easiest way.
I want you to position this item right here.
So we can expand these elements here, for example, over multiple rows or multiple columns.
So imagine we wanted to have this blue element here spanning all over these two cells here.
And it's actually something really, really important.
Let's now learn how to span grid items across multiple grid cells in order to occupy entire areas.
Suppose you wanted this violet element here not only to occupy this single cell here, but to span across these two cells here, occupying this entire area.
And actually, it's really not hard.
And this part here is then called an implicit grid.
But that's something to talk about later, because I don't wanna confuse you.
So for now just note that if we span these items here, then we will simply be created a new line.
Unless we explicitly tell it to stay in this exact row where we wanted it to be.
And so if we want to put this one here in the same place, then this item here will try to find a new place.
So right now we have the orange one here in this cell, so here behind the violet one.
So we can always have multiple grid items in the same cell.
And then you will see it move on top.
And this is actually a technique that is used quite a lot with CSS grids,
And so let me demonstrate that one to you as well.
And so suppose that we wanted to be from here to here.
We can simply say that we want it to span across two.
And then what happens is that it's gonna span across three grid cells.
All we know is that we want it to be all until the end.
Because suppose that we wanted to start it in number one.
We can put two and it goes all the way until the end, and even three.
First, we simply defined the end number of the row or the column line.
Then as a second way, we used the span keyword, which is actually better.
Whenever we want to span all the way until the end of our grid, we can simply use minus one.
You really understood how we use all of these properties to come up with the layouts and the designs that we want.
So don't worry about how it looks.
I really encourage you to play around with all of these properties that I just showed you.
And what I want you to do is to simply recreate this entire layout using grid
So you can try to define the size of this column here.
Then in these other elements here, like the header, and the footer, and the content, you'll also have some spanning going on, as you can probably already see.
Is a layout that would've been extremely difficult to make it right using the old school CSS techniques.
And this actually looks already like a real world layout.
So this could perfectly be like a blog, or something like that.
Imagine this is a blog with a header, then three featured posts, and here a main post, a footer down here.
So I hope this isn't all too hard for you.
It's not so important if you actually can do it or not.
The most important part, believe me, is trying.
And I made this challenge a bit complex, a bit difficult on purpose, so that you get really challenged.
But I think that would've been too simple.
You wouldn't have been challenged, and then you wouldn't progress.
But then you really have to think, and that will move you forward.
So good luck and see you in a minute.
So we don't want to bother with incorporating some font here, okay, just so that it looks a bit better than the original.
So this is our layout let's say without any of the styling.
Remember that the sidebar was a bit smaller than the rest of the columns.
What we want is basically to have three equally sized columns and then one smaller one.
Let's just give them some gap so we can see it.
We see these empty grid cells, which were actually created with our grid track declaration.
We just don't have enough grid items to fill all of these cells up.
But don't worry, we're going to care about that right now.
Notice that we don't actually have to declare the grid row property here.
Because the automatic placement algorithm takes care of putting the header right in the first line.
They're also already exactly where they should be so we don't have to do anything.
I'm not really sure why this grid overlay is actually overlaying here this entire editor.
I'm sure that's not really correct, but it's probably just a bug in Firefox right now, so it's not a big deal.
All right, so let's worry about the sidebar and that's actually not correct.
I guess it's not really necessary.
I have to do all the scrolling up and down.
You can see that it's already taking shape and becoming more like we want in the end.
So the main content is now exactly in a place where it should be.
Now all that's left to do is to also position the footer, and nothings easier than that.
That would have been so complex to build with the old school float layouts like we've been doing for way too long actually.
I really hope that you achieved to do it by yourself.
I want to use this example to demonstrate to you the different ways of doing exactly that.
I want to show you, and this example is actually it's really perfect
But we can actually make our lives a bit easier by naming these grid lines.
And now I'm gonna actually comment out all of this code here, and then start from scratch.
All we have to do is to use the squared brackets like this.
The best practice is to basically describe the content that will be in the track.
It doesn't make much sense that this line here is called header-end.
Because it's not just the end of the header, but also the start of these boxes here.
But instead of using the number here, we just use the names.
But just for the fun of it, let's now make it like this.
I still wanted to show you how we do it, okay?
But in here, we have kind of a different situation.
We want all of them to have a name, so what can we do?
So what's the first column grid line gonna be called?
Remember that I just explained that?
And yeah, here we go, so it works just as before.
I hope this made a lot of sense to you, and in the next video, we're going to see the third method of how to position these grid items.
And this is how it works, so head, head, head, and head.
The four cells will be called head, and that's because we want the header to be in all of these four cells.
And so basically this template area here just creates a representation with text of how our final layout is gonna look like.
This is actually a great way of placing items on the grid, because it's really simple.
Once you have this declaration here finished, then all we have to do is, to actually place the items, like this.
We go back to our initial state.
This is how it looks like without any placement whatsoever.
But there actually another way of using this property here.
And it's very simple again, we don't have to think like we had to think with the other methods.
In this case, we first think about layout.
While with the two other methods that we did before, we used the names of the lines or the numbers, which is quite similar.
Let me, for example, just delete this here.
You will see that it's not working at all anymore.
All we have to do for that, is instead just delete it, is to put a dot.
We can have empty cells in our grids without any problem.
Then it's completely fine to do it like this.
So actually, you already know many of the important stuff about CSS grids.
But for now it's great that you've come this far, that you made all this progress.
Maybe you can even try to build something right now.
This is the same as usual.
Now it's that time where we're gonna talk about that.
This is the so called explicit grid because we explicitly defined how these rows and these columns should look like.
So that we can then fit all of these remaining items.
This is called the implicit grid.
The implicit and explicit grids, they look a bit different.
You see here that this solid line here goes around the explicit grid.
And so now this item here is vertically centered inside of that grid area.
Let's change that a bit, so decrease the padding of all the items.
Why was it stretching all the way from the top to the bottom.
Well, it's because of the same reason that it is in Flexbox.
Then the grid item stretches across the entire grid cell.
These are the ones that are really important.
Now you see that each of these items is actually aligned to the end.

Learning with DuoCards

What is DuoCards?

DuoCards is a language-learning app that helps you build vocabulary with flashcards. You can save words you want to learn and practise them over time.

How do I learn with this set?

Browse the flashcards to see the words and their translations. Select Card picker and choose the words you want to learn. Then create a free account to save your words and practise them.

How do I practise my words?

You'll see a flashcard with a word and a hidden translation. Guess what it means, then tap to check your answer. DuoCards repeats the words you find difficult.

Do I need an account, and is learning free?

You can browse this page without an account. A free account lets you save words and track your progress. The free plan has limits, and Premium offers additional features.