英文:
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;
}
英文:
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
<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;
}
答案1
得分: 1
在你的示例中,没有真正的区别,因为每个示例中都只有一个网格项。但它们实际上有不同的目的。
justify-content
和 align-content
用于对齐整个网格容器的所有内容,作为一个组。所以在下面的第一个示例中,所有四个框都居中在黑色容器内。
justify-items
和 align-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 -->
<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>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论