With flexbox, center list aligning items on the left

Asked

Viewed 435 times

3

I’m using Bootstrap 4 as documentation (https://getbootstrap.com/docs/4.0/utilities/flex/).

Using the class Justify-content-center I have the following result:

Aplicação da classe Bootstrap, que não me atende

My div is with the classes: d-flex flex-wrap Justify-content-center

I would like these smaller items to be centered on the page, however, that their last line be aligned to the left, this way:

Como eu gostaria que fosse

Is it possible? I accept suggestions in another way to get the same result, even if you don’t use Flexbox. I need the solution to be "responsive", being able to serve for any size and amount of elements.

  • I don’t quite understand your question.. You want to get the result as in the second picture, right? But when I opened your codepen link, the result was the same as the second image, see this print

  • Hello @celsomtrindade, I ended up updating Codepen after the first interaction with the dvd user. I returned now to the original code. Anyway, it solved my problem using Javascript. Thank you.

1 answer

1


I believe you’ll achieve this by grouping divs within a div-mother and using the events resize and load (to adjust on page loading) of jQuery to set a width on this div of the exact size until the maximum of divs .item within it.

You don’t have to touch anything in your original code, just create a div with a id any (placed id="items") and group, see:

$(window).on("load resize", function(){
   var content = $(".content.d-flex").width();
   var items = $(".item").outerWidth(true);

   var items_width = Math.floor(content / items);
   
   $("#items").css({
      'max-width':items_width*items+'px'
   });
});
.content {
  max-width: 500px;
  margin: 0 auto;
  border: 1px solid black;
  margin-bottom: 50px;
}

.item {
  width: 100px;
  height: 100px;
  margin: 2px;
  background: gray;
  float: left;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">

<div class="content d-flex flex-wrap justify-content-center">
   <div id="items">
      <div class='item'></div>
      <div class='item'></div>
      <div class='item'></div>
      <div class='item'></div>
      <div class='item'></div>
      <div class='item'></div>
      <div class='item'></div>
   </div>
</div>

  • Hello dvd, I forgot to mention that I am thinking about responsiveness, being the three columns only one example. With its code, it no longer works by decreasing the screen, resulting in: [! [Column with two items][1]][1] [1]: http://i68.tinypic.com/o0huc.jpg

  • I couldn’t open the image.

  • I’m new to the forum and I’m not sure how to attach it to my comment. URL: https://uploaddeimagens.com.br/imagens/responsivo-jpg To view this result, simply resize the screen with its code.

  • I’ve updated Codepen: https://codepen.io/natodp/pen/EQgogR Simply resize the display window.

  • Blz, I’ll take a look here and get back to you

  • Quiet, thank you!

  • @natodp I updated the answer. I think you’ll only get it via JS. Abs!

  • Calm down, answer me. Thanks!

Show 3 more comments

Browser other questions tagged

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