Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

122
Views
Display Flex not working with Divs created by Javascript

I am making a 16 x 16 square grid in Javascript and have two for loops that are creating divs for the grid. I'm assigning each row to be a flex-container and the cells inside to be flex boxes. Instead of the divs showing up as a grid, they are showing up as a bunch of cells, one under the other. Also, the borders are not showing properly too - so I tried adding a new class, called rows to get them to show, but that didn't work. Does anyone have any suggestions on why this is not working?

let board = document.getElementById("board");
let row;
let column;

for (let j = 0; j < 16; j++) {
  row = document.createElement("div");
  row.className = "flex-container";
  row.innerHTML = '';
  board.appendChild(row);

  for (let i = 0; i < 16; i++) {
    column = document.createElement("div");
    column.className = 'flexBoxes';
    column.classList.add = "rows";
    column.innerHTML = 'a';
    row.appendChild(column);

  }
}
flex-container {
  display: flex;
  flex-direction: row;
  width: 100%;
}

rows {
  background-color: pink;
  border-color: black;
  border-style: solid;
  border-width: 2px;
}

flexBoxes {
  flex: 1;
  border-color: black;
  border-style: solid;
  border-width: 2px;
  max-width: 20px;
  clear: both;
}
<div id="board"></div>

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Your CSS classes are missing the . at the start:

let board = document.getElementById("board");
let row;
let column;

for (let j = 0; j < 16; j++) {
  row = document.createElement("div");
  row.className = "flex-container";
  row.innerHTML = '';
  board.appendChild(row);

  for (let i = 0; i < 16; i++) {
    column = document.createElement("div");
    column.className = 'flexBoxes';
    column.classList.add = "rows";
    column.innerHTML = 'a';
    row.appendChild(column);

  }
}
.flex-container {
  display: flex;
  flex-direction: row;
  width: 100%;
}

.rows {
  background-color: pink;
  border-color: black;
  border-style: solid;
  border-width: 2px;
}

.flexBoxes {
  flex: 1;
  border-color: black;
  border-style: solid;
  border-width: 2px;
  max-width: 20px;
  clear: both;
}
<div id="board"></div>

Class selectors - CSS: Cascading Style Sheets | MDN

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!