The nodes on a 2-dimensional grid overlap each other when the browser window is resized

In my project, I have set up a grid with 24 rows and 64 columns, totaling to 24x64 nodes. However, I am facing an issue where the nodes overlap when I open the console window.

I am looking for a solution to automatically resize all nodes based on changes in the window size. Can anyone provide assistance?

grid.jsx

const Grid = ({ grid }) => {
  return (
    <div className="grid">
      {grid.map((row, rowIdx) => {
        return (
          <div className="cur-row" key={rowIdx}>
            {row.map((node) => (
              <Node node={node} />
            ))}
          </div>
        );
      })}
    </div>
  );
};

node.jsx

const Node = ({ node }) => {
  return (
    <div className="node"></div>
  );
};

css file

.grid {
  overflow: hidden;
  position: absolute;
  left: 20px;
  align-content: flex-start;
  justify-content: center;
  margin-top: 50px;
}

.cur-row {
  height: 23px;
}

.node {
  width: 23px;
  height: 23px;
  border: 1px solid rgb(175, 216, 248);
  display: inline-block;
}

Here is the grid when running the app:

And here is the grid when opening the console window: Some nodes are visibly overlapping with one another in the latter scenario.

I need a solution that will dynamically adjust the node sizes based on the window dimensions. Can someone provide guidance?

Answer №1

You have the option to utilize both Grid layout and Flexbox layout in order to achieve your desired outcome. Here is a sample code snippet demonstrating the use of CSS display: grid property.

/* This code snippet is for demonstration purposes only, disregard if you already have React code */
let cols = 64;
let rows = 24;
let gridEl = document.querySelector('.grid');

for (let i = 1; i <= 24; i++) {
  gridEl.innerHTML += `<div class="grid-row"></div>`;
}

document.querySelectorAll('.grid-row').forEach(function(item) {
  for (let j = 1; j <= 64; j++) {
    item.innerHTML += `<div class="grid-column"></div>`;
  }
});
:root {
  --grid-container: 1023px;
  --column-size: 23px;
  --column-count: 64;
  --border-color: rgb(175, 216, 248);
}

.grid {
  margin: 20px auto;
  max-width: var(--grid-container);
  overflow: auto;
  /* Enable scroll if necessary for responsive design */
}

.grid-row {
  display: grid;
  grid-template-columns: repeat(var(--column-count), 1fr);
}

.grid-column {
  height: var(--column-size);
  width: var(--column-size);
  border: 1px solid var(--border-color);
}
<div class="grid"></div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How do I retrieve the return value of another function in React?

I am working on a function called HandleCitiesArray where I need to access the myCitiesArray. To achieve this, I want to utilize the useSelector hook from Redux. Specifically, I aim to remove an object from initialState.myCities array. How can I go about ...

The background image will expand to fill any available space

I'm currently working on rendering divs with a background image CSS property. The images have fixed sizes, and I want to display them in a grid layout. However, the divs with background images stretch when there is extra space available, which is not ...

Ways to adjust timestamps (DayJs) by increments of 1 minute, 5 minutes, 15 minutes, 30 minutes, and more

Currently, I am exploring time functionality within React Native by utilizing DayJs. I have noticed a slight inconsistency when comparing 2 different points in time to calculate the time difference. Typically, everything works smoothly such as with 10:00 ...

Unnecessary spacing found within the side navigation menu

Recently, I decided to practice my web development skills by creating a basic webpage using flexbox for the topnav and CSS Grid 12 column layout for the main content. Everything was going smoothly until I encountered some extra whitespace in the sidenav se ...

Transitioning to webpack 5: Encountering an error - Unable to access the 'prototype' property of an undefined value

After spending several days attempting to fix this bug, none of the solutions I found online have been helpful. The issue arose after migrating from webpack version 4 to version 5. Here is the specific error that's occurring: https://i.stack.imgur.co ...

Urgent issue: a dependency request is an expression in Next.js

I am facing an issue with my multi-language site that uses i18next. Whenever I try to transition between pages, it takes too long and sometimes even refreshes the entire page. The console shows this warning: warn - ./node_modules/next-i18next/dist/common ...

Aligning floating elements in the center

Presently, I am working with the following code... <!DOCTYPE html> <html> <head> <title>Test</title> <meta charset="UTF-8" /> <link href="verna.css" type="text/css" rel="stylesheet" /> </head> ...

Issue encountered with using the `useCallback` inside `useFocusEffect` in React Navigation V6 leading

While exploring the React Navigation V6 documentation, I came across examples of utilizing useCallback within useFocusEffect. However, when attempting to implement the same code, I encountered an "invalid-hook-call" error. For reference: https://reactnavi ...

What is the best method for locating X-Path for elements inside a frameset?

I need help creating a test case for Selenium because I am struggling to locate elements on my website. The issue seems to be related to the fact that my site uses an HTML frame set. When I try to select all links using Firebug: //a I do not get any res ...

Persisting Undefined Values Even After Proper Prop Passing

I'm currently working on fetching and passing coaching data as props to another component for rendering on the frontend. I need to pass these props to the CoachingCard Component in order to display the coaching values. However, I'm encountering ...

Is there a more efficient approach to extracting the border width using javascript?

I implemented the following code: const playGard = document.getElementsByClassName("playGard")[0]; const borderW = getComputedStyle(playGard,null).getPropertyValue('border-left-width').substr(0,2); The result I obtained was "10". Is there a m ...

React-dropzone experiencing delays in loading new files for readers

Is there a way to handle conditional responses from an API and assign the desired value to errorMessageUploaded? I'm looking for a solution to receive error messages from the API, but currently, the errormessageupload variable is not being set withou ...

Tips for preventing tiny separation lines from appearing above and below unordered list elements

I am attempting to utilize twitter bootstrap to create a select-option style list. How can I eliminate the thin separation lines above and below the list of items? Refer to the screenshot: Below is the visible code snippet. It would be best to view the j ...

React Alert: Unable to update state in an unmounted React component

I have encountered an issue while using Redux in this particular file. When I click on the second component, I receive the following error message: Warning: Can't perform a React state update on an unmounted component. This is a no-op, but it ind ...

Integrating dynamic transition effects using Jquery Mobile for <div> elements

Just a friendly reminder, I must admit that my knowledge of Javascript is quite limited. I received a script from Padilicious to implement swipe navigation on my Jquery Mobile Site. It involves adding functions to a div tag like this: <div data-ro ...

adaptive height that scales with content proportionally

I am looking to achieve a responsive height based on the content inside my container. For example, I want the height of the container to adjust according to the text it contains. <!DOCTYPE html> <html lang="en"> <head> < ...

The state variable remains undefined even after integrating useEffect in a React.js component

Hello, I have a component within my React application that looks like this: import React, { useEffect, useState } from "react"; import AsyncSelect from "react-select/async"; import { ColourOption, colourOptions } from "./docs/data"; const App = () => ...

Is there a specific reason why state cannot be altered within the render method? Where is the optimal location for modifying the state prior to rendering?

I'm encountering an issue while attempting to update the state within the render method, resulting in the following error: Error: Maximum update depth exceeded. React has set a limit on nested updates to prevent infinite loops when setState is contin ...

Maintain a fixed navigation bar in Next.js using a customized `App` component

As I delve into learning Next.js, I encountered a stumbling block. In the traditional approach, here is how one would implement a navbar that appears on all pages: return ( <Container> <Navbar /> <Component {...pageProps ...

Issue with Swiper JS: The buttons on pages three and beyond are unresponsive

I'm having trouble with the functionality of the "Back" button on pages 2 and 3 of my website. Can anyone help me figure out why it's not working? My goal is to create a website that resembles a game menu, similar to Deus Ex The Fall. I'm u ...