简体   繁体   中英

How to create a grid in react?

I am trying to create a simple grid that is uniform. For some reason, my grid has this weird spacing between rows. Here is my grid so far:

My Grid

在此处输入图像描述

I am creating a grid like so:

const grid = [];
for (let row = 0; row < GRID_ROW_LENGTH; row++) {
  const currentRow = [];
  for (let col = 0; col < GRID_COL_LENGTH; col++) {
    currentRow.push(newNode);
  }
    grid.push(currentRow);
}

Where node is just some data and is considered each cell in the grid. The node is simply wrapped in a div with the class name.node

Node Class returns:

<div
  className="node"
></div>

node.css

  .node {
      width: 20px;
      height: 20px;
      background-color: white;
      outline: 1px solid rgba(144, 175, 175, 0.75);
      display: inline-block;
    }

I display the grid like so:

<div className="grid">
  {grid.map((row, rowId) => {
    return (
      <div key={rowId}>
        {row.map((node, nodeId) => {
          return (
            <Node></Node>
          );
   })}
</div>`

grid.css

.grid {
  text-align: center;
}

How do I get rid of that spacing between my rows? Also, is there any way to make this grid somewhat responsive to the size of the webpage?

The spaces appears because of the inline-block layout. There are some technics how to get rid of it. But I would say the most proper way to create a grid is using display: flex or display: grid

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM