Почему у моего гибкого дисплея дисплея справа пустое пространство?

Я делаю приложение, которое отображает 100 столбцов случайной высоты внутри контейнера # bar, который является div. По какой-то причине в моем приложении справа от полосок есть пробелы. Я хочу, чтобы div уменьшился до размера содержимого, а затем центрировался на экране. Мое приложение выглядит так:

введите описание изображения здесь

JSX:

    return (
    <div id="main-container">

        <button onClick={() => newArray()}>New Array</button>

        <div id="bar-container">

            {arr.map((value, index) => (
                <div
                    className='bar'
                    key={index}
                    style={{
                        backgroundColor: "lightgreen",
                        height: `${value}px`
                    }}
                >
                    {value}
                </div>
            ))}

        </div>
    </div>

CSS:

#bar-container {
    align-items: flex-end;
    display: flex;
    margin: 100px;
}

.bar {
    margin: 0 2px;
}

Я попытался включить auto в поле для # bar-container, но он не центрирует полосы и не удаляет лишнее пустое пространство.


person SushiCode    schedule 11.04.2021    source источник
comment
попробуйте удалить margin: 100px;   -  person StepUp    schedule 11.04.2021


Ответы (1)


Используйте свойство justify-content,

justify-content: center;

Пример

#bar-container {
  align-items: flex-end;
  display: flex;
  justify-content: center;
}

.bar {
  margin: 0 2px;
  background: lightgreen;
  width: 10px;
}
<div id="bar-container">

  <div class="bar" style="height: 70px"></div>
  <div class="bar" style="height: 170px"></div>
  <div class="bar" style="height: 20px"></div>
  <div class="bar" style="height: 120px"></div>
  <div class="bar" style="height: 50px"></div>
  <div class="bar" style="height: 100px"></div>
  <div class="bar" style="height: 90px"></div>
  <div class="bar" style="height: 110px"></div>
  <div class="bar" style="height: 40px"></div>
  <div class="bar" style="height: 30px"></div>
  <div class="bar" style="height: 140px"></div>

</div>

person BOZ    schedule 11.04.2021