小编Par*_*edi的帖子

垂直和水平居中网格容器

令人惊讶的是,我无法在网上找到任何相关信息.也许我错过了什么.如何使用CSS Grid水平和垂直地将整个主包装器中心放置,显然,保持响应性?

.wrapper {
  display: grid;
  grid-template-columns: minmax(100vw, 1fr);
  grid-template-rows: minmax(100vh, 1fr);
  align-items: center;
  justify-content: center;
}
Run Code Online (Sandbox Code Playgroud)
<div class="wrapper">
  <div class="centerthis">loremloremlorem</div>
</div>
Run Code Online (Sandbox Code Playgroud)

使用上面的CSS,它div是垂直居中,但不是水平居中.

以下CSS将div水平居中,但不是垂直居中:

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

.centerthis {
  align-self: center;
  justify-self: center;
}
Run Code Online (Sandbox Code Playgroud)
<div class="maingrid">
  <div class="centerthis">loremloremlorem</div>
</div>
Run Code Online (Sandbox Code Playgroud)

html css grid css3 css-grid

6
推荐指数
2
解决办法
1万
查看次数

标签 统计

css ×1

css-grid ×1

css3 ×1

grid ×1

html ×1