弹性列导致自适应网格变成单列。

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

Flex column causes auto-fit grid to have single column

问题

我有一个CSS网格,它在我将其包装在一个弹性列中之前工作得很好。下面是我能够简化的最小示例。示例显示"hello" div以单列方式显示,而我原本希望有多列。我发现我可以通过添加.grid { width: 100% }或删除.wrapper { align-items: flex-start; }来修复这个问题,但我不明白为什么这两者都可以解决问题。这个问题让我花费了数小时进行调试,我真的想了解支配这种行为的基本规则,以便将来避免类似的错误。

  1. .wrapper {
  2. display: flex;
  3. flex-direction: column;
  4. align-items: flex-start;
  5. }
  6. .grid {
  7. display: grid;
  8. /* width: 100%; */
  9. grid-template-columns: repeat(auto-fit, minmax(100px, 1fr));
  10. }
  1. <div class="wrapper">
  2. <div class="grid">
  3. <div>hello</div>
  4. <div>hello</div>
  5. <div>hello</div>
  6. <div>hello</div>
  7. <div>hello</div>
  8. <div>hello</div>
  9. <div>hello</div>
  10. <div>hello</div>
  11. <div>hello</div>
  12. </div>
  13. </div>
英文:

I have a css grid that works fine until I wrap it in a flex column. Below is the most minimal example I could narrow it down to. The example shows the hello divs are displayed in a single column, when I was expecting multiple columns. I've found I can fix this and make the grid layout as expected by either adding .grid { width: 100% } or removing the .wrapper { align-items: flex-start; } however I do not understand why either fixes the problem. This issue caused me hours of debugging and I would really like to understand the underlying rules governing this behavior so I can avoid similar mistakes in the future.

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

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

  1. .wrapper {
  2. display: flex;
  3. flex-direction: column;
  4. align-items: flex-start;
  5. }
  6. .grid {
  7. display: grid;
  8. /* width: 100%; */
  9. grid-template-columns: repeat(auto-fit, minmax(100px, 1fr));
  10. }

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

  1. &lt;div class=&quot;wrapper&quot;&gt;
  2. &lt;div class=&quot;grid&quot;&gt;
  3. &lt;div&gt;hello&lt;/div&gt;
  4. &lt;div&gt;hello&lt;/div&gt;
  5. &lt;div&gt;hello&lt;/div&gt;
  6. &lt;div&gt;hello&lt;/div&gt;
  7. &lt;div&gt;hello&lt;/div&gt;
  8. &lt;div&gt;hello&lt;/div&gt;
  9. &lt;div&gt;hello&lt;/div&gt;
  10. &lt;div&gt;hello&lt;/div&gt;
  11. &lt;div&gt;hello&lt;/div&gt;
  12. &lt;/div&gt;
  13. &lt;/div&gt;

<!-- end snippet -->

答案1

得分: 1

When you set the flex container (.wrapper) to align-items: flex-start, it packs the flex item (.grid) to the start of the container. See the image below, with added borders for illustration.

  1. .wrapper = 弹性容器 = 黑色边框
  2. .grid = 弹性项目 = 红色边框

弹性列导致自适应网格变成单列。

注意,弹性列具有最小宽度为100px,根据网格代码。

当您移除 align-items: flex-start 时,容器回退到其默认设置:align-items: stretch,这为 .grid 提供了在容器中使用水平空间的全部功能。

弹性列导致自适应网格变成单列。

当您将 .grid 设置为 width: 100% 时,它会覆盖 align-items 并且,就像 stretch 一样,为 .grid 提供了在水平空间中的全部使用权。

英文:

When you set the flex container (.wrapper) to align-items: flex-start, it packs the flex item (.grid), to the start of the container. See the image below, with added borders for illustration.

  1. .wrapper = flex container = black border
  2. .grid = flex item = red border

弹性列导致自适应网格变成单列。

Notice that the flex column has a minimum width of 100px, per the grid code.

When you remove align-items: flex-start, the container falls back to its default setting: align-items: stretch, which provides .grid with full use of the horizontal space in the container.

弹性列导致自适应网格变成单列。

When you set .grid to width: 100%, it overrides align-items and, like with stretch, provides .grid with full use of the horizontal space.

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

发表评论

匿名网友

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

确定