How to properly align an item gallery with flexbox?

Asked

Viewed 115 times

-1

I’m starting to use CSS flex, and I realized that when we centralize a container, it looks like the elements inside the container seem to be centralized, but if I align it to the right, the container loses centralization:

Left aligned (I have the problem with a large space on the right): inserir a descrição da imagem aqui

Aligning to the center, I have a problem with the centralized elements:

inserir a descrição da imagem aqui

CSS:

.container {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
}

.item {
    max-width: 136px;
    flex: 1 0 136px;
    margin: 0 5px 20px;
}

This is the example above. And that’s not quite it that I want.

Using float, I could still make one screen alignment.

1 answer

1


Use an internal wrapper,

.container {
  display: flex;
  justify-content: center;
  width: 100%;
}

.container-inner {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-start;
  width: calc(146px * 3)
}

.item {
  max-width: 136px;
  flex: 1 0 136px;
  margin: 0 5px 20px;
}
<div class="container">
  <div class="container-inner">
    <div class="item">
      <img src="http://placekitten.com/136">
    </div>
    <div class="item">
      <img src="http://placekitten.com/136">
    </div>
    <div class="item">
      <img src="http://placekitten.com/136">
    </div>
    <div class="item">
      <img src="http://placekitten.com/136">
    </div>
  </div>
</div>

#Obs: you can remove the Calc from css, it was only for display, flex will do it automatically

#Edit: container aligned in the center, still can remove the Calc from the .container-inner will work the same way, the concept remains the same.. I hope I’ve helped

  • 1

    Mine but his answer was the way he DOES NOT want, with the last item in the center...

  • Yeah, so that’s the problem...

  • 1

    @Ruansenadev, I put that your answer is right, that’s exactly how I did and it worked, but I ended up arriving at the solution alone, anyway, thanks!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.