Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Created June 11, 2023 03:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save code-boxx/1fb772b1e4e2dd854311a77640445603 to your computer and use it in GitHub Desktop.
Save code-boxx/1fb772b1e4e2dd854311a77640445603 to your computer and use it in GitHub Desktop.
Javascript Create HTML Grid

JAVASCRIPT CREATE HTML GRID

https://code-boxx.com/create-grid-javascript/

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

<!DOCTYPE html>
<html>
<head>
<title>HTML CSS Grid</title>
<!-- (A) CSS GRID : 3 PER ROW -->
<link rel="stylesheet" href="1b-basic-grid.css">
<style>
#demoA { grid-template-columns: repeat(3, minmax(0, 1fr)); }
</style>
</head>
<body>
<!-- (B) HTML CONTAINER -->
<div id="demoA" class="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>
</body>
</html>
/* (A) GRID LAYOUT */
.grid {
display: grid;
grid-gap: 10px;
}
/* (B) OPTIONAL - CELLS */
.head, .cell { padding: 10px; }
.head {
font-weight: 700;
border: 1px solid #f18e8e;
background: #ffbfbf;
}
.cell {
border: 1px solid #c2ba3c;
background: #f8ffde;
}
/* (X) NOT IMPORTANT */
* {
font-family: Arial, Helvetica, sans-serif;
box-sizing: border-box;
}
<!DOCTYPE html>
<html>
<head>
<title>Array To Grid</title>
<!-- (A) CSS GRID -->
<link rel="stylesheet" href="1b-basic-grid.css">
</head>
<body>
<!-- (B) HTML CONTAINER -->
<div id="demoB" class="grid"></div>
<!-- (C) JS ARRAY TO GRID -->
<script>
// (C1) DATA ARRAY
var data = [
["A", "B", "C"],
["D", "E", "F"],
["G", "H", "I"]
];
// (C2) SET NUMBER OF COLUMNS
var grid = document.getElementById("demoB");
grid.style.cssText = `grid-template-columns:repeat(${data[0].length}, minmax(0, 1fr))`;
// (C3) FIRST ROW - HEADER
for (let i of data[0]) {
let cell = document.createElement("div");
cell.innerHTML = i;
cell.className = "head";
grid.appendChild(cell);
}
// (C4) FOLLOW ROWS - CELLS
for (let i=1; i<data.length; i++) {
for (let j of data[i]) {
let cell = document.createElement("div");
cell.innerHTML = j;
cell.className = "cell";
grid.appendChild(cell);
}
}
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Object To Grid</title>
<!-- (A) CSS GRID -->
<link rel="stylesheet" href="1b-basic-grid.css">
</head>
<body>
<!-- (B) HTML CONTAINER -->
<div id="demoC" class="grid"></div>
<!-- (C) JS ARRAY TO GRID -->
<script>
// (C1) DATA OBJECT
var data = {
"Name" : ["Joe", "Jon", "Joy"],
"Gender" : ["M", "M", "F"]
};
// (C2) SET NUMBER OF COLUMNS
var grid = document.getElementById("demoC"),
keys = Object.keys(data);
grid.style.cssText = `grid-template-columns:repeat(${keys.length}, minmax(0, 1fr))`;
// (C3) FIRST ROW - HEADER
for (let i of keys) {
let cell = document.createElement("div");
cell.innerHTML = i;
cell.className = "head";
grid.appendChild(cell);
}
// (C4) FOLLOWING ROWS - CELLS
for (let i=0; i<data[keys[0]].length; i++) {
for (let k of keys) {
let cell = document.createElement("div");
cell.innerHTML = data[k][i];
cell.className = "cell";
grid.appendChild(cell);
}
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment