令人惊讶的是,我在网上找不到任何相关信息。也许我错过了什么。您如何使用 CSS Grid 水平和垂直居中整个主包装器,并且显然保持响应能力?
.wrapper {
display: grid;
grid-template-columns: minmax(100vw, 1fr);
grid-template-rows: minmax(100vh, 1fr);
align-items: center;
justify-content: center;
}
<div class="wrapper">
<div class="centerthis">loremloremlorem</div>
</div>
使用上面的 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;
}
<div class="maingrid">
<div class="centerthis">loremloremlorem</div>
</div>
原文由 parsa_047Fletcher 发布,翻译遵循 CC BY-SA 4.0 许可协议
而不是
justify-content: center
使用justify-items: center
。在你的第二个例子中,你说:
那是因为容器没有额外的高度。行是内容的高度。而不是
grid-template-rows: 1fr
尝试类似grid-template-rows: 100vh
的东西。更多信息: