0

I am trying to center a flex-box horizontally but I want that its remaining space would be deleted. I don't wan't use justify-content: center; because I want that when the last row is loaded, it start from the start of the flex box.

The result I have so far is this:

enter image description here

I am want this result:

enter image description here

Note that the remaining space of the flex-box is removed. But I want that the flex-box would be responsive and I want to use flex-wrap: wrap. Using space-between, I get a similar result, but I wanted also use gap.

This is the code of the result I have:

html:

<div class="flex-container">
    <div class="flex-item">1</div>
    <div class="flex-item">2</div>
    <div class="flex-item">3</div>
    <div class="flex-item">4</div>
    <div class="flex-item">5</div>
  </div>

css:

.flex-container {
  padding: 0;
  margin: 0;
  display: flex;
  flex-wrap: wrap;
  gap: 1rem;
  border: 1px solid blue;
  width: 80%;
  margin: auto;
  align-content: center;
}

.flex-item {
  background-color: tomato;
  padding: 5px;
  width: 200px;
  flex-grow: 1 1 auto;
  margin: 10px;
  line-height: 20px;
  color: white;
  font-weight: bold;
  font-size: 2em;
  text-align: center;
}
kevin parra
  • 336
  • 4
  • 11

1 Answers1

-1

Will this do?

.flex-container {
  padding: 0;
  margin: 0;
  display: flex;
  gap: 1rem;
  border: 1px solid blue;
  width: auto;
  margin: auto;
  align-content: center;
}

.flex-item {
  background-color: tomato;
  padding: 5px;
  width: 200px;
  margin: 10px;
  line-height: 20px;
  color: white;
  font-weight: bold;
  font-size: 2em;
  text-align: center;
}
<div class="flex-container">
    <div class="flex-item">1</div>
    <div class="flex-item">2</div>
    <div class="flex-item">3</div>
    <div class="flex-item">4</div>
    <div class="flex-item">5</div>
  </div>
Deadpool
  • 7,811
  • 9
  • 44
  • 88