CSS Grid Introduction

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...

Today we'll be looking at a flex competitor called CSS Grid!
As the name suggests, it's an awesome tool to make grids and layouts.
In general, a grid is build by having a container and some children inside it.
As for our HTML we are using the following setup
<div class="grid">
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
</div>
And to style this basic grid into four equal columns:
.grid {
background: #1b9aaa;
display: grid;
grid-template-columns: 1fr 1fr 1fr 1fr;
grid-gap: 1.5em;
}
.item {
width: 100%;
height: 200px;
background: #06d6a0;
}
Note we add the grid-gap property, if we leave that out, the columns will be stuck to each other.
The template will work by defining it will have four small columns.
See and test it on Codepen.
But what if we want the first item to span two columns? Then two small ones and the other way around for the second row?
<div class="grid">
<!-- row 1 -->
<div class="item item-col-2"></div>
<div class="item"></div>
<div class="item item-row-2"></div>
<!-- row 2 -->
<div class="item"></div>
<div class="item item-col-2"></div>
</div>
For the CSS:
.grid {
background: #1b9aaa;
display: grid;
grid-template-columns: 1fr 1fr 1fr 1fr;
grid-gap: 1.5em;
}
.item {
width: 100%;
min-height: 200px;
background: #06d6a0;
&-col-2 {
grid-column: span 2;
}
&-row-2 {
grid-row: span 2;
}
}
As you can see, we use grid-column to span the grid over two blocks horizontal.
And grid-row to span over two blocks vertical.
Feel free to play around with this 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