Modifying inline styles in React is as easy as placing the number within a div tag

I'm currently working on a React table component that consists of rows and columns. My main goal is to have the rows arranged vertically and the numbers within each row aligned horizontally.

Here's how it looks at the moment:

https://i.sstatic.net/hEVwH.png

However, when I encapsulate each number within a basic Cell component, the layout changes to this:

https://i.sstatic.net/BIXQ2.png

Below is the code snippet for reference:

function Cell({ number }) {
    return (
        <div>
            {number}
        </div>
    )
}

function Row({ row }) {
    // issue arises when using Cell component
    const mapped = row.map((datapoint, index) => {
        return <li className="horizontalList" key={index.toString()}><Cell number={datapoint}/></li>
    })
    
    // uncomment this and comment out the other one to see the intended output
    
    // const mapped = row.map((datapoint, index) => {
    //     return <li className="horizontalList" key={index.toString()}>{datapoint}</li>
    // })
    return (
        <div className="row">
            <ul>
                {mapped}
            </ul>
        </div>
    )
}


function App() {
   const data = [
        [1, 2, 3, 4, 5],
        [1, 2, 3, 4, 5],
        [1, 2, 3, 4, 5],
        [1, 2, 3, 4, 5],
        [1, 2, 3, 4, 5],
    ]

  const mapped = data.map((rows, index) => {
      return <li className="verticalList" key={index.toString()}><Row row={rows}/></li>
  })

  return (
    <div className="App">
        <div className="container">
            <ul>
                {mapped}
            </ul>
        </div>
    </div>
  );
}

The accompanying CSS styles are as follows:

.container {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.horizontalList {
  display: inline;
  padding: 10px;
}

.verticalList {
  padding: 10px;
}

Answer №1

When it comes to inline elements, like <span>, they typically display inline next to each other. However, if you were to insert a block element, such as <div>, inside them:

<span>
  <div></div>
</span>

The div will take up the full width and disregard the boundaries of its parent span. This is similar to what happens when you wrap an inline number in a <Cell />, which is essentially just a div.

You can test this difference by running the following code snippet:

<h4>Inline elements</h4>

<span>item</span>
<span>item</span>

<hr />

<h4>Block element inside inline elements</h4>

<span><div>item</div></span>
<span><div>item</div></span>

Solution: To resolve this issue, change the <Cell /> from a div to a span.

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

Axios is unable to retrieve data in React render, but it is successful when accessed through the console

I am currently in the process of developing an e-commerce website and encountering an issue with rendering image data stored in MongoDB. The goal is to fetch the image data using axios and display it on the website. However, despite successfully fetching t ...

There was an issue converting the value {null} to the data type 'System.Int32', resulting in a 400 error

Attempting to make a POST request with some missing data is causing errors in my angular form. Here is the payload I am using: DeviceDetail{ deviceId:'332', sideId: null, deviceName:'test' } Unfortunately, I encountered a 400 bad re ...

Anticipated to provide a result upon completion of the arrow function with consistent-return

exports.create = (req, res) => { if (!req.body.task) { return res.status(400).send({ message: "Task Can't be empty", }); } const task = new Task({ task: req.body.task, }); task.save() .then((data) => { ...

Using jQuery to create a drop-down menu

I currently have a table filled with data. Whenever a user clicks on a cell within a specific column, I would like it to transform into a select dropdown menu. This dropdown menu will allow the user to choose a category for that particular row. The selecte ...

Troubleshooting challenges arise with Bootstrap's button group spacing issue

I am encountering spacing issues with the markup provided below. <h2>Profitability Report</h2> <form method="post"> <div class="row"> <div class="col-md-12"> <div class="btn-group btn-group-toggle ...

confirming the phone field's character count

My website has multiple phone fields, each formatted like this: $("#HomePhoneInput").inputmask({ "mask": "(999) 999-9999" }); $("#WorkPhoneInput").inputmask({ "mask": "(999) 999-9999" }); $("#CellPhoneInput").inputmask({ "mask": "(999) 999-9999" ...

Is it possible for JavaScript to capture scroll events while allowing clicks to pass through?

Currently, I have a setup where the user needs to interact with the layer behind the transparent scroll capture: http://jsbin.com/huxasup/4/edit?html,css,js,console,output scrollerCapture = document.querySelector('.scroller-capture'); scrollerC ...

jQuery does not support the addition of new fields in HTML

Recently, I've been working on creating a lucky number generator. Initially, I developed it using C# and now I'm in the process of transitioning it to JavaScript and jQuery. You can view the latest version here. However, I've encountered an ...

Ways to dismiss a Modal popup instantly without having to wait for an ajax response

I am currently facing an issue with sending email attachments through ajax. The process takes too long to send and close the email modal window. I am looking for a solution where the modal window closes immediately after clicking the send email button, all ...

Slicing an array in Javascript/Angular before it is officially initialized

Is it possible to specify the portion of an array to retrieve before making a $http GET request in Angular? I am aware of slicing arrays, but wondering if I can set this up in advance for better performance. In PHP, you can do something similar, but not ...

Create an input box with a designated class

When I click the button in my HTML and JavaScript code below, I am able to dynamically add a text field and radio button. This functionality is working properly. However, I also want the newly generated text field to have the same font size and appearance ...

In order to determine if components linked from anchor elements are visible on the screen in Next.js, a thorough examination of the components

Currently, I am in the process of developing my own single-page website using Next.js and Typescript. The site consists of two sections: one (component 1) displaying my name and three anchor elements with a 'sticky' setting for easy navigation, a ...

Email attachments not working in Node Mailgun

I am facing an issue with my code that is designed to send emails using Mailgun in Node. The code functions as expected and sends the email successfully; however, it fails to attach the specified files. // pdfA and pdfB are both buffers defined earlier le ...

What is the best way to convert a series of sentences into JSON format?

I'm struggling with breaking down sentences. Here is a sample of the data: Head to the dining room. Open the cabinet and grab the bottle of whisky. Move to the kitchen. Open the fridge to get some lemonade for Jason. I am looking to format the outc ...

Click on AngularJS to reveal additional details

I have a dropdown menu displaying a list of operations that users can select. When the user clicks on an operation, I want all related information to be displayed instantly. This is what I have implemented so far: app.controller('selectAll', ...

Modify a quartet of divs simultaneously

I am currently working on a project that involves 4 input fields, each accompanied by a dropdown element for selecting currency. My goal is to create a JavaScript function that will update all input fields when one of them is changed. For instance, if I s ...

Node.js - Retrieving POST request parameters and directing users in an Express application

I encountered this specific issue while setting up a post endpoint for my nodejs CLI script that utilizes express to handle requests. app.use( express.static( path.format({dir: __dirname, base: 'client/dist'})) ); app.use( express ...

Utilizing version 4 of Bootstrap to create a visually appealing full

I am currently using the latest version of bootstrap, but I have encountered an issue with my sidebar being too short and not reaching full height. You can view the problem here: Is this problem caused by my CSS or is it a bootstrap issue? And how can ...

The JavaScript game's set interval function is failing to execute at consistent 10-second intervals

Trying to incorporate gravity into a circular object in an HTML, CSS, and JavaScript game. Here is the relevant JavaScript code section: Check out the code on repl.it: https://repl.it/join/wukcvzow-iamapersonthing The specific part of interest in this tu ...

Attempting to have this .js lightbox appear as soon as the page loads

This Lightbox is absolutely stunning! However, I am looking for a way to automatically trigger the lightbox when the page loads. ...