这两种CSS Grid的方法在将内部框居中对齐方面有何不同?

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

What's difference between these two css of grid to center align inner box

问题

我已经使用CSS网格编写了两个代码片段来使内部框居中对齐,但我无法理解它们之间的区别。

我知道我们还有其他方法可以做到,但我好奇知道这两种方法之间的确切区别。

哪一个更合适/正确,为什么

第一个

justify-content: center;
align-content: center;

第二个

justify-items: center;
align-items: center;

HTML

<div class="box1">
  <div>Box1</div>
</div>
<div class="box2">
  <div>Box2</div>
</div>

CSS

.box1, .box2 {
  border: 5px solid lightgreen;
  height: 250px;
  width: 250px;
  display: grid;
  justify-content: center;
  align-content: center;
  margin: 5px;
}
.box2 {
  display: grid;
  justify-items: center;
  align-items: center;
}
.box1>div, .box2>div {
  background: lightgreen;
  padding: 20px;
}

输出
这两种CSS Grid的方法在将内部框居中对齐方面有何不同?

英文:

I have written two snippets using a CSS grid to center align the inner box but am not able to understand the difference.

I know we have other ways to do but am curious to know the exact difference between these two.

Which one is more appropriate/correct and why

First

justify-content: center;
align-content: center;

Second

justify-items: center;
align-items: center;

HTML

&lt;div class=&quot;box1&quot;&gt;
  &lt;div&gt;Box1&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;box2&quot;&gt;
 &lt;div&gt;Box2&lt;/div&gt;
&lt;/div&gt;

CSS

 .box1, .box2 {
  border: 5px solid lightgreen;
  height: 250px;
  width: 250px;
  display: grid;
  justify-content: center;
  align-content: center;
  margin: 5px;
}
.box2 {
  display: grid;
  justify-items: center;
  align-items: center;
}
.box1&gt;div, .box2&gt;div {
  background: lightgreen;
  padding: 20px;
}

Output
这两种CSS Grid的方法在将内部框居中对齐方面有何不同?

答案1

得分: 1

在你的示例中,没有真正的区别,因为每个示例中都只有一个网格项。但它们实际上有不同的目的。

justify-contentalign-content 用于对齐整个网格容器的所有内容,作为一个组。所以在下面的第一个示例中,所有四个框都居中在黑色容器内。

justify-itemsalign-items 用于对齐各个网格项在其各自的网格单元格内。所以在下面的第二个示例中,每个黄色正方形都居中在其所在容器的四分之一内。

当容器中只有一个 div 时,两组属性的结果是相同的,但原因不同。

article.content {
  grid-template-columns: 100px 100px;
  grid-template-rows: 100px 100px;
  justify-content: center;
  align-content: center;
}

article.items {
  grid-template-columns: 1fr 1fr;
  grid-template-rows: 1fr 1fr;
  justify-items: center;
  align-items: center;
}

article {
  display: grid;
  background: black;
  width: 400px;
  height: 400px;
  gap: 20px;
}

div {
  background: yellow;
  width: 100px;
  height: 100px;
}
<h1>justify-content and justify-items</h1>

<article class="content">
  <div></div>
  <div></div>
  <div></div>
  <div></div>
</article>

<h1>justify-items and align-items</h1>

<article class="items">
  <div></div>
  <div></div>
  <div></div>
  <div></div>
</article>
英文:

In your examples, there's no real difference, because you only have one grid item in each. But these actually have different purposes.

justify-content and align-content are for aligning the entire contents of the grid container, as a group. So in the first example below, all four boxes are centered within the black container.

justify-items and align-items are for aligning the individual grid items within their individual grid cells. So in the second example below, each yellow square is centered within its own quarter of the container.

When you have just one div in the container, the result is the same for both sets of properties, but for different reasons.

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

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

article.content {
  grid-template-columns: 100px 100px;
  grid-template-rows: 100px 100px;
  justify-content: center;
  align-content: center;
}

article.items {
  grid-template-columns: 1fr 1fr;
  grid-template-rows: 1fr 1fr;
  justify-items: center;
  align-items: center;
}

article {
  display: grid;
  background: black;
  width: 400px;
  height: 400px;
  gap: 20px;
}

div {
  background: yellow;
  width: 100px;
  height: 100px;
}

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

&lt;h1&gt;justify-content and justify-items&lt;/h1&gt;

&lt;article class=&quot;content&quot;&gt;
  &lt;div&gt;&lt;/div&gt;
  &lt;div&gt;&lt;/div&gt;
  &lt;div&gt;&lt;/div&gt;
  &lt;div&gt;&lt;/div&gt;
&lt;/article&gt;

&lt;h1&gt;justify-items and align-items&lt;/h1&gt;

&lt;article class=&quot;items&quot;&gt;
  &lt;div&gt;&lt;/div&gt;
  &lt;div&gt;&lt;/div&gt;
  &lt;div&gt;&lt;/div&gt;
  &lt;div&gt;&lt;/div&gt;
&lt;/article&gt;

<!-- end snippet -->

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

发表评论

匿名网友

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

确定