CSS弹性布局问题:空间分配不均匀问题

huangapple go评论88阅读模式
英文:

css flex layout problem:space allocation not even problem

问题

这是我的代码:

  1. <!-- 开始代码片段: js 隐藏: false 控制台: true Babel: false -->
  2. <!-- 语言: lang-css -->
  3. .cell {
  4. border: 1px solid red;
  5. display: flex;
  6. flex-grow: 1;
  7. margin: 0px;
  8. padding: 3px;
  9. }
  10. .container {
  11. display: flex;
  12. flex-direction: column;
  13. height: 100%;
  14. margin: 0px;
  15. }
  16. .row {
  17. display: flex;
  18. flex-direction: row;
  19. flex-grow: 1;
  20. margin: 0px;
  21. }
  22. html {
  23. height: 100%;
  24. margin: 0px;
  25. padding: 0px;
  26. }
  27. body {
  28. /*border:1px solid black;*/
  29. height: calc(100% - 20px);
  30. margin: 10px;
  31. padding: 0px;
  32. }
  33. <!-- 语言: lang-html -->
  34. <html>
  35. <body onload="">
  36. <div class="container">
  37. <div class="row">
  38. <div class="cell">Caller</div>
  39. <div class="cell">Callee</div>
  40. </div>
  41. <div class="row">
  42. <div class="cell">
  43. sdsdfddd
  44. </div>
  45. <div class="cell">sdfdsf</div>
  46. </div>
  47. </div>
  48. </body>
  49. </html>
  50. <!-- 结束代码片段 -->

为什么第一行单元格的空间分配是均匀的,而第二行单元格不均匀呢?如何使第二行的空间分配与第一行相同?

英文:

Here is my code:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

  1. .cell {
  2. border: 1px solid red;
  3. display: flex;
  4. flex-grow: 1;
  5. margin: 0px;
  6. padding: 3px;
  7. }
  8. .container {
  9. display: flex;
  10. flex-direction: column;
  11. height: 100%;
  12. margin: 0px;
  13. }
  14. .row {
  15. display: flex;
  16. flex-direction: row;
  17. flex-grow: 1;
  18. margin: 0px;
  19. }
  20. html {
  21. height: 100%;
  22. margin: 0px;
  23. padding: 0px;
  24. }
  25. body {
  26. /*border:1px solid black;*/
  27. height: calc(100% - 20px);
  28. margin: 10px;
  29. padding: 0px;
  30. }

<!-- language: lang-html -->

  1. &lt;html&gt;
  2. &lt;body onload=&quot;&quot;&gt;
  3. &lt;div class=&quot;container&quot;&gt;
  4. &lt;div class=&quot;row&quot;&gt;
  5. &lt;div class=&quot;cell&quot;&gt;Caller&lt;/div&gt;
  6. &lt;div class=&quot;cell&quot;&gt;Callee&lt;/div&gt;
  7. &lt;/div&gt;
  8. &lt;div class=&quot;row&quot;&gt;
  9. &lt;div class=&quot;cell&quot;&gt;
  10. sdsdfddd
  11. &lt;/div&gt;
  12. &lt;div class=&quot;cell&quot;&gt;sdfdsf&lt;/div&gt;
  13. &lt;/div&gt;
  14. &lt;/div&gt;
  15. &lt;/body&gt;
  16. &lt;/html&gt;

<!-- end snippet -->

The space allocation for the first-row cells is even, why the second-row cells are not even?
How can I make the space allocation as the first row?

答案1

得分: 1

添加 flex-basis: 0;(或使用 flex: 1 简写)

  1. .cell {
  2. border: 1px solid red;
  3. display: flex;
  4. flex-grow: 1;
  5. flex-basis: 0;
  6. margin: 0px;
  7. padding: 3px;
  8. }
英文:

Add flex-basis: 0; (or use flex:1 shorthand)

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

  1. .cell {
  2. border: 1px solid red;
  3. display: flex;
  4. flex-grow: 1;
  5. flex-basis: 0;
  6. margin: 0px;
  7. padding: 3px;
  8. }
  9. .container {
  10. display: flex;
  11. flex-direction: column;
  12. height: 100%;
  13. margin: 0px;
  14. }
  15. .row {
  16. display: flex;
  17. flex-direction: row;
  18. flex-grow: 1;
  19. margin: 0px;
  20. }
  21. html {
  22. height: 100%;
  23. margin: 0px;
  24. padding: 0px;
  25. }
  26. body {
  27. /*border:1px solid black;*/
  28. height: calc(100% - 20px);
  29. margin: 10px;
  30. padding: 0px;
  31. }

<!-- language: lang-html -->

  1. &lt;html&gt;
  2. &lt;body onload=&quot;&quot;&gt;
  3. &lt;div class=&quot;container&quot;&gt;
  4. &lt;div class=&quot;row&quot;&gt;
  5. &lt;div class=&quot;cell&quot;&gt;Caller&lt;/div&gt;
  6. &lt;div class=&quot;cell&quot;&gt;Callee&lt;/div&gt;
  7. &lt;/div&gt;
  8. &lt;div class=&quot;row&quot;&gt;
  9. &lt;div class=&quot;cell&quot;&gt;
  10. sdsdfddd
  11. &lt;/div&gt;
  12. &lt;div class=&quot;cell&quot;&gt;sdfdsf&lt;/div&gt;
  13. &lt;/div&gt;
  14. &lt;/div&gt;
  15. &lt;/body&gt;
  16. &lt;/html&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月18日 21:23:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76500760.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定