0

I'm making a html chess board. The problem is between each row there are unwanted space

I'm trying to remove vertical space between the div elements with margin-top: 0px. But it doesn't work? My browser is Firefox

Here is the javascript i use to generate the chess board

for (var i = 0; i < 8; i++) {
  let row = document.createElement("div");
  row.className = "row";
  for (var j = 0; j < 8; j++) {
    let tile = document.createElement("div");
    tile.className = "tile";
    tile.id = `${i}_${j}`
    if (i % 2 == j % 2) {
      tile.style.background = "white"
    } else {
      tile.style.background = "gray"
    }
    row.appendChild(tile);
  }
  document.getElementById("game").appendChild(row);
}
body {
  background-color: black;
}

.tile {
  height: 60px;
  width: 60px;
  display: inline-block;
}
.row {
  margin-top: 0px;
}
  <body>
    <div id="menu"></div>
    <div id="game"></div>
    <script src="client.js"></script>
  </body>
Rickard Elimää
  • 7,107
  • 3
  • 14
  • 30
Green Ball
  • 64
  • 9

3 Answers3

2

Add display: flex to .row.

EDIT: tried it in Firefox, Chrome and Safari.

for (var i = 0; i < 8; i++) {
  let row = document.createElement("div");
  row.className = "row";
  for (var j = 0; j < 8; j++) {
    let tile = document.createElement("div");
    tile.className = "tile";
    tile.id = `${i}_${j}`
    if (i % 2 == j % 2) {
      tile.style.background = "white"
    } else {
      tile.style.background = "gray"
    }
    row.appendChild(tile);
  }
  document.getElementById("game").appendChild(row);
}
body {
  background-color: black;
}

.row {
  display: flex;
}

.tile {
  height: 60px;
  width: 60px;
}
  <body>
    <div id="menu"></div>
    <div id="game"></div>
    <script src="client.js"></script>
  </body>
Rickard Elimää
  • 7,107
  • 3
  • 14
  • 30
0

Setting the line-height property of row to 0 will solve the issue. This will it will take the height of the elements inside it i.e. tile

-2

I found a solution: setting margin-top to a negative value, in my case -4px

Green Ball
  • 64
  • 9