CSS Grid Container

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
No comments yet. Be the first to comment.
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system. We'll be looking at two different layers of customization for customi...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

Yesterday we had the first touch with CSS Grid. Today we will be diving more into the container part of the grid.
The container is the outer wrapper, and much like flex it has some awesome properties we can leverage.
You must think of the container is the element where you define your layout structure.
In today's example we will be working with a three column, two row layout.
<div class="grid">
<!-- Row 1 -->
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<!-- Row 2 -->
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
</div>
So let's start by making our container a grid:
.grid {
display: grid;
grid-template-columns: auto auto auto;
}
We define our columns layout and tell it to make three evenly spaced columns.
Let's add some gaps because these make it easier to see what's happening.
We can add a allround gap (column and row) or the separate ones.
.grid {
grid-gap: 1em; // Around
grid-column-gap: 1em; // Columns
grid-row-gap: 1em; // Rows
}
By default, the gaps are off (not existing)
We can also change the column setup to be variant like this:
.grid {
display: grid;
grid-template-columns: 20px auto 100px;
}
The auto will fill to 100% setup.
As we've seen now we are only defining the columns so rows will be made automatically, but we can influence them like so:
.grid {
display: grid;
grid-template-rows: 80px 200px;
}
You can see the above in the following Codepen.
To align the grid, we can use several awesome functions:
Let's start with justify-content. This property will align on the horizontal axis.
Space the items evenly:
.grid {
display: grid;
justify-content: space-evenly;
}
Give them space around:
.grid {
display: grid;
justify-content: space-around;
}
Give them space between them
.grid {
display: grid;
justify-content: space-between;
}
Center the elements
.grid {
display: grid;
justify-content: center;
}
Move all blocks to the start
.grid {
display: grid;
justify-content: start;
}
Or to the end
.grid {
display: grid;
justify-content: end;
}
Have a play with this Codepen.
That was the horizontal axis, but we can also influence the vertical axis by using the align-content property.
Space the items evenly:
.grid {
display: grid;
align-content: space-evenly;
}
Give them space around:
.grid {
display: grid;
align-content: space-around;
}
Give them space between them
.grid {
display: grid;
align-content: space-between;
}
Center the elements
.grid {
display: grid;
align-content: center;
}
Move all blocks to the start
.grid {
display: grid;
align-content: start;
}
Or to the end
.grid {
display: grid;
align-content: end;
}
Play around with the following Codepen.
Ofcourse we can combine the two, let's make a centred grid:
.flex {
display: flex;
align-content: center;
justify-content: center;
}
You can see this on the following Codepen.
CSS Grid has support from all major browsers, we have some issues in IE, but they can be Polyfilled.

Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter