CSS网格为什么超出了父元素?

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

Why is the CSS grid extending beyond the parent?

问题

以下是您要翻译的内容:

"I am in an Angular 15 component using CSS grid. When the grid has one row, the grid bottom is the same as the 'services-section' bottom. When I have more than one row, the grid extends beyond the bottom of 'services-section'. Plus, with each row, that bottom extension grows a little more.

Setting overflow to auto adds scroll bars to the div. Setting overflow to hidden, truncates
the last row. Both of these are not desired. Height, min-height 100%, auto, etc is no go either.

How do I get that grid container to behave?
https://jsfiddle.net/eflat123/vq08u7gb/7/"

  <div class="grid" style="min-height: fit-content;">
    <mat-card class="card">
      <mat-card-content><p>My Service 1</p><p>1 hour</p></mat-card-content>
    </mat-card>
    <mat-card class="card">
      <mat-card-content><p>My Service 2</p><p>1 hour</p></mat-card-content>
    </mat-card>
    <mat-card class="card">
      <mat-card-content><p>My Service 3</p><p>1 hour</p></mat-card-content>
    </mat-card>
    <mat-card class="card">
      <mat-card-content><p>My Service 4</p><p>1 hour</p></mat-card-content>
    </mat-card>
    <mat-card class="card">
      <mat-card-content><p>My Service 5</p><p>1 hour</p></mat-card-content>
    </mat-card>
    <mat-card class="card">
      <mat-card-content><p>My Service 6</p><p>1 hour</p></mat-card-content>
    </mat-card>
    <mat-card class="card">
      <mat-card-content><p>My Service 7</p><p>1 hour</p></mat-card-content>
    </mat-card>
  </div>
</div>

<style>
  .services-section {
    border: 1px solid blue;
  }
  .grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    padding: 0 1.5em;
    grid-row-gap: 7%;
    grid-column-gap: 5%;
  }
</style>
英文:

I am in an Angular 15 component using CSS grid. When the grid has one row, the grid bottom is the same as the 'services-section' bottom. When I have more than one row, the grid extends beyond the bottom of 'services-section'. Plus, with each row, that bottom extension grows a little more.

Setting overflow to auto adds scroll bars to the div. Setting overflow to hidden, truncates
the last row. Both of these are not desired. Height, min-height 100%, auto, etc is no go either.

How do I get that grid container to behave?
https://jsfiddle.net/eflat123/vq08u7gb/7/

  &lt;div class=&quot;grid&quot; style=&quot;min-height: fit-content;&quot;&gt;
    &lt;mat-card class=&quot;card&quot;&gt;
      &lt;mat-card-content&gt;&lt;p&gt;My Service 1&lt;/p&gt;&lt;p&gt;1 hour&lt;/p&gt;&lt;/mat-card-content&gt;
    &lt;/mat-card&gt;
    &lt;mat-card class=&quot;card&quot;&gt;
      &lt;mat-card-content&gt;&lt;p&gt;My Service 2&lt;/p&gt;&lt;p&gt;1 hour&lt;/p&gt;&lt;/mat-card-content&gt;
    &lt;/mat-card&gt;
    &lt;mat-card class=&quot;card&quot;&gt;
      &lt;mat-card-content&gt;&lt;p&gt;My Service 3&lt;/p&gt;&lt;p&gt;1 hour&lt;/p&gt;&lt;/mat-card-content&gt;
    &lt;/mat-card&gt;
    &lt;mat-card class=&quot;card&quot;&gt;
      &lt;mat-card-content&gt;&lt;p&gt;My Service 4&lt;/p&gt;&lt;p&gt;1 hour&lt;/p&gt;&lt;/mat-card-content&gt;
    &lt;/mat-card&gt;
    &lt;mat-card class=&quot;card&quot;&gt;
      &lt;mat-card-content&gt;&lt;p&gt;My Service 5&lt;/p&gt;&lt;p&gt;1 hour&lt;/p&gt;&lt;/mat-card-content&gt;
    &lt;/mat-card&gt;
    &lt;mat-card class=&quot;card&quot;&gt;
      &lt;mat-card-content&gt;&lt;p&gt;My Service 6&lt;/p&gt;&lt;p&gt;1 hour&lt;/p&gt;&lt;/mat-card-content&gt;
    &lt;/mat-card&gt;
    &lt;mat-card class=&quot;card&quot;&gt;
      &lt;mat-card-content&gt;&lt;p&gt;My Service 7&lt;/p&gt;&lt;p&gt;1 hour&lt;/p&gt;&lt;/mat-card-content&gt;
    &lt;/mat-card&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;style&gt;
  .services-section {
    border: 1px solid blue;
  }
  .grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    padding: 0 1.5em;
    grid-row-gap: 7%;
    grid-column-gap: 5%;
  }
&lt;/style&gt;

答案1

得分: 1

"grid-row-gap: 7%;" 是问题的原因,请将其更改为除百分比以外的其他值,这将解决您的问题。

grid-row-gap: 2em;

还要删除或更改

style="height: 100%"

style="min-height: 100%"

在父元素中,以允许在添加卡片时扩展网格。

英文:

the "grid-row-gap: 7%;" is the cause of the issue, change it to something other than a percent, this will fix your problem

grid-row-gap: 2em;

also remove or change the

style=&quot;height: 100%&quot;

to

style=&quot;min-height: 100%&quot;

in the parent to allow the grid to grow when adding cards

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

发表评论

匿名网友

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

确定