我有一个简单的flex-box布局的容器,如:

.grid {
  display: flex;
  flex-flow: row wrap;
  justify-content: space-between;
}

现在我想让最后一行中的项目与其他项目对齐。justify-content:之间的空间;应使用,因为网格的宽度和高度是可以调节的。

目前看来

在这里,我希望右下角的项目在“中间一列”中。最简单的方法是什么?下面是一个展示这种行为的小jsfiddle。

.exposegrid { display: flex; flex-flow: row wrap; justify-content: space-between; } .exposetab { width: 100px; height: 66px; background-color: rgba(255, 255, 255, 0.2); border: 1px solid rgba(0, 0, 0, 0.4); border-radius: 5px; box-shadow: 1px 1px 2px rgba(0, 0, 0, 0.2); margin-bottom: 10px; } <div class="exposegrid"> <div class="exposetab"></div> <div class="exposetab"></div> <div class="exposetab"></div> <div class="exposetab"></div> <div class="exposetab"></div> <div class="exposetab"></div> <div class="exposetab"></div> <div class="exposetab"></div> <div class="exposetab"></div> <div class="exposetab"></div> <div class="exposetab"></div> <div class="exposetab"></div> <div class="exposetab"></div> <div class="exposetab"></div> </div>


当前回答

如果你想让最后一项与网格对齐,请使用以下代码:

网格容器

.card-grid {
  box-sizing: border-box;
  max-height: 100%;
  display: flex;
  flex-direction: row;
  -webkit-box-orient: horizontal;
  -webkit-box-direction: normal;
  justify-content: space-between;
  align-items: stretch;
  align-content: stretch;
  -webkit-box-align: stretch;
  -webkit-box-orient: horizontal;
  -webkit-box-direction: normal;
  -ms-flex-flow: row wrap;
  flex-flow: row wrap;
}

.card-grid:after {
  content: "";
  flex: 1 1 100%;
  max-width: 32%;
}

网格中的项

.card {
  flex: 1 1 100%;
  box-sizing: border-box;
  -webkit-box-flex: 1;
  max-width: 32%;
  display: block;
  position: relative;

}

诀窍是将项目的max-width设置为.card-grid的max-width:after。

Codepen上的现场演示

其他回答

只需要添加一些具有相同属性的假项目,除了高度设置为0px到最后。

哦,男孩,我想我找到了一个很好的解决方案与最小的CSS和没有JS。看看吧:

img {width:100%;} li { display: inline-block; width:8em; list-style:none; } ul {text-align: justify;} <ul> <li> <img src="http://www.planwallpaper.com/static/images/kitty-cat.jpg" /> </li> <li> <img src="http://www.planwallpaper.com/static/images/kitty-cat.jpg" /> </li> <li> <img src="http://www.planwallpaper.com/static/images/kitty-cat.jpg" /> </li> <li> <img src="http://www.planwallpaper.com/static/images/kitty-cat.jpg" /> </li> <li> <img src="http://www.planwallpaper.com/static/images/kitty-cat.jpg" /> </li> <li> <img src="http://www.planwallpaper.com/static/images/kitty-cat.jpg" /> </li> <li> <img src="http://www.planwallpaper.com/static/images/kitty-cat.jpg" /> </li> <li> <img src="http://www.planwallpaper.com/static/images/kitty-cat.jpg" /> </li> <li> <img src="http://www.planwallpaper.com/static/images/kitty-cat.jpg" /> </li> <li> <img src="http://www.planwallpaper.com/static/images/kitty-cat.jpg" /> </li> <li> <img src="http://www.planwallpaper.com/static/images/kitty-cat.jpg" /> </li> <li> <img src="http://www.planwallpaper.com/static/images/kitty-cat.jpg" /> </li> <li> <img src="http://www.planwallpaper.com/static/images/kitty-cat.jpg" /> </li> <li> <img src="http://www.planwallpaper.com/static/images/kitty-cat.jpg" /> </li> <li></li> <li></li> <li></li> <li></li> <li></li> <li></li> <li></li> </ul>

这里的关键是要记住,我们试图实现的正是text-align: justify所做的!

HTML中的空元素是为了在不改变外观的情况下完美地显示最后一行,但考虑到您试图实现的目标,可能不需要这些空元素。为了在每种情况下实现完美的平衡,您至少需要x-4个空元素,x是要显示的元素数量,或者n-2, n是要显示的列的数量。

你不能。Flexbox不是一个网格系统。它没有实现您所要求的功能的语言结构,至少在使用justify-content: space between时是这样。使用Flexbox最接近的方法是使用列方向,这需要设置显式的高度:

http://cssdeck.com/labs/pvsn6t4z(注:不包括前缀)

ul {
  display: flex;
  flex-flow: column wrap;
  align-content: space-between;
  height: 4em;
}

然而,只使用列会更简单,它有更好的支撑,不需要设置特定的高度:

http://cssdeck.com/labs/dwq3x6vr(注:不包括前缀)

ul {
  columns: 15em;
}

尽管Flexbox即将出现缺口,但我将添加一个有效的解决方案。

它使用兄弟组合子检查2个条件。

它检查的第一个条件是元素是否位于倒数第二div:

对于4列布局,我们需要检查位置2和3 检查它是否在第4个div的第二行:第n -of-type(4n+2)或第3行div:第n -of-type(4n+3)

对于3列布局,我们只需要检查位置2

div:nth-of-type(3n+2)

然后我们可以像下面这样组合4列布局

div:nth-last-child(2) + div:nth-of-type(4n+2)

div:nth-last-child(2) + div:nth-of-type(4n+3)

我们还需要注意一种边情况,任何3n+2且倍数为4的数字都将得到35%的右边距div:n -last-child(2) + div:n -of-type(4n+4)

3栏布局将

div:nth-last-child(2) + div:nth-of-type(3n+2)

然后我们需要给上面的选择器添加一个边距。右边的差额将需要计算,并将取决于弹性基制。

我添加了一个带有3列和4列的示例和一个媒体查询。我还添加了一个小的JavaScript按钮,添加了一个新的div,所以你可以检查它的工作。

它有点像CSS,但很好用。 如果你想要更多的解释,我也在我的网站上写过。 https://designkojo.com/css-programming-using-css-pseudo-classes-and-combinators

var number = 11; $("#add").on("click", function() { number = number + 1; $("#main").append("<div>" + number + "</div>"); }); body { margin: 0; } main{ display: flex; flex-wrap: wrap; align-items: flex-start; align-content: flex-start; /* vertical */ justify-content: space-between; min-width: 300px; max-width: 1200px; margin: 20px auto; background-color: lightgrey; height: 100vh; } div { flex-basis: 30%; background-color: #5F3BB3; min-height: 20px; height: 50px; margin-bottom: 20px; display: flex; justify-content: center; align-items: center; color: #9af3ff; font-size: 3em; } div:nth-last-child(2) + div:nth-of-type(3n+2) { background-color: #f1b73e; margin-right: 35%; } @media screen and (min-width: 720px) { div { flex-basis: 22%; } div:nth-last-child(2) { background-color: greenyellow; } div:nth-of-type(4n+2) { background-color: deeppink; } /* Using Plus combinator is for direct sibling */ div:nth-last-child(2) + div:nth-of-type(4n+2) { background-color: #f1b73e; margin-right: 52%; } div:nth-last-child(2) + div:nth-of-type(4n+3) { background-color: #f1b73e; margin-right: 26%; } /* Also need to set the last to 0% to override when it become (3n+2) * Any number that is 3n+2 & multiple of 4 will get the 35% margin-right * div:nth-last-child(2) + div:nth-of-type(3n+2) */ div:nth-last-child(2) + div:nth-of-type(4n+4) { background-color: #f1b73e; margin-right: 0; } } <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="stylesheet" href="style.css"> <title>My New Project</title> </head> <body> <header> </header> <button id="add">Add</button> <main id="main"> <div>1</div> <div>2</div> <div>3</div> <div>4</div> <div>5</div> <div>6</div> <div>7</div> <div>8</div> <div>9</div> <div>10</div> <div>11</div> </main> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> <script src="action.js"></script> </body> </html>

一个可能的解决方案是使用justify-content: flex-start;在.grid容器中,其子元素的大小限制以及适当子元素的边距——取决于所需的列数。

对于一个3列网格,基本的CSS看起来是这样的:

.grid {
    display: flex;
    flex-flow: row wrap;
    justify-content: flex-start;
}

.grid > * {
    flex: 0 0 32%;
    margin: 1% 0;
}

.grid > :nth-child(3n-1) {
    margin-left: 2%;
    margin-right: 2%;
}

这是另一个不完美的解决方案,但它有效。

http://codepen.io/tuxsudo/pen/VYERQJ