我如何在CSS网格项目中垂直和水平的水平中心文本?
奇怪的是,CSS网格没有可以轻松执行此操作的属性(我的Codepen: htttps:htttps:// codepen.io/chiz/pen/dydezpz )。
.container {
display: grid;
grid-template-columns: 1fr 1fr;
grid-template-rows: 1fr 1fr;
grid-gap: 20px;
align-items: center;
justify-content: center;
}
.container div {
background-color: gray;
height: 100px;
}
<div class="container">
<div>Box 1</div>
<div>Box 2</div>
<div>Box 3</div>
<div>Box 4</div>
</div>
如果我使用align-felt:中心
和合理:中心
内部.container div
part,它会缩小盒子的尺寸到内容的大小。
CSS网格是否应该具有诸如Grid-Item-Align之类的属性:顶级中心的东西? 我试图不使用90年代的CSS hacks来执行这种cos',这将破坏CSS网格的目的。
使用Flexbox,将我想要的东西对齐要容易得多。
It's strange that CSS grid doesn't have properties that can do this easily (my codepen here: https://codepen.io/Chiz/pen/dydeZpz).
.container {
display: grid;
grid-template-columns: 1fr 1fr;
grid-template-rows: 1fr 1fr;
grid-gap: 20px;
align-items: center;
justify-content: center;
}
.container div {
background-color: gray;
height: 100px;
}
<div class="container">
<div>Box 1</div>
<div>Box 2</div>
<div>Box 3</div>
<div>Box 4</div>
</div>
If I use align-self: center
and justify-self: center
inside the .container div
part, it shrinks the sizes of the boxes down to the size of the content.
Shouldn't CSS grid have properties such as grid-item-align:top center sort of thing?
I'm trying not to use 90s CSS hacks to do this cos' that would defeat the purpose of CSS grid.
With flexbox, it's far easier to align things where I want.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Flex
和网格
一起工作。只需在所需的项目上应用您的flex
- 位置属性即可。flex
andgrid
works wonderfully together. Just apply yourflex
-positioning properties on the item you want.在容器上使用网格时,您可以在项目上使用Flexbox。由于Flexbox通常更适合对齐任何东西。
You could use flexbox on the items, while using grid on the container. Since flexbox is generally more used to align anything.