How To Create A Grid In Javascript – Simple Examples

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:

  1. Define an HTML grid container.
    • <div id="grid"></div>
    • #grid { display: grid; grid-template-columns: auto auto; }
  2. 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.

 

 

REAL QUICK SLIDES

 

TABLE OF CONTENTS

Download & Notes JS Grid Useful Bits & Links
The End

 

DOWNLOAD & NOTES

Firstly, here is the download link to the example code as promised.

 

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.

 

QUICK NOTES

If you spot a bug, please feel free to comment below. I try to answer 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.

 

 

JAVASCRIPT GRID

All right, let us now get started with creating grids with HTML, CSS, and Javascript.

 

1) BASIC HTML CSS GRID

THE SCRIPT

1a-basic.html
<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>
1b-basic.css
/* (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;
}
Head 1
Head 2
Head 3
Cell A
Cell B
Cell C

 

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

2-js-grid.html
<!-- (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>
2-js-grid.html
<script>
window.addEventListener("load", function(){
  // (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>
Head 1
Head 2
Head 3

 

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.

 

INFOGRAPHIC CHEAT SHEET

Create Grid In Javascript (Click To Enlarge)

 

LINKS & REFERENCES

 

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!

Leave a Comment

Your email address will not be published. Required fields are marked *