在 Mozilla 上,这支笔有效。但是当我切换到 Chrome 时它就坏了。
这只是我的问题还是浏览器有问题?
.container {
height: 500px;
width: 500px;
background-color: beige;
display: grid;
grid-template-rows: 1fr;
grid-template-columns: 1fr;
}
.container h2 {
position: absolute;
justify-self: center;
align-self: center;
grid-row: 1;
grid-column: 1;
}
<div class="container">
<h2>TEXT</h2>
</div>
最佳答案
Chrome 似乎在这个问题上偏离了规范指南。
justify-self
和 align-self
属性应该适用于网格容器的绝对定位子元素。
9.2. With a Grid Container as Parent
An absolutely-positioned child of a grid container is out-of-flow and not a grid item, and so does not affect the placement of other items or the sizing of the grid.
The static position of an absolutely-positioned child of a grid container is determined as if it were the sole grid item in a grid area whose edges coincide with the padding edges of the grid container.
Note that this position is affected by the values of
justify-self
andalign-self
on the child.
所以,Firefox 似乎有这个权利。
有关其他居中选项,请参阅此帖子:
关于html - 在 CSS Grid 中将绝对定位的元素居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45784341/