CSS Grid – 子元素超出父容器的宽度

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

CSS Grid - child elements going over the width of the parent container

问题

我想使用CSS Grid来实现上面的效果 - 如果宽度无法容纳所有内容,子项目应该向下流动。然而,我得到的结果是子元素超出了父容器的设置宽度。

(项目的宽度需要是 120px 而不是动态宽度)

以下是我的代码:

<div style="width: 50%;">
    <div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(120px, 1fr)); grid-gap: 10px;">
       <div style="width: 120px; border: 1px solid blue;">Item</div>
       <div style="width: 120px; border: 1px solid blue;">Item</div>
       <div style="width: 120px; border: 1px solid blue;">Item</div>
       <div style="width: 120px; border: 1px solid blue;">Item</div>
       <div style="width: 120px; border: 1px solid blue;">Item</div>
       <div style="width: 120px; border: 1px solid blue;">Item</div>
       <div style="width: 120px; border: 1px solid blue;">Item</div>
       <div style="width: 120px; border: 1px solid blue;">Item</div>
       <div style="width: 120px; border: 1px solid blue;">Item</div>
       <div style="width: 120px; border: 1px solid blue;">Item</div>
       <div style="width: 120px; border: 1px solid blue;">Item</div>
       <div style="width: 120px; border: 1px solid blue;">Item</div>
       <div style="width: 120px; border: 1px solid blue;">Item</div>
       <div style="width: 120px; border: 1px solid blue;">Item</div>
       <div style="width: 120px; border: 1px solid blue;">Item</div>
       <div style="width: 120px; border: 1px solid blue;">Item</div>
       <div style="width: 120px; border: 1px solid blue;">Item</div>
       <div style="width: 120px; background: red; font: #fff; grid-column-start: -1; grid-row-start: -1;">Act</div>
     </div>
</div>

排列的结果应该看起来像这样:

英文:

CSS Grid – 子元素超出父容器的宽度

I want to achieve the effect above using CSS Grid - child items should flow downwards if the width can't contain everything. However what I am getting instead is the child elements going over the set width of the parent container.

(The width of the Item needs to be 120px instead of dynamic width)

CSS Grid – 子元素超出父容器的宽度

Below is my code:

&lt;div style=&quot;width: 50%;&quot;&gt;
    &lt;div style=&quot;display: grid; grid-template-columns: repeat(auto-fit, minmax(120px, 1fr)); grid-gap: 10px;&quot;&gt;
       &lt;div style=&quot;width: 120px; border: 1px solid blue;&quot;&gt;Item&lt;/div&gt;
       &lt;div style=&quot;width: 120px; border: 1px solid blue;&quot;&gt;Item&lt;/div&gt;
       &lt;div style=&quot;width: 120px; border: 1px solid blue;&quot;&gt;Item&lt;/div&gt;
       &lt;div style=&quot;width: 120px; border: 1px solid blue;&quot;&gt;Item&lt;/div&gt;
       &lt;div style=&quot;width: 120px; border: 1px solid blue;&quot;&gt;Item&lt;/div&gt;
       &lt;div style=&quot;width: 120px; border: 1px solid blue;&quot;&gt;Item&lt;/div&gt;
       &lt;div style=&quot;width: 120px; border: 1px solid blue;&quot;&gt;Item&lt;/div&gt;
       &lt;div style=&quot;width: 120px; border: 1px solid blue;&quot;&gt;Item&lt;/div&gt;
       &lt;div style=&quot;width: 120px; border: 1px solid blue;&quot;&gt;Item&lt;/div&gt;
       &lt;div style=&quot;width: 120px; border: 1px solid blue;&quot;&gt;Item&lt;/div&gt;
       &lt;div style=&quot;width: 120px; border: 1px solid blue;&quot;&gt;Item&lt;/div&gt;
       &lt;div style=&quot;width: 120px; border: 1px solid blue;&quot;&gt;Item&lt;/div&gt;
       &lt;div style=&quot;width: 120px; border: 1px solid blue;&quot;&gt;Item&lt;/div&gt;
       &lt;div style=&quot;width: 120px; border: 1px solid blue;&quot;&gt;Item&lt;/div&gt;
       &lt;div style=&quot;width: 120px; border: 1px solid blue;&quot;&gt;Item&lt;/div&gt;
       &lt;div style=&quot;width: 120px; border: 1px solid blue;&quot;&gt;Item&lt;/div&gt;
       &lt;div style=&quot;width: 120px; border: 1px solid blue;&quot;&gt;Item&lt;/div&gt;
       &lt;div style=&quot;width: 120px; background: red; font: #fff; grid-column-start: -1; grid-row-start: -1;&quot;&gt;Act&lt;/div&gt;
     &lt;/div&gt;
&lt;/div&gt;

The arrangement outcome should look something like this:
CSS Grid – 子元素超出父容器的宽度

答案1

得分: 0

找到答案:我应该使用grid-column-end: -1而不是grid-column-start: -1

我认为通过使用后者,它创建了一个额外的列,这一点一开始并未考虑到。

英文:

Found the answer: I should have used grid-column-end: -1 instead of grid-column-start: -1.

I think by using the latter, it was creating an additional column not considered to begin with.

huangapple
  • 本文由 发表于 2023年5月29日 06:01:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76353762.html
匿名

发表评论

匿名网友

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

确定