css – 删除带有包装儿童的Flexbox上的额外间距?

前端之家收集整理的这篇文章主要介绍了css – 删除带有包装儿童的Flexbox上的额外间距?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

每当flexBox内的物品包裹时,它们都会拉伸弹性箱,在侧面留出额外的空间.它只在物品包装时发生.如何摆脱额外的间距?

问题的屏幕截图

enter image description here

我想要它做什么

enter image description here

HTML

CSS

  1. body > div {
  2. display: flex;
  3. width: 34rem;
  4. justify-content: space-between;
  5. border: 1px solid black;
  6. }
  7. body > div > div {
  8. display: flex;
  9. flex-flow: row wrap;
  10. }
  11. body > div > div.red {
  12. background: rgba(255,.1);
  13. }
  14. body > div > div.green {
  15. background: rgba(0,255,.1);
  16. flex-shrink: 0;
  17. }
  18. body > div > div > div {
  19. margin: 1rem;
  20. height: 5rem;
  21. width: 5rem;
  22. background: rgba(0,.1);
  23. }

See an example on JSFiddle

最佳答案
更新2

据我所知,所有标准都得到了满足.

>没有固定宽度
>适应各种宽度的内容
>没有过多的填充物

变化

>红色框具有对齐内容:空格 – 在其间减少红色框左右两侧的填充.
>每个弹性项目(flex-ghost除外)是flex:0 0 auto.
> Flex-ghost现在是纯粹的宽度,没有flex的高度:2 0 auto.
>添加了一些JS以使其具有交互性.

PLUNKER

更新1

OP表示固定宽度不起作用,因为如果内容(即弹性项目)可以改变宽度并打破布局.真正构成固定布局的是外容器是刚性的,具有绝对值测量和/或最大限制长度.固定,液体,反应灵敏,适应性强,在一些白葡萄酒等中炒,这不是真正的问题.真正的问题是,在处理不均匀的行时,行方向的flexBox不能很好地处理计算.因此,为了确保内容的统一形成,我发现:

> justify-content:如果您执行下一步,space-around将完美运行.
>再添加一个弹性项目以使行均匀,如果必须具有奇数量的弹性项目,则使用visibility:hidden或opacity:0使最后一项不可见,但不显示:none

详细信息在JS区域(没有添加JS,只是用于评论的空间).

SNIPPET

  1. /* Changes
  2. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  3. CSS
  4. line 1 to 8
  5. || Needed basic reset
  6. =====================
  7. line 12
  8. || To demonstrate behavior in liquid layout.
  9. =====================
  10. line 19 to 24
  11. || Needed some type of measurement,otherwise it
  12. || will expand to fill in that extra space on
  13. || the right.
  14. || flex was set to shrink when width has reached
  15. || it's basis of 62%.
  16. ======================
  17. line 22 to 28
  18. || Width and flex growth/shrink/basis were added
  19. || for the same reasons as explained of the
  20. || prior ruleset.
  21. || max-width: 380px is the limit for the red
  22. || Box before the content is askew.
  23. || justify-content: space-around was added in
  24. || order to stabilize it's width.
  25. ======================
  26. line 11,31 to 33
  27. || Changed rem units to em because it's ideal for
  28. || a responsive layout. Although rem is a
  29. || relative measurement like it's sister
  30. || property,em,it's based on the root's default
  31. || so directly changing that measurement will
  32. || yield a change,whilst with em,only the
  33. || container (or parent) needs to resize on the
  34. || fly.
  35. ======================
  36. line 44 to 46
  37. || This ghost flex item is the key to the red
  38. || Boxes' content being able to maintain a
  39. || uniformed layout.
  40. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  41. HTML
  42. line 8
  43. || Added an extra div in div.red to be the ghost
  44. || flex item. Flex items will exhibit a uniform
  45. || behavior when there are an equal amount of
  46. || them in each row.
  47. */
  1. html,body {
  2. height: 100%;
  3. width: 100%;
  4. margin: 0;
  5. padding: 0;
  6. Box-sizing: border-Box;
  7. }
  8. body > div {
  9. display: flex;
  10. width: 100vw;
  11. justify-content: space-between;
  12. border: 1px solid black;
  13. }
  14. body > div > div {
  15. display: flex;
  16. flex-flow: row wrap;
  17. }
  18. body > div > div.red {
  19. background: rgba(255,.1);
  20. flex: 0 1 62%;
  21. min-width: 60%;
  22. justify-content: space-around;
  23. }
  24. body > div > div.green {
  25. background: rgba(0,.1);
  26. justify-content: space-around;
  27. flex: 0 1 22%;
  28. width: 20%;
  29. }
  30. body > div > div > div {
  31. margin: 1em;
  32. height: 5em;
  33. width: 4em;
  34. background: rgba(0,.1);
  35. }
  36. div.red > div:last-of-type {
  37. opacity: 0;
  38. }

猜你在找的CSS相关文章