Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

125
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda