Flexbox make one item 4x bigger than other items

江枫思渺然 提交于 2020-12-24 14:38:43

问题


I am looking at this Flexbox cheat sheet:

http://www.sketchingwithcss.com/samplechapter/cheatsheet.html#wrapcolumn

Here we have an example:

I want to make the bigitem 4x bigger than the smallitem, not 2x as big, but I cannot figure out how to do that?! I tried substituting 4 for 2 and no that didn't work.


回答1:


I want to make the bigitem 4x bigger than the smallitem, not 2x as big, but I cannot figure out how to do that?!

Don't use flex-grow for this task. Use flex-basis.

.bigitem { flex: 0 0 80%; } /* flex-grow, flex-shrink, flex-basis */

.smallitem { flex: 0 0 20%; }

The flex-grow property does not actually size flex items. It distributes free space in the container.

So flex: 4 0 0 on one item, and flex: 1 0 0 on the other item, means the following:

  • Determine the amount of free space on the main axis line (the row, in this case)
  • Divide that amount by five.
  • One item consumes four parts.
  • The other item consumes one part.

Because you're dealing only with free space, the 4 vs 1 flex-grow doesn't necessarily mean one item will be 4x the size of the other. It means that one item will consume 4x more free space than the other.

It also means that flex-grow values of 8 vs 2, 16 vs 4, 20 vs 5, etc., will yield the exact same result, because the proportions are the same.

See here for more details:

  • flex-grow not sizing flex items as expected
  • Make div fill remaining *horizontal* space in flexbox


来源:https://stackoverflow.com/questions/45202838/flexbox-make-one-item-4x-bigger-than-other-items

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!