3

I am trying to create a 16x16 grid. Everything works perfectly except that I have some space between my rows, I can't figure out why. The spacing between the columns is perfect but the rows have some gap between them.

Here's my code:

const container = document.getElementById("drawingGrid");
let rows = document.getElementsByClassName("gridRow");
let cells = document.getElementsByClassName("cell");

function createGrid() {
  makeRows(16);
  makeColumns(16);
}

// create rows
function makeRows(rowNum) {
  for (r = 0; r < rowNum; r++) {
    let row = document.createElement("div");
    container.appendChild(row).className = "gridRow";
  }
}
// create columns
function makeColumns(cellNum) {
  for (i = 0; i < rows.length; i++) {
    for (j = 0; j < cellNum; j++) {
      let newCell = document.createElement("div");
      rows[j].appendChild(newCell).className = "cell";
    }
  }
}

createGrid();
.cell {
  border: 1px solid gray;
  min-width: 10px;
  min-height: 10px;
  display: inline-block;
  margin: 0;
  padding: 0;
}
<!doctype html>

<html lang="en">

<head>
  <meta charset="utf-8">

  <title>EtchASketch</title>
  <link rel="stylesheet" href="style.css">

</head>

<body>
  <div id="drawingGrid">
  </div>

  <script src="app.js"></script>
</body>

</html>

And here's what it looks like on chrome:

enter image description here

Alexander Nied
  • 12,804
  • 4
  • 25
  • 45
Gobhniu
  • 265
  • 3
  • 11

2 Answers2

3

You can turn the gridRow into a flexbox container. You can read the reason behind the space: Why does my image have space underneath? here.

const container = document.getElementById("drawingGrid");
let rows = document.getElementsByClassName("gridRow");
let cells = document.getElementsByClassName("cell");

function createGrid() {
  makeRows(16);
  makeColumns(16);
}

// create rows
function makeRows(rowNum) {
  for (r = 0; r < rowNum; r++) {
    let row = document.createElement("div");
    container.appendChild(row).className = "gridRow";
  }
}
// create columns
function makeColumns(cellNum) {
  for (i = 0; i < rows.length; i++) {
    for (j = 0; j < cellNum; j++) {
      let newCell = document.createElement("div");
      rows[j].appendChild(newCell).className = "cell";
    }
  }
}

createGrid();
.cell {
  border: 1px solid gray;
  min-width: 10px;
  min-height: 10px;
  display: inline-block;
  margin: 0;
  padding: 0;
}

.gridRow {
  display: flex;
}
<!doctype html>

<html lang="en">

<head>
  <meta charset="utf-8">

  <title>EtchASketch</title>
  <link rel="stylesheet" href="style.css">

</head>

<body>
  <div id="drawingGrid">
  </div>

  <script src="app.js"></script>
</body>

</html>
m4n0
  • 29,823
  • 27
  • 76
  • 89
2

You should define the height of your row:

.gridRow{
  max-height:10px;
}

or setting the line-height of your row:

.gridRow{
  line-height:10px;
}

or using flexbox as defined by m4n0.