[英]Inconsistent margin between flex items on last row
I'm using flex
. 我正在使用
flex
。 There seems to be an unexpected margin difference between two div
elements - the third row has a different margin between the divs than the first two rows: 两个
div
元素之间似乎存在意外的差异 - 第三行在div之间的边距不同于前两行:
How do I amend the code so that the third row's margin is the same as the first two rows? 如何修改代码,使第三行的边距与前两行相同?
.container { display: flex; flex-wrap: wrap; justify-content: space-between; } .container:after { flex: 1; content: ''; } .container form { width: 100%; display: flex; } .container .comment { flex: 1; } .square { padding: 10px; width: calc(100% / 9); margin: 0.7vw 0 0.7% 1vw; background: red; }
<div class="container"> <form method="post" style="margin: 0.7vw 0 0.7% 1vw;"> <input class="comment" type="text"> <input type="submit"> </form> <div class="square"> </div> <div class="square"> </div> <div class="square"> </div> <div class="square"> </div> <div class="square"> </div> <div class="square"> </div> <div class="square"> </div> <div class="square"> </div> <div class="square"> </div> <div class="square"> </div> <div class="square"> </div> <div class="square"> </div> <div class="square"> </div> <div class="square"> </div> </div>
JsFiddle: https://jsfiddle.net/4ydyoqmx/ JsFiddle: https ://jsfiddle.net/4ydyoqmx/
The problem is that your pseudo-element is consuming all available space on the last row. 问题是你的伪元素占用了最后一行的所有可用空间。
Yes, this is done to neutralize the effect of justify-content: space-between
on two flex items, which causes them to appear on opposite ends... 是的,这样做是为了抵消两个flex项目
justify-content: space-between
的justify-content: space-between
,这会导致它们出现在两端......
... but it also kills the proportional spacing that space-between
provides when the items fill the row. ...但是当项目填满行时,它也会杀死
space-between
提供的比例间距。
Until the flex specification is revised with specific last-row alignment properties, you could implement a hack that solves this problem: 在使用特定的最后一行对齐属性修改flex规范之前,您可以实现解决此问题的hack:
Add invisible flex items after the last real item.
在最后一个真实项目后添加隐形弹性项目。
In the example below, I've added seven phantom items to your code. 在下面的示例中,我为您的代码添加了七个幻像项。
.container { display: flex; flex-wrap: wrap; justify-content: space-between; } /* .container:after { flex: 1; content: ''; } */ .invisible { visibility: hidden; } .container form { width: 100%; display: flex; } .container .comment { flex: 1; } .square { padding: 10px; width: calc(100% / 9); margin: 0.7vw 0 0.7% 1vw; background: red; }
<div class="container"> <form method="post" style="margin: 0.7vw 0 0.7% 1vw;"> <input class="comment" type="text"> <input type="submit"> </form> <div class="square"></div> <div class="square"></div> <div class="square"></div> <div class="square"></div> <div class="square"></div> <div class="square"></div> <div class="square"></div> <div class="square"></div> <div class="square"></div> <div class="square"></div> <div class="square"></div> <div class="square"></div> <div class="square"></div> <div class="square"></div> <div class="square invisible"></div> <div class="square invisible"></div> <div class="square invisible"></div> <div class="square invisible"></div> <div class="square invisible"></div> <div class="square invisible"></div> <div class="square invisible"></div> </div>
More information: 更多信息:
There's a good chance you'll encounter some trouble with this rule: 你很可能会遇到这个规则的一些麻烦:
.square { margin: 0.7vw 0 0.7% 1vw; }
The flexbox specification advises against using percentage margin and padding in a flex container. flexbox规范建议不要在flex容器中使用百分比边距和填充。
Authors should avoid using percentages in paddings or margins on flex items entirely, as they will get different behavior in different browsers.
作者应该完全避免在弹性项目中使用填充或边距的百分比,因为它们会在不同的浏览器中获得不同的行为。
Here's some more: 这里还有一些:
4.2.
4.2。 Flex Item Margins and Paddings
Flex项目边距和填充
Percentage margins and paddings on flex items can be resolved against either:
Flex项目的边距和填充百分比可以通过以下任一方式解决:
- their own axis (left/right percentages resolve against width, top/bottom resolve against height), or,
他们自己的轴(左/右百分比分解宽度,顶部/底部分解高度),或,
- the inline axis (left/right/top/bottom percentages all resolve against width)
内联轴(左/右/上/下百分比全部解析宽度)
A User Agent must choose one of these two behaviors.
用户代理必须选择这两种行为之一。
Note: This variance sucks, but it accurately captures the current state of the world (no consensus among implementations, and no consensus within the CSSWG).
注意:这种差异很糟糕,但它准确地捕获了当前的世界状态(实现之间没有达成共识,并且在CSSWG中没有达成共识)。 It is the CSSWG's intention that browsers will converge on one of the behaviors, at which time the spec will be amended.
CSSWG的目的是浏览器将收敛于其中一个行为,此时规范将被修改。
There is a few different ways of doing this, depending on how you want the "squares" to respond: 有几种不同的方法可以做到这一点,具体取决于你希望“方块”如何响应:
flex-grow
: flex-grow
:
.square {
flex-grow: 1;
padding: 10px;
width: calc(100% / 9);
margin: 0.7vw 0 0.7% 1vw;
background: red;
}
Fiddle: https://jsfiddle.net/4ydyoqmx/2/ 小提琴: https : //jsfiddle.net/4ydyoqmx/2/
inline-block
: inline-block
:
.square {
display: inline-block;
padding: 10px;
width: calc(100% / 9);
margin: 0.7vw 0 0.7% 1vw;
background: red;
}
Fiddle: https://jsfiddle.net/4ydyoqmx/4/ 小提琴: https : //jsfiddle.net/4ydyoqmx/4/
Third Option: 第三种选择:
Removed content: ''
from .container:after
because that was causing problems 删除
content: ''
来自.container:after
因为这会导致问题
It's the justify-content: space-between
setting on .container
which does this (in conjunction with flex-wrap: wrap;
): It evently distributes the elements in those lines that are filled, but in the last line that would not look good, so it leaves the predefined / default margin between those elements. 这是
justify-content: space-between
.container
justify-content: space-between
设置,它执行此操作(与flex-wrap: wrap;
):它最终将元素分布在那些被填充的行中,但是在最后一行中看起来不太好,因此它在这些元素之间留下预定义/默认边距。 (That's similar to a justified text paragraph) (这与合理的文本段落类似)
One possibility to avoid this is to simply erase justify-content: space-between
, thereby setting it to the default left
alignment (but they won't be distributed across the whole width anymore): 避免这种情况的一种可能性是简单地擦除
justify-content: space-between
,从而将其设置为默认的left
对齐(但它们将不再分布在整个宽度上):
https://jsfiddle.net/h8ejaob7/ https://jsfiddle.net/h8ejaob7/
It is doing like that because you're justifying content using space between. 这样做是因为你在使用空间来证明内容的合理性。 So on the screen it can not accommodate 7th div so it puts only 6 div on screen and puts equal space between 6 div elements.But in the last line there has only 2 div's so that means your 3rd line is not fully filled with div elements.
所以在屏幕上它不能容纳第7个div所以它在屏幕上只放置6个div并在6个div元素之间放置相等的空间。但是在最后一行只有2个div,这意味着你的第3行没有完全填充div元素。 That's why it is not putting space as it did on upper line's div's .
这就是为什么它没有像上线那样放置空间。
If you put 4 more div's then you'll get exact effect as it has on upper line's . 如果再放4个div,那么你将获得与上线相同的精确效果。
This problem could be solved by adding this piece code to your css.. 这个问题可以通过将这个代码添加到你的CSS来解决。
*{margin:0px;
padding:0px}
Hope it works.. 希望它有效..
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.