css - flexbox 不适合我

标签 css flexbox

我正在玩 display: flex,但我无法让它做我想做的事:平均分布三列。听起来简单易行?那么请看这段代码:

<div class='main'>
    <div class="left">left</div>
    <div class="center">article article article article article article article article article article article article article article article article article</div>
    <div class="right">right</div>
</div>

我的 CSS 看起来像这样

body, html {
    width: 100%;
    height: 100%;
    padding: 0;
    margin: 0;
}
div {
    box-sizing: border-box;
    border: 1px dotted black;
    overflow: hidden;
}
.main {
    height: 100%;
    display: flex;
    flex-direction: row;
    flex-grow: 1;
}
.center, .left, .right {
    flex-grow: 1;
}

FIDDLE 在这里:http://jsfiddle.net/n2PSg/2/

您可以看到,中间的 DIV 几乎拉伸(stretch)到了可用宽度的 90%。我希望它正好是可用宽度的 33%。

尝试更改中心 DIV 内的文本:如果我将其更改为一个单词“文章”,一切都会按预期进行。

我哪里想错了?

最佳答案

flex 元素的 flex 属性是:

  • 灵活成长
  • flex 收缩
  • flex 基础

如果你想确保空间分布均匀,你应该为你的元素设置 flex-basis,否则它会根据需要分配空间:

.main > div{
   flex: 1 1 30%; /* using the shorthand */
}

参见 your updated fiddle

关于css - flexbox 不适合我,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24755874/

相关文章:

javascript - 使用 html 和 css 打印页面时,如何以最小高度对齐页脚和页眉?

html - 桌面与移动设备中的 Flex/Flex-Flow 问题

css - 如何创建一个内容居中并在包裹时保持居中的 flexbox 容器?

html - 限制 flexbox 元素的高度

javascript - 在 div 上完成多次点击时 onclick() 事件的问题

javascript - 将 chatango 添加到 Bootstrap 问题

CSS - 在 float div 中垂直居中图像

jQuery 在悬停时切换元素的颜色

html - 如何不过度拉伸(stretch)带有对齐项 : stretch 的 flex 容器中的最后一行

html - 我怎样才能把这个 div 放在我想要的地方?