Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

139
Visualizações
React inline styling changes once I wrap number in div

I'm building a React table component with rows and columns. Naturally, I want the rows to be distributed vertically and the numbers within the rows to be distributed horizontally.

Right now, it looks like this:

enter image description here

But when I wrap each number in a simple Cell component, I end up with this:

enter image description here

React Code:

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

function Row({ row }) {
    // problem when data point is wrapped in Cell
    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 how it should look
    
    // 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((row, index) => {
      return <li className="verticalList" key={index.toString()}><Row row={row}/></li>
  })

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

CSS:

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

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

.verticalList {
  padding: 10px;
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Inline elements (e.g. <span>) normally display inline beside eaah other. However, when you insert a block element (e.g. <div>) inside like:

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

That div keeps taking the full width and ignores the boundaries of its parent span.
And that's exactly what happens when you wrap the number, which is inline by your styles, inside a <Cell />, as it's just a div.

Run this code snippet to check the difference:

<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: Make the <Cell /> a span instead of div.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda