[英]Equal space between flex items
Is there a way to put a full unit of space on both sides of all items, including the first and last? 有没有办法在所有物品的两面都放置一个完整的空间,包括第一个和最后一个?
I am trying to find a way to have equal spacing around flexbox children. 我正在尝试找到一种在flexbox子代之间具有相等间距的方法。
In this article it seems like the nearest thing is justify-content: space-around
. 在本文中 ,最近的事情似乎是
justify-content: space-around
。 It says that: 它说:
space-around
: items are evenly distributed in the line with equal space around them.space-around
:项目在行中均匀分布,周围具有相等的空间。 Note that visually the spaces aren't equal, since all the items have equal space on both sides.请注意,从视觉上看,空间是不相等的,因为所有项目的两侧都具有相等的空间。
The first item will have one unit of space against the container edge, but two units of space between the next item because that next item has its own spacing that applies.
第一项相对于容器边缘有一个单位的空间,但是下一项之间有两个单位的空间,因为下一项具有自己的适用间距。
There are at least two methods for equal space between all items, including the first and last items. 至少有两种方法可以使所有项目(包括第一个项目和最后一个项目)之间的距离相等。 One method, however, doesn't yet have full browser support.
但是,一种方法尚未完全支持浏览器。
Note this section from Firefox documentation: 请注意Firefox文档中的这一部分:
In-flow
::after
and::before
pseudo-elements are now flex items .流入
::after
和::before
伪元素现在是flex项目 。
In fact, all major browsers consider pseudo-elements on a flex container to be flex items. 实际上,所有主流浏览器都将flex容器上的伪元素视为flex项。
Knowing that, add ::before
and ::after
to your container. 知道了,将
::before
和::after
添加到您的容器中。
With justify-content: space-between
and zero-width pseudo-elements, the visible flex items will appear evenly spaced. 使用
justify-content: space-between
和零宽度的伪元素之间,可见的flex项目将均匀分布。
flex-container { display: flex; justify-content: space-between; } flex-container::before { content: ""; } flex-container::after { content: ""; } /* non-essential decorative styles */ flex-container { padding: 5px 0; background-color: lightyellow; border: 1px solid #aaa; } flex-item { height: 50px; width: 75px; background-color: lightgreen; }
<flex-container> <flex-item></flex-item> <flex-item></flex-item> <flex-item></flex-item> <flex-item></flex-item> </flex-container>
space-evenly
The CSS Box Alignment Module , which is the W3C's unfinished proposal to establish a common set of alignment properties for use across all box models, provides the space-evenly
value for use with the justify-content
and align-content
properties. CSS框对齐模块 ,这是W3C尚未完成的建议,用于建立一套通用的对齐属性以供所有框模型使用,它提供了
space-evenly
值,以与justify-content
和align-content
属性一起使用。
4.3.
4.3。 Distributed Alignment: the
stretch
,space-between
,space-around
, andspace-evenly
keywords分布式对齐方式:
stretch
,space-between
,space-around
和space-evenly
关键字
space-evenly
The alignment subjects are evenly distributed in the alignment container, with a full-size space on either end.
对齐对象均匀地分布在对齐容器中,两端各有一个完整的空间。
The alignment subjects are distributed so that the spacing between any two adjacent alignment subjects, before the first alignment subject, and after the last alignment subject is the same.
对准对象被分布,使得在任何两个相邻对准对象之间,在第一个对准对象之前和最后一个对准对象之后的间隔是相同的。
As of this writing, however, it looks like space-evenly
only works in Firefox and Chrome . 但是,在撰写本文时,它看起来似乎只在Firefox和Chrome中适用于
space-evenly
。
flex-container { display: flex; justify-content: space-evenly; } /* non-essential decorative styles */ flex-container { padding: 5px 0; background-color: lightyellow; border: 1px solid #aaa; } flex-item { height: 50px; width: 75px; background-color: lightgreen; }
<flex-container> <flex-item></flex-item> <flex-item></flex-item> <flex-item></flex-item> <flex-item></flex-item> </flex-container>
Also, here's a useful demo from the MDN justify-content
page for testing space-evenly
and other values in your browser. 另外,这是MDN
justify-content
页面中的有用演示,用于测试浏览器中的space-evenly
和其他值。 https://jsfiddle.net/gkrsr86n/ https://jsfiddle.net/gkrsr86n/
You can do this by setting the padding
of the flex container and the margin
of the flex items: 您可以通过设置弹性容器的
padding
和弹性项目的margin
来做到这一点:
.container {
display: flex;
padding: 0 1%;
}
.item {
flex: 1;
margin: 0 1%;
}
https://codepen.io/danieldilly/pen/PjgRbe https://codepen.io/danieldilly/pen/PjgRbe
In firefox only there is a space-evenly
value for justify-content
that does this. 在firefox中,只有
justify-content
的space-evenly
值可以做到这一点。
It's in the CSS3 working draft 在CSS3工作草案中
https://www.w3.org/TR/css-align-3/#valdef-align-content-space-evenly https://www.w3.org/TR/css-align-3/#valdef-align-content-space-evenly
div { display: flex; height: 100px; justify-content: space-evenly; border: 1px solid black; margin: auto; } span { width: 20%; background: red; }
<div> <span></span> <span></span> <span></span> </div>
This is a perfect use case for flex-basis
and justify-content: space-between
if you know how many components will be in your row beforehand. 这是
flex-basis
和justify-content: space-between
的完美用例justify-content: space-between
如果您事先知道行中将有多少个组件,请justify-content: space-between
使用justify-content: space-between
。 Specify a flex-basis percentage on your flex-items that totals less than 100% for all items. 在弹性项目上指定所有项目的总弹性百分比小于100%。 The leftover percentages will become margins.
剩余的百分比将成为边距。
No psuedo elements, child selectors or padding/margin. 没有伪元素,子选择器或填充/边距。
div { display: flex; justify-content: space-between; height: 100px; } span { flex-basis: 32%; background: red; }
<div> <span></span> <span></span> <span></span> </div>
You could try this: 您可以尝试以下方法:
*, *:before, *:after { -webkit-box-sizing: border-box; -moz-box-sizing: border-box; box-sizing: border-box; } .list_wrap { display: flex; flex-wrap: wrap; border: 1px solid purple; padding-top: 5px; } .list_item { width: 24%; margin-right: 0.8%; margin-bottom: 5px; height: 30px; border: 1px solid green; } .list_item:nth-child(4n+1) { margin-left: 0.8%; }
<div class="list_wrap"> <div class="list_item"></div> <div class="list_item"></div> <div class="list_item"></div> <div class="list_item"></div> <div class="list_item"></div> <div class="list_item"></div> <div class="list_item"></div> <div class="list_item"></div> </div>
You can use flexbox's all property form here. 您可以在此处使用flexbox的all属性形式。 this is perfect example for how to use complete flex property with example http://the-echoplex.net/flexyboxes/
这是如何通过示例http://the-echoplex.net/flexyboxes/使用完整的flex属性的完美示例
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.