【问题标题】:CSS Flexbox (or Grid): 2 columns, row-wrapping, no growth, inner marginsCSS Flexbox(或网格):2 列,换行,无增长,内边距
【发布时间】:2020-03-19 19:29:21
【问题描述】:

我正在尝试获得类似这张图片的结果(模拟):

这是我迄今为止尝试过的,但它显然不能正常工作。

.flex-container {
	width: 50%;
	border: 2px dashed blue;
	margin-bottom: 40px;
}
.flex-container > .wrapper {
	display: flex;
	flex-wrap: wrap;
	margin: -5px;
}
.flex-container > .wrapper > input {
	flex-basis: 45%;
	flex-shrink: 1;
	flex-grow: 1;
	margin: 5px;
}

.wrapper.alt1 > input {
	flex-basis: 50%;
	flex-grow: 0;
}

.wrapper.alt2 > input {
	flex-basis: 45%;
	flex-grow: 0;
}
<div class="flex-container">
	<div class="wrapper">
		<input type="button" value="Example">
		<input type="button" value="Example">
		<input type="button" value="Example">
		<input type="button" value="Example">
		<input type="button" value="Example">
	</div>
</div>

<div class="flex-container">
	<div class="wrapper alt1">
		<input type="button" value="Example">
		<input type="button" value="Example">
		<input type="button" value="Example">
		<input type="button" value="Example">
		<input type="button" value="Example">
	</div>
</div>

<div class="flex-container">
	<div class="wrapper alt2">
		<input type="button" value="Example">
		<input type="button" value="Example">
		<input type="button" value="Example">
		<input type="button" value="Example">
		<input type="button" value="Example">
	</div>
</div>

我得到了一个动态的按钮列表,我更愿意将宽度设置为特定的 %,而不是为了让 flexbox 工作而将其向下舍入到不太具体的值。或者,如果有另一种方法可以将列数设置为特定的数字,那也可以。

编辑:我正在编辑以指定按钮的数量是动态的,因此它可能是奇数或偶数。对于左对齐的示例,这无关紧要,但对于居中的最终项目示例,这可能会出现问题。

【问题讨论】:

    标签: html css flexbox css-grid


    【解决方案1】:

    为此考虑 CSS 网格:

    .container {
      width: 50%;
      display: grid;
      grid-template-columns: repeat(4, 1fr);
      grid-gap: 5px;
      border: 2px dashed blue;
      margin-bottom: 40px;
    }
    
    input {
      grid-column: span 2;
    }
    
    .alt :nth-child(odd):last-child {
      grid-column: 2/span 2;
    }
    <div class="container">
      <input type="button" value="Example">
      <input type="button" value="Example">
      <input type="button" value="Example">
      <input type="button" value="Example">
      <input type="button" value="Example">
    </div>
    
    
    <div class="container alt">
      <input type="button" value="Example">
      <input type="button" value="Example">
      <input type="button" value="Example">
      <input type="button" value="Example">
      <input type="button" value="Example">
    </div>
    
    <div class="container alt">
      <input type="button" value="Example">
      <input type="button" value="Example">
      <input type="button" value="Example">
      <input type="button" value="Example">
      <input type="button" value="Example">
      <input type="button" value="Example">
    </div>

    【讨论】:

    • 谢谢。与 04FS 的答案一样,第一部分是完美的,但如果按钮数量为偶数,则第二部分无法正确对齐。您认为有解决方案吗?
    • 另外,感谢您提供grid 示例。我不熟悉,但快速阅读证明非常有趣。
    • 哈,是的,我在发现我可以组合两个伪选择器后自己才想到这一点。
    【解决方案2】:

    将边距放在一边,使用 flex: 0 0 calc(50% - 5px); 作为项目宽度,使用 justify-content: space-between; 作为 flex 父级。

    要获得最后一个元素居中的第二个版本,您可以简单地设置其侧边距auto

    .flex-container {
    	width: 50%;
    	border: 2px dashed blue;
    	margin-bottom: 40px;
    }
    .flex-container > .wrapper {
    	display: flex;
    	flex-wrap: wrap;
    	justify-content: space-between;
    	margin-top: -10px;
    }
    .flex-container > .wrapper > input {
    	flex: 0 0 calc(50% - 5px);
    	margin-top: 10px;
    }
    
    /*
      make that
      .flex-container > .wrapper.alt1 > input:nth-child(odd):last-child
      actually, so that it only applies to last child when it's an odd one
    */
    .flex-container > .wrapper.alt1 > input:last-child {
    	margin: 10px auto 0;
    }
    <div class="flex-container">
    	<div class="wrapper">
    		<input type="button" value="Example">
    		<input type="button" value="Example">
    		<input type="button" value="Example">
    		<input type="button" value="Example">
    		<input type="button" value="Example">
    	</div>
    </div>
    
    <div class="flex-container">
    	<div class="wrapper alt1">
    		<input type="button" value="Example">
    		<input type="button" value="Example">
    		<input type="button" value="Example">
    		<input type="button" value="Example">
    		<input type="button" value="Example">
    	</div>
    </div>

    如果 wrapper 的负 margin-top 会影响布局的其余部分,您也可以取消前两项的 margin-top,

    .flex-container > .wrapper > input:nth-child(1),
    .flex-container > .wrapper > input:nth-child(2) {
      margin-top:0;
    }
    

    【讨论】:

    • 谢谢。第一部分是完美的。但是第二部分仅在按钮数量为奇数的情况下才有效。如果存在偶数,则最终按钮未对齐。您认为有解决方案吗?
    • @therks 我的错,没有考虑这种极端情况——在 50:50 的概率不是那么“前卫”。容易修复,如果它也是一个奇怪的孩子,让我们只将自动边距应用于最后一个孩子。将最后一个选择器更改为 .flex-container &gt; .wrapper.alt1 &gt; input:nth-child(odd):last-child
    • 就像我在另一个答案中评论的那样,我只是在暗示我可以组合两个伪选择器后自己想出了这一点。我真的不知道该把录取通知给谁。 ?
    • 我决定接受 Temani 的回答,因为我觉得基于网格的解决方案可能是我正在做的更合适的解决方案。似乎 grid 更适合像我所拥有的那样更严格的布局,而 flex 则更适合更灵活的布局,好吧,顾名思义。但是非常感谢您的意见!
    • 我很好,不用担心。 flex 和 grid 之间的区别更多地在于它们的“维度”,而不是真正的“刚性与柔性” - flexbox 基于 one 主轴工作,grid 更多地用于二维,嗯 -网格。使用 flexbox 时,您有点“购买”第二个维度,通过允许项目进行包装 - 可以这么说,将您的一个主轴分成多个主轴。这两种工具都可以在这里完成工作,因此请选择您觉得更舒服的工具。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-07
    • 2019-01-29
    • 2021-05-21
    • 1970-01-01
    • 2021-01-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多