简体   繁体   English

带有边距对齐问题的Flexbox

[英]Flexbox with margins alignment issue

I'm running into a flexbox + margin issue that I can't quite figure out. 我遇到了无法完全解决的flexbox + margin问题。 I'm assuming it's a flex-quirk but figured I'd see if anyone else has noticed this and if any solutions exist. 我假设这是一个灵活的问题,但是我想知道是否还有其他人注意到了这一点以及是否存在任何解决方案。

In the CodePen below you'll see I have a row of buckets set to flex: 1; 在下面的CodePen中,您将看到我有一行设置为flex: 1;的存储桶flex: 1; with a margin-left: 20px; margin-left: 20px; on them. 在他们。

Below that is a content area with a flex: 2; 在它下面是具有flex: 2;的内容区域flex: 2; and flex: 1; flex: 1; and equal margin-left: 20px; 并等于margin-left: 20px; 's as the buckets. 就像水桶一样。

Both areas have a container that pulls them left 20px to offset the first margin. 两个区域都有一个容器,可将其向左拉20px以抵消第一个边距。

CodePen Example Here 此处的CodePen示例

As you'll see - the two 1/3rd's divs do not line up with the 1 2/3rds div. 如您所见-两个1/3的div不与1 2/3的div对齐。 If you remove the margins, they all line up perfectly but margins seems to throw it off every so slightly. 如果删除页边距,它们的排列都非常完美,但是页边距似乎将其稍微甩开。

If I want these to line up, will I have to find a non-margin solution? 如果我想将它们排成一列,是否需要寻找非保证金解决方案?

Edit: This is the area I'm referring to specifically where you can see the mis-alignment: http://cl.ly/image/0z1z2j141V2X/Image%202014-12-19%20at%205.37.07%20PM.png 编辑:这是我指的是您可能会看到未对准的区域: http : //cl.ly/image/0z1z2j141V2X/Image%202014-12-19%20at%205.37.07%20PM.png

The margins are the issue here, resp. 利润是这里的问题。 the “gutters” you're placing between the boxes by those margins – because two times 1/3 of (1200px - 20px - 20px) is of course not the same as 2/3 of (1200px - 20px). 您在框之间放置的“装订线”的边距-因为两倍(1200px-20px-20px)的1/3当然不是(1200px-20px)的2/3。

The only way I can think of spontaneously to solve this, is to use a percentage margin value instead, and use according percentages for the flex property of the boxes as well. 我能自发解决的唯一方法是改用百分比边距值,并对框的flex属性也使用相应的百分比。

Doing away with the negative margins, using a margin of 1% each side in between, and eliminating the left margin of the first and the right margin of the last element, and flex: 32.667% (1/3 of 100%-2% for the margins) for the buckets and flex:66% / flex:32% for the 2/3 and 1/3 elements below (2/3 and 1/3 of 100%-1%), gives you this: http://codepen.io/CBroe/pen/YPWOJG 消除负边距,在两边之间使用每边1%的边距,并消除第一个元素的第一个左边距和最后一个元素的右边距,然后消除flex: 32.667% (100%-2%的1/3) (对于边距)和flex:66% / flex:32%用于下面的2/3和1/3元素(100%-1%的2/3和1/3),可以为您提供: http: //codepen.io/CBroe/pen/YPWOJG

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM