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

125
Views
Block Positon changes after changing innerHTML

I am trying to make a X-O game so html is this:

    const blocks = document.querySelectorAll(".block")
    let turn =  "X"
    for (let i = 0; i < blocks.length; i++) {
        blocks[i].addEventListener("click", function () {
            if (this.innerHTML == "") {
                this.innerHTML = turn
            }}
        )}
    .block {
        width: 100px;
        height: 100px;
        border: 1px solid black;
        text-align: center;
        line-height: 100px;
        font-size: 50px;
        font-weight: bold;
        cursor: pointer;
        padding: 0;
        display: inline-block;
}
    #game{
        max-width: 400px;
        max-height: 400px;
}
<div id="game">
    <div class="block"></div>
    <div class="block"></div>
    <div class="block"></div>
    <div class="block"></div>
    <div class="block"></div>
    <div class="block"></div>
    <div class="block"></div>
    <div class="block"></div>
    <div class="block"></div>
</div>

And every thing is OK. But when I press a block it's position changes a little bit: enter image description here

about 4 years ago · Santiago Gelvez
2 answers
Answer question

0

I have a slightly different solution using rows and column structure and flex.

Divide the structure into a tabular structure (ex: row, column). Try using display:flex to display the boxes in the same line.

const blocks = document.querySelectorAll(".col");
let turn =  "X";
for (let i = 0; i < blocks.length; i++) {
    blocks[i].addEventListener("click", function () {
        if (this.innerHTML == "") {
            this.innerHTML = turn;
        }
    });
 }
.row{
    display: flex;
}
.col {
    width: 100px;
    height: 100px;
    border: 1px solid black;
    text-align: center;
    line-height: 100px;
    font-size: 50px;
    font-weight: bold;
    cursor: pointer;
    padding: 0;
}
#game {
    max-width: 400px;
    max-height: 400px;
}
<div id="game">
  <div class="row">
    <div class="col"></div>
    <div class="col"></div>
    <div class="col"></div>
  </div>
  <div class="row">
    <div class="col"></div>
    <div class="col"></div>
    <div class="col"></div>
  </div>
  <div class="row">
    <div class="col"></div>
    <div class="col"></div>
    <div class="col"></div>
  </div>
</div>

about 4 years ago · Santiago Gelvez Report

0

Instead of using inline-block for your blocks, you could use a flex (display: flex) and wrapped (flex-wrap: wrap) container with fixed width and height for your #game div.

Using flex-wrap saves you from manually splitting your rows.

display: flex, by default, set flex-direction to row.
Using flex-wrap: wrap, if the content horizontally exceeds the width of the container, it is wrapped.

In our case, the container has dimention 400px, so we will get 3 element per row (since every block has width: 100px).

As in this runnable example

const blocks = document.querySelectorAll(".block")
let turn = "X"
for (let i = 0; i < blocks.length; i++) {
  blocks[i].addEventListener("click", function() {
    if (this.innerHTML == "") {
      this.innerHTML = turn
    }
  })
}
#game {
  /* Flex Wrapped Container*/ 
  display: flex;
  justify-content: start;
  align-items: center;
  flex-wrap: wrap;
  
  /* Dimensions */
  max-width: 400px;
  max-height: 400px;
}

.block {
  width: 100px;
  height: 100px;
  border: 1px solid black;
  margin: 1px; /* margin for some gap between boxes */
  text-align: center;
  line-height: 100px;
  font-size: 50px;
  font-weight: bold;
  cursor: pointer;
  padding: 0;
  display: inline-block;
  vertical-align: center;
}
<div id="game">
  <div class="block"></div>
  <div class="block"></div>
  <div class="block"></div>
  <div class="block"></div>
  <div class="block"></div>
  <div class="block"></div>
  <div class="block"></div>
  <div class="block"></div>
  <div class="block"></div>
</div>

about 4 years ago · Santiago Gelvez 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!