I have a gallery layout and I want to split each box with a margin. The problem is that the last box in each row does not align with the grid; there is still room on the right side, how can I do this without using a gasket? (which will make me add another div shell)
* { box-sizing: border-box; margin: 0; padding: 0; } ul { list-style-type: none; } .flex { display: -webkit-box; display: -webkit-flex; display: -ms-flexbox; display: flex; -webkit-flex-wrap: wrap; -ms-flex-wrap: wrap; flex-wrap: wrap; } .align-center { -webkit-box-align: center; -webkit-align-items: center; -ms-flex-align: center; align-items: center; } .space-between { -webkit-box-pack: justify; -webkit-justify-content: space-between; -ms-flex-pack: justify; justify-content: space-between; } .container { max-width: 1000px; margin: 0 auto; height: 100%; padding: 0 1em; border: 1px solid; } nav { background: #464646; height: 70px; } .box { background: lightblue; padding: 1em; -webkit-box-flex: 0; -webkit-flex: 0 0 calc(50% - 1em); -ms-flex: 0 0 calc(50% - 1em); flex: 0 0 calc(50% - 1em); text-align: center; margin-right: 1em; margin-bottom: 1em; }
<nav> <div class="container flex align-center space-between"> <ul> <li>Logo</li> </ul> <ul> <li>Home</li> </ul> </div> </nav> <div class="container"> <header> <h1>Header</h1> </header> </div> <div class="container"> <div class="gallery flex"> <div class="box">BOX 1</div> <div class="box">BOX 2</div> <div class="box">BOX 3</div> </div> </div>
Note: I cannot use flex grow: 1 because I do not always have two lines in a row.
I canβt use the space between them, because the grid is dynamic, for example, if the field width is 33.33% and you have five boxes, the second row will be with two cells on the left and the second on the right side.
user7122183
source share