Is it possible to align divs so that they touch when they wrap to a new line, creating a grid-like appearance?

My React board component consists of an array of divs that I want to arrange in a grid-like map. The issue is, when the div wraps to a new line, there is significant space between each row. I aim to have the divs close together with no gaps.

GameMap state:

this.state = {
  gameMap: [1,1,1,1,1,1,1,1,1,1,1,1,1,1,
            1,1,1,1,1,1,1,1,1,1,1,1,1,1,
            1,1,1,1,1,1,1,1,1,1,1,1,1,1,
            1,1,1,1,1,1,1,1,1,1,1,1,1,1,
            1,1,1,1,1,1,1,1,1,1,1,1,1,1,
            1,1,1,1,1,1,1,1,1,1,1,1,1,1,
            1,1,1,1,1,1,1,1,1,1,1,1,1,1]
}

rendering the Grid:

const MapView = ({gameMap, rows, cols}) => {
const style = {
    'width': 140,
  };

  return (
    <div style={style}>
      {gameMap.map((tile, i) => {
        return (
          <div
            key={i}
            className={tile === 'player' ? 'player' : 'floor'}
          />
        )
      })}
    </div>
  );
}

CSS for tiles and board:

$cell-size: .6em;

.tile {
  display: inline-flex;
  flex-wrap: wrap;
  height: $cell-size;
  width: $cell-size;
  border-style: solid;
  border-width: 1px;
  box-sizing: border-box;
}

.player {
  @extend .tile;
  background-color: red;
}

.wall {
  @extend .tile;
  background-color: black;
}

.floor {
  @extend .tile;
  background-color: white;
}

Answer №1

One possible solution is to include font-size: 0; and line-height: 0; in the styling of the parent element.

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

utilize jQuery to load webpage with an HTML dropdown element

Querying the Campaigns: // Getting the campaigns $campaigns = $wpdb->get_results( "SELECT * FROM tbl_campaigns ORDER BY campaignID DESC", OBJECT_K ); // Displaying the Cam ...

Error message thrown: "The reference to $ is undefined. Are you using Jsfiddle?"

As a newcomer to javascript, I decided to experiment with creating a navigation bar that only appears when the user scrolls down using JSFiddle. However, when I transferred the code to Dreamweaver, it stopped functioning. Why is this happening? I've ...

step-by-step guide on transferring the text content of an HTML paragraph element to another HTML paragraph element through JavaScript in ASP.NET

I'm looking for help with passing the text value from one HTML paragraph element to another when a JavaScript function is called. The function loads a div element with an enlarged image and a paragraph content. Below is the code I am using: JavaScrip ...

Advanced jQuery Autocomplete with Nested Ajax Requests

I am currently developing a feature that allows users to search for albums using the Spotify Metadata API. The main functionality is working well, but I'm running into an issue with retrieving album cover art when making nested calls. This seems to be ...

Tips on invoking a method from a JavaScript object within an AJAX request

Considering the following code snippet: var submit = { send:function (form_id) { var url = $(form_id).attr("action"); $.ajax({ type: "POST", url: url, data: $(form_id).serialize(), dataType: 'json', succes ...

Unwillingness of Ajax to accept Names containing apostrophes as a parameter

$(".loadingPnl").removeClass('hdn'); var siteurlA = window.location.protocol + "//" + window.location.host + _spPageContextInfo.siteServerRelativeUrl; var callUrl = siteurlA + "/_layouts/15/SynchronyFinancial.Intranet/CreateMySite.aspx/S ...

Is that file or directory non-existent?

While working on developing a Discord bot, I keep encountering an error message stating: "Line 23: no such file or directory, open 'C:\Users\Owner\Desktop\Limited Bot\Items\Valkyrie_Helm.json']" even though the filep ...

In Visual Studio 2022, curly braces are now positioned on the same line for JavaScript code

I'm struggling with keeping Visual Studio from moving my curly braces to a new line whenever I auto format the code. Here's an example: Let's say I write this code: $('#details-modal').on('change', '#Foo1', fun ...

The default value for the MUI object in the Select component is not displayed initially, but is shown once

Is there a way to set a default value for the dropdown in this code snippet? Currently, the "chosenValue" is set by default, but I need to change it so that something else appears selected when the page loads. How can I achieve this? You can refer to thi ...

Rotate object within HTML table

I have a simple data structure as shown below: [ { "ClientId": 512, "ProductId": 7779, "Date": "2019-01-01", "Quantity": 20.5, "Value": 10.5 }, { "ClientId": 512, "ProductId": ...

Error: Attempting to assign a value to property 'x' of an undefined object has resulted in a TypeError

When I tried to create an array of randomly generated circles (stars) in my first code, I encountered a TypeError on this line: stars[i].x = Math.floor(Math.random() * w) Even though stars is defined in the code, the issue persisted. $(document).ready(f ...

Does the CORS error pertain only to the nginx server, or do I need to make changes to my code as

After successfully deploying my website for the first time, I encountered an issue with data not being properly sent from the contact form to our node.js server and then to our email address via nodemailer. The error message "Access to XMLHttpRequest at &a ...

Implementing automatic value setting for Material UI slider - a complete guide

I am working on developing a slider that can automatically update its displayed value at regular intervals. Similar to the playback timeline feature found on platforms like Spotify, Soundcloud, or YouTube. However, I still want the slider to be interactive ...

The function call FirstName.Val() is invalid and will not execute as intended

When attempting to create an array called requestData using the input values from a user details form, I encountered an error stating that .val() is not a function. The code snippet where this problem occurred is as follows: Jquery $('#submit' ...

Is it possible to execute asynchronous queries within a map function in Mongoose?

Currently, I am struggling to create queries using a .map function, pushing the results to an array and then returning it. The issue is that the array always ends up empty due to the asynchronous nature of the operation. Although I attempted using async/ ...

When Using MUI Masonry with Collapse, an Item Causes the Parent Width to Overflow

Imagine utilizing the dynamic capabilities of MUI's Masonry component, where each item contains a collapsible element that expands upon clicking. However, when an item is clicked to be moved to another column, it ends up exceeding the parent's sp ...

Issues with jQuery autocomplete when using special characters (Norwegian)

On my website in Norway, I am facing an issue with jQuery's autocomplete function. When users type in the Norwegian characters æ, ø, and å, the autocomplete feature suggests words with these characters within them but not ones that start with these ...

Receiving an eslint error while trying to integrate Stripe pricing table into a React web application

If you're looking to incorporate a Stripe documentation code snippet for adding a stripe-pricing-table element, here's what they suggest: import * as React from 'react'; // If you're using TypeScript, don't forget to include ...

Google Maps API - Custom Label for Map Markers

I am trying to implement a custom map on my website, and everything seems to be working fine except for one issue. The red marker on the map needs to have a label, but I don't want to use an additional image as an icon. Is there a way to add a label ...

One way to assign the starting value of a select element in React

I have a straightforward application that showcases data, with the state changing based on a select input. However, I am looking to set the default select option to United Kingdom instead of Afghanistan, which is currently the default due to alphabetical o ...