flex-grow not sizing flex items as expected

前端 未结 5 1078
你的背包
你的背包 2020-11-22 11:55

It seems that the content inside a flex div affects its calculated size concerning the flex-grow property. Am I doing something wrong?

In the fiddle pr

5条回答
  •  一整个雨季
    2020-11-22 12:17

    flexbox doesn't respond well to margins, in my opinion.

    The better approach / workaround I prefer is to make sure all flex children have 0 margins, set the flex container to justify-content: space-between;, and then give the children a total width less than 100%. The remainder will be your margin.

    In other words, if you want two elements per row, set each to 49% wide and you'll have 2% space between them. Three elements, each at 32% wide and you'll have 2% between them. In the calculator example, the 0 cell should be 66% wide and the rest 32%.

    Edit: Note that because reasons (namely that content-box is terrible), if any of your children have borders you'll need to use box-sizing: border-box for my suggestion to work properly.

    https://jsfiddle.net/r3L1mtbe/2/

提交回复
热议问题