这CSS网格为什么溢出?

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

Why is this CSS grid overflowing?

问题

这个网格为什么比它的容器大?在我的数学计算中,45+20+35应该等于100%。当使用百分比时,为什么会发生溢出问题?是加入了间距属性吗?在使用百分比时应该如何考虑这一点?

body {
  width: 100%;
}

.photo-grid {
  max-width: 100%;
  margin: 50px 0;
  height: 300px;
  display: grid;
  grid-template-columns: 45% 20% 35%;
  grid-template-rows: repeat(2, 1fr);
  gap: 30px;
  grid-template-areas: "column-1 column-2 column-3" "column-1 column-2 column-3";
}

.column-1 {
  grid-area: column-1;
}

.photo-1 {
  height: calc(80% - 30px);
  margin-bottom: 30px;
}

.column-2 {
  grid-area: column-2;
}

.photo-2 {
  height: 100%;
}

.column-3 {
  grid-area: column-3;
}

.photo-3 {
  height: calc(50% - 15px);
  margin-bottom: 30px;
}

.photo-4 {
  height: calc(50% - 15px);
}

.textblock {
  background: salmon;
}

img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}
<div class="photo-grid">
  <div class="column-1">
    <div class="photo-1">
      <img src="https://images.unsplash.com/photo-1568605117036-5fe5e7bab0b7?ixlib=rb-4.0.3&amp;ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&amp;auto=format&amp;fit=crop&amp;w=1740&amp;q=80" alt="Team na ziplinen" loading="lazy" />
    </div>
    <div class="textblock">
      <div>
        <p class="bold">Textblock</p>
        <p>Subtext</p>
      </div>
    </div>
  </div>
  <div class="column-2">
    <div class="photo-2">
      <img src="https://images.unsplash.com/photo-1568605117036-5fe5e7bab0b7?ixlib=rb-4.0.3&amp;ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&amp;auto=format&amp;fit=crop&amp;w=1740&amp;q=80" loading="lazy" />
    </div>
  </div>
  <div class="column-3">
    <div class="photo-3">
      <img src="https://images.unsplash.com/photo-1568605117036-5fe5e7bab0b7?ixlib=rb-4.0.3&amp;ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&amp;auto=format&amp;fit=crop&amp;w=1740&amp;q=80" loading="lazy" />
    </div>
    <div class="photo-4">
      <img src="https://images.unsplash.com/photo-1568605117036-5fe5e7bab0b7?ixlib=rb-4.0.3&amp;ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&amp;auto=format&amp;fit=crop&amp;w=1740&amp;q=80" loading="lazy" />
    </div>
  </div>
</div>
英文:

Why is this grid bigger than it's container? In my mathematical calculations 45+20+35 should equal to 100%. The problem does not occur when fractions are used.

What is causing the overflowing issue when using percentages? Is it the gap property being added? How should one account for that when using percentages?

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

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

body {
  width: 100%;    
}

.photo-grid {
  max-width: 100%;
  margin: 50px 0;
  height: 300px;
  display: grid;
  grid-template-columns: 45% 20% 35%;
  grid-template-rows: repeat(2, 1fr);
  gap: 30px;
  grid-template-areas: &quot;column-1 column-2 column-3&quot; &quot;column-1 column-2 column-3&quot;;
}

.column-1 {
  grid-area: column-1;
}

.photo-1 {
  height: calc(80% - 30px);
  margin-bottom: 30px;
}

.column-2 {
  grid-area: column-2;
}

.photo-2 {
  height: 100%;
}

.column-3 {
  grid-area: column-3;
}

.photo-3 {
  height: calc(50% - 15px);
  margin-bottom: 30px;
}

.photo-4 {
  height: calc(50% - 15px);
}

.textblock {
  background: salmon;
}

img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

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

&lt;div class=&quot;photo-grid&quot;&gt;
  &lt;div class=&quot;column-1&quot;&gt;
    &lt;div class=&quot;photo-1&quot;&gt;
      &lt;img src=&quot;https://images.unsplash.com/photo-1568605117036-5fe5e7bab0b7?ixlib=rb-4.0.3&amp;ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&amp;auto=format&amp;fit=crop&amp;w=1740&amp;q=80&quot; alt=&quot;Team na ziplinen&quot; loading=&quot;lazy&quot; /&gt;
    &lt;/div&gt;
    &lt;div class=&quot;textblock&quot;&gt;
      &lt;div&gt;
        &lt;p class=&quot;bold&quot;&gt;Textblock&lt;/p&gt;
        &lt;p&gt;Subtext&lt;/p&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
  &lt;div class=&quot;column-2&quot;&gt;
    &lt;div class=&quot;photo-2&quot;&gt;
      &lt;img src=&quot;https://images.unsplash.com/photo-1568605117036-5fe5e7bab0b7?ixlib=rb-4.0.3&amp;ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&amp;auto=format&amp;fit=crop&amp;w=1740&amp;q=80&quot; loading=&quot;lazy&quot; /&gt;
    &lt;/div&gt;
  &lt;/div&gt;
  &lt;div class=&quot;column-3&quot;&gt;
    &lt;div class=&quot;photo-3&quot;&gt;
      &lt;img src=&quot;https://images.unsplash.com/photo-1568605117036-5fe5e7bab0b7?ixlib=rb-4.0.3&amp;ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&amp;auto=format&amp;fit=crop&amp;w=1740&amp;q=80&quot; loading=&quot;lazy&quot; /&gt;
    &lt;/div&gt;
    &lt;div class=&quot;photo-4&quot;&gt;
      &lt;img src=&quot;https://images.unsplash.com/photo-1568605117036-5fe5e7bab0b7?ixlib=rb-4.0.3&amp;ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&amp;auto=format&amp;fit=crop&amp;w=1740&amp;q=80&quot; loading=&quot;lazy&quot; /&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

答案1

得分: 2

Yup, it's because of the gap.

You can use fraction units to get rid of this behavior:

grid-template-columns: 4.5fr 2fr 3.5fr;

If you still want to use percentages, you have to calculate the available room according to your set gap (我编辑了这个答案并在代码段中应用了一个示例)

Additionally, instead of using containers with classes, you can access the grid-cells using css child-selectors like nth-child or nth-of type to style them and position them by using grid-column and grid-row.

I simplified your source for this example:

英文:

Yup, it's because of the gap.

You can use fraction units to get rid of this behavior:

grid-template-columns: 4.5fr 2fr 3.5fr;

If you still want to use percentages, you have to calculate the available room according to your set gap ( i edited this answer and applied an example in the snippet)

Additionally, instead of using containers with classes, you can access the grid-cells using css child-selectors like nth-child or nth-of type to style them and position them by using grid-column and grid-row.

I simplified your source for this example:

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

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

* {
  margin: 0;
  padding: 0;
}

.photo-grid {
  margin: 50px 0;
  height: 300px;
  display: grid;
  gap: 30px;
  /*
   * easy Solution: grid-template-columns: 4.5fr 2fr 3.5fr;
   */
  /*
   * complex solution: use calc() to calculate the available space per column excluding gaps
   * (available space = gap * (columnCount - 1) / columnCount
   */
  --gapSpace: calc((30px * 2) / 3);
  grid-template-columns: calc(45% - var(--gapSpace)) calc(20% - var(--gapSpace)) calc(35% - var(--gapSpace));
  grid-template-rows: repeat(2, 1fr);
}

.photo-grid img:nth-of-type(2) {
  grid-column: 2 / 3;
  grid-row: 1/3;
}

.textblock {
  grid-column: 1/2;
  grid-row: 2/3;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: salmon;
}

img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: cover;
  object-position: center;
}

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

&lt;div class=&quot;photo-grid&quot;&gt;

  &lt;img src=&quot;https://images.unsplash.com/photo-1568605117036-5fe5e7bab0b7?ixlib=rb-4.0.3&amp;ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&amp;auto=format&amp;fit=crop&amp;w=1740&amp;q=80&quot; loading=&quot;lazy&quot; /&gt;

  &lt;img src=&quot;https://images.unsplash.com/photo-1568605117036-5fe5e7bab0b7?ixlib=rb-4.0.3&amp;ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&amp;auto=format&amp;fit=crop&amp;w=1740&amp;q=80&quot; loading=&quot;lazy&quot; /&gt;

  &lt;img src=&quot;https://images.unsplash.com/photo-1568605117036-5fe5e7bab0b7?ixlib=rb-4.0.3&amp;ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&amp;auto=format&amp;fit=crop&amp;w=1740&amp;q=80&quot; loading=&quot;lazy&quot; /&gt;

  &lt;img src=&quot;https://images.unsplash.com/photo-1568605117036-5fe5e7bab0b7?ixlib=rb-4.0.3&amp;ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&amp;auto=format&amp;fit=crop&amp;w=1740&amp;q=80&quot; loading=&quot;lazy&quot; /&gt;

  &lt;div class=&quot;textblock&quot;&gt;
    &lt;p class=&quot;bold&quot;&gt;Textblock&lt;/p&gt;
    &lt;p&gt;Subtext&lt;/p&gt;
  &lt;/div&gt;

&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年3月7日 17:20:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/75660044.html
匿名

发表评论

匿名网友

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

确定