此fiddle是此question的结果。在其中,我有一个3 div flexbox和一个4 div flexbox。 (此刻,它与Angular无关)
理想情况下,flex divs可以放在容器内,但即使它们总是比它们的容器更窄,它们也不适合(一个div被推出)。更令人困惑的是,它们的数量不同。
当有4个div时,4的宽度之和为814px,而容器的宽度为819px。如果我将最宽div的宽度缩小9个像素,使总宽度为804,它们都按预期方式放入容器内。
#hbox1: width is 819, inner is 819, outer is 821
0 Object {borderWidth: 0, marginWidth: 0, paddingWidth: 0, flex: 1,
1 Object {borderWidth: 2, marginWidth: 0, paddingWidth: 0, flex: 2,
2 Object {borderWidth: 0, marginWidth: 0, paddingWidth: 0, flex: 1,
3 Object {borderWidth: 0, marginWidth: 0, paddingWidth: 0, flex: 1,
#hbox1 finalFlexTotal is 814
当有3个div时,我必须将最宽div的宽度缩小6个像素,以使它们都适合它们的容器。
#hbox2: width is 819, inner is 819, outer is 821
0 Object {borderWidth: 0, marginWidth: 0, paddingWidth: 0, flex: 1,
1 Object {borderWidth: 2, marginWidth: 0, paddingWidth: 0, flex: 2,
2 Object {borderWidth: 0, marginWidth: 0, paddingWidth: 0, flex: 1,
#hbox2 finalFlexTotal is 815
我已经考虑了borderWidth,marginWidth和paddingWidth都是0.为什么在没有任何随机调整的情况下div不适合他们的容器?
以下是小提琴消失的代码和HTML:
<html>
<body>
<div id="hbox1" style="border: 1px solid red">
<div flex=1 style="display:inline-block;height:100%">This is the A Box</div>
<div flex=2 style="border:1px solid black;display:inline-block;height:100%">This is the B Box</div>
<div flex=1 style="display:inline-block;height:100%">This is the C Box</div>
<div flex=1 style="display:inline-block;height:100%">This is the D Box</div>
</div>
<div id="hbox2" style="border: 1px solid red;margin-top:40px">
<div flex=1 style="display:inline-block;height:100%">This is the A Box</div>
<div flex=2 style="border:1px solid black;display:inline-block;height:100%">This is the B Box</div>
<div flex=1 style="display:inline-block;height:100%">This is the C Box</div>
</div><script type="text/javascript" src="jquery-1.10.2.js" />
<script type="text/javascript">
function flexBoxIt(selector) {
var $node,
index,
flexValue,
flexInfo = [],
totalFlex = 0,
finalFlexTotal = 0;
$(selector).children('div').each(function (index) {
$node = $(this);
flexValue = $node.attr("flex");
flexValue = flexValue ? parseInt(flexValue, 10) : 1,
flexInfo[index] = {
"borderWidth": parseInt($node.css('borderLeftWidth'), 10) + parseInt($node.css('borderRightWidth'), 10),
"marginWidth": parseInt($node.css('marginLeft'), 10) + parseInt($node.css('marginRight'), 10),
"paddingWidth": parseInt($node.css('paddingLeft'), 10) + parseInt($node.css('paddingRight'), 10),
"flex": flexValue,
"$jq": $node
};
totalFlex += flexValue;
});
width = $(selector).width();
console.log("%s: width is %d, inner is %d, outer is %d", selector, width, $(selector).innerWidth(), $(selector).outerWidth(true));
for (index in flexInfo) {
node = flexInfo[index];
node.width = Math.floor(width * node.flex / totalFlex - node.borderWidth);
finalFlexTotal += node.width;
console.log(index, node);
node.$jq.css("width", node.width + "px");
}
console.log("%s finalFlexTotal is %d", selector, finalFlexTotal);
}
flexBoxIt("#hbox1");
flexBoxIt("#hbox2");
</script>
</body>
</html>
答案 0 :(得分:2)
元素之间的空间会产生额外的宽度。您需要将所有元素放在一行上,如下所示:
<div flex=1 style="...">This is the A Bo</div><div ...>This is the B Box</div>...
答案 1 :(得分:1)
基本上,它是因为你的标记中有空格(新行被视为inline-block
元素中的空格)。他们造成了额外的空间。
article有一个格栅{{3}}如何对抗inline-block
元素之间的空间,你有很多不同的解决方案。