Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

127
Vistas
how do I fix the total amount of pixel used from a grid in css?

I'm sorry if this is a repeated question and I'd like some help cause I'm a noob. I created a grid. But every time I enter a different number, it uses a different total amount of pixel. I want the height and the width of the body that contains grid to stay exactly the same size no matter what the number of grid is.

https://moonsol124.github.io/ETCH-A-SKETCH-TOP-PROJECT-/

here is what I made.

java:

function createGrid(maxGrid = 16) {
    for (let i = 0; i < maxGrid; i++) {
        const divRow = document.createElement('div');
        divRow.classList.add('div-row');
        for (let j = 0; j < maxGrid; j++) {
            const div = document.createElement('div');
            div.classList.add('div-style');
            divRow.appendChild(div);
        }
        body.appendChild(divRow);
    }
addColorOnGrid();

}

css:

.div-row {
    display: flex;
    flex-direction: column;
}

.div-style {
    width: 25px;
    height: 25px;
    flex: 1 1 auto;
    background-color: RGB(0, 0, 0);
    border: solid 3px RGB(255, 255, 255);
}

html:

   <body>
        <div class="btn-group">
            <button class="btn" id="grid-button">Enter number of grid</button>
            <button class="btn" id="reset-button">Reset</button>
        </div>
        <div class="grid-body">

        </div>
    </body>

please tell me if my approach is wrong. thanks for your help.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I think this will help to you solve your problem I just add these parts

divRow.appendChild(document.createElement("div"));
document.getElementById('ac').appendChild(divRow);

it access the parent alement and create div inside that element

And you can pass any number here to test onLoad="createGrid(6)"

function createGrid(maxGrid) {

    for (let i = 0; i < maxGrid; i++) {
    
        const divRow = document.createElement("div");
        divRow.classList.add('div-row');
        
        for (let j = 0; j < maxGrid; j++) {
            const div = document.createElement('div');
            div.classList.add('div-style');
            divRow.appendChild(div);
        }
        
        divRow.appendChild(document.createElement("div"));
            document.getElementById('ac').appendChild(divRow);
    }

}

function popup() {

    let val = prompt("Please enter value here"); 
  
    const div = document.getElementsByClassName("div-style");
    
    for(i = 0; div.length > 0; i++){
        div[0].remove()
    }
  
   this.createGrid(parseInt(val));
  
}
.grid-body {
    display: flex;
    flex-direction: row;
}

.div-style {
    width: 25px;
    height: 25px;
    flex: 1 1 auto;
    background-color: RGB(0, 0, 0);
    border: solid 3px RGB(255, 255, 255);
}
<body onLoad="createGrid(6)">
        <div class="btn-group">
            <button class="btn" id="grid-button" onClick="popup()">Enter number of grid</button>
            <button class="btn" id="reset-button">Reset</button>
        </div>
        <div class="grid-body" id="ac">

        </div>
    </body>

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda