html - 在 CSS Grid 中将绝对定位的元素居中

标签 html css css-position css-grid

在 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>

codepen LINK

最佳答案

Chrome 似乎在这个问题上偏离了规范指南。

justify-selfalign-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 and align-self on the child.

所以,Firefox 似乎有这个权利。

有关其他居中选项,请参阅此帖子:

关于html - 在 CSS Grid 中将绝对定位的元素居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45784341/

相关文章:

php - 给出警告 : mysqli_real_escape_string() expects parameter 1 to be mysqli, 字符串

javascript - JQuery 遍历每个 div id

html - 如何对齐中心 Font Awesome 图标

javascript - 在 jquery 中,如何设置元素的背景图像而不在我的 javascript 中对图像的 url 进行硬编码?

html - 按钮上的背景图像不显示

CSS DIV定位和边框问题

html - 使用位置 : relative 将文本和图像定位为水平居中

javascript - 如何将焦点设置在 dynatree 标题中的输入字段上

html - 复选框按钮需要多次点击

CSS 下拉列表显示在下拉列表后面,IE6,IE7,绝对定位错误