Welcome to a quick tutorial on how to create a grid in Javascript. Need to display some data in a nice grid? Gone are the good old days where we have to use crazy tables and CSS hacks.
To create a grid in Javascript:
- Define an HTML grid container.
<div id="grid"></div>
#grid { display: grid; grid-template-columns: auto auto; }
- Use Javascript to add cells to the grid container.
var container = document.getElementById("grid");
var cell = document.createElement("div");
cell.innerHTML = "TEXT";
container.appendChild(cell);
Yep, it’s that simple. Let us walk through a few more examples in this guide – Read on!
ⓘ I have included a zip file with all the example source code at the start of this tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in.
QUICK SLIDES
TABLE OF CONTENTS
DOWNLOAD & NOTES
Firstly, here is the download link to the example code as promised.
QUICK NOTES
If you spot a bug, feel free to comment below. I try to answer short questions too, but it is one person versus the entire world… If you need answers urgently, please check out my list of websites to get help with programming.
EXAMPLE CODE DOWNLOAD
Click here to download the source code, I have released it under the MIT license, so feel free to build on top of it or use it in your own project.
JAVASCRIPT GRID
All right, let us now get started with creating grids with HTML, CSS, and Javascript.
1) BASIC HTML CSS GRID
THE SCRIPT
<div id="grid">
<div class="head">Head 1</div>
<div class="head">Head 2</div>
<div class="head">Head 3</div>
<div class="cell">Cell A</div>
<div class="cell">Cell B</div>
<div class="cell">Cell C</div>
</div>
/* (A) 3 COLUMNS PER ROW */
#grid {
display: grid;
grid-template-columns: auto auto auto;
grid-gap: 10px;
}
/* (B) 1 COL ON SMALL SCREENS */
@media screen and (max-width:768px) {
#grid { grid-template-columns: auto; }
}
/* (C) OPTIONAL FOR THE CELLS */
.head, .cell { padding: 10px; }
.head {
font-weight: bold;
border: 1px solid #f18e8e;
background: #ffbfbf;
}
.cell {
border: 1px solid #c2ba3c;
background: #f8ffde;
}
THE EXPLANATION
Let us start without the Javascript first:
- The HTML should be super straightforward. Just create a
<div id="grid">
container and sandwich<div class="cell">
inside. - The magic happens when we set
#grid { display: grid }
. The browser will automatically set all the cells out in a grid layout. - Lastly, use
grid-template-columns
to specify the number of columns.
That’s all. The rest is pretty much cosmetics.
2) JAVASCRIPT ARRAY TO GRID
THE SCRIPT
<!-- (A) HTML CONTAINER -->
<div id="grid">
<div class="head">Head 1</div>
<div class="head">Head 2</div>
<div class="head">Head 3</div>
</div>
<script>
window.addEventListener("load", () => {
// (B1) DATA ARRAY
var data = [
["A", "B", "C"],
["D", "E", "F"]
];
// (B2) LOOP + ADD CELLS
let container = document.getElementById("grid");
for (let i of data) { for (let j of i) {
let cell = document.createElement("div");
cell.innerHTML = j;
cell.className = "cell";
container.appendChild(cell);
}}
});
</script>
THE EXPLANATION
I don’t think this needs a lot of explanation – We can use Javascript to loop through an array, create cells, and append them to the grid.
USEFUL BITS & LINKS
That’s all for the tutorial, and here is a small section on some extras and links that may be useful to you.
LINKS & REFERENCES
- Grids – Learn web development | MDN
- How To Create a Grid In HTML CSS – Code Boxx
- Need a fancy grid for images? Check out Macy.
INFOGRAPHIC CHEAT SHEET

THE END
Thank you for reading, and we have come to the end. I hope that it has helped you to better understand, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!