如何在 CSS 网格中进行单行、垂直居中布局?
How do I do a single row, vertically centered, layout in CSS grid?
我正在尝试使用 CSS 网格进行单行、垂直居中布局。这是一个粗略的草图:
注:
- 我只有一行项目
- 项目(可能)宽度相同
- 我不知道我有多少东西(所以我不想说“200px”八十遍)
- 项目高度不同,但需要垂直居中
HTML:
<div class="wrapper">
<div class="box a">A</div>
<div class="box b">B</div>
<div class="box c">C</div>
</div>
CSS:
.wrapper {
display: grid;
grid-gap: 10px;
grid-auto-columns: 200px;
background-color: #fff;
color: #444;
.box {
background-color: #444;
color: #fff;
border-radius: 5px;
padding: 20px;
font-size: 150%;
}
}
我试过这个 i 但它真的想在一行上做多行而不是多列。
我可以在 CSS 网格中做单行、垂直居中的布局吗?如果是,怎么办?
这是一个工作示例。它与其他答案一样有效,但使用不同的 CSS 来避免显式设置网格行。点击下面的'Run':
grid-auto-flow: column;
使项目跨列流动,即排成一行
align-self: center;
垂直居中
.wrapper {
display: grid;
grid-auto-flow: column;
}
.box {
align-self: center;
}
/* Additional styles below */
.wrapper {
grid-gap: 10px;
background-color: #fff;
color: #444;
}
.box {
background-color: #444;
color: #fff;
border-radius: 5px;
padding: 20px;
font-size: 150%;
}
body {
margin: 40px;
}
.box.a {
height: 200px;
}
.box.b {
height: 20px;
}
.box.c {
height: 120px;
}
<div class="wrapper">
<div class="box a">A</div>
<div class="box b">B</div>
<div class="box c">C</div>
<div class="box c">D</div>
</div>
要将所有项目强制排成一行,请将它们设置为 grid-row: 1
。
要将项目居中,请将容器设置为 align-items: center
,或将每个项目设置为 align-self: center
。 (align-self
默认继承align-items
值)。
.wrapper {
display: grid;
align-items: center; /* new */
}
.box {
grid-row: 1; /* new */
}
.box.a { height: 200px; }
.box.b { height: 20px; }
.box.c { height: 120px; }
.box.d { height: 50px; }
/* non-essential decorative styles */
.wrapper {
grid-gap: 10px;
background-color: #fff;
color: #444;
}
.box {
background-color: #444;
color: #fff;
border-radius: 5px;
padding: 20px;
font-size: 150%;
}
body {
margin: 40px;
}
<div class="wrapper">
<div class="box a">A</div>
<div class="box b">B</div>
<div class="box c">C</div>
<div class="box d">D</div>
</div>
我正在尝试使用 CSS 网格进行单行、垂直居中布局。这是一个粗略的草图:
注:
- 我只有一行项目
- 项目(可能)宽度相同
- 我不知道我有多少东西(所以我不想说“200px”八十遍)
- 项目高度不同,但需要垂直居中
HTML:
<div class="wrapper">
<div class="box a">A</div>
<div class="box b">B</div>
<div class="box c">C</div>
</div>
CSS:
.wrapper {
display: grid;
grid-gap: 10px;
grid-auto-columns: 200px;
background-color: #fff;
color: #444;
.box {
background-color: #444;
color: #fff;
border-radius: 5px;
padding: 20px;
font-size: 150%;
}
}
我试过这个 i 但它真的想在一行上做多行而不是多列。
我可以在 CSS 网格中做单行、垂直居中的布局吗?如果是,怎么办?
这是一个工作示例。它与其他答案一样有效,但使用不同的 CSS 来避免显式设置网格行。点击下面的'Run':
grid-auto-flow: column;
使项目跨列流动,即排成一行align-self: center;
垂直居中
.wrapper {
display: grid;
grid-auto-flow: column;
}
.box {
align-self: center;
}
/* Additional styles below */
.wrapper {
grid-gap: 10px;
background-color: #fff;
color: #444;
}
.box {
background-color: #444;
color: #fff;
border-radius: 5px;
padding: 20px;
font-size: 150%;
}
body {
margin: 40px;
}
.box.a {
height: 200px;
}
.box.b {
height: 20px;
}
.box.c {
height: 120px;
}
<div class="wrapper">
<div class="box a">A</div>
<div class="box b">B</div>
<div class="box c">C</div>
<div class="box c">D</div>
</div>
要将所有项目强制排成一行,请将它们设置为 grid-row: 1
。
要将项目居中,请将容器设置为 align-items: center
,或将每个项目设置为 align-self: center
。 (align-self
默认继承align-items
值)。
.wrapper {
display: grid;
align-items: center; /* new */
}
.box {
grid-row: 1; /* new */
}
.box.a { height: 200px; }
.box.b { height: 20px; }
.box.c { height: 120px; }
.box.d { height: 50px; }
/* non-essential decorative styles */
.wrapper {
grid-gap: 10px;
background-color: #fff;
color: #444;
}
.box {
background-color: #444;
color: #fff;
border-radius: 5px;
padding: 20px;
font-size: 150%;
}
body {
margin: 40px;
}
<div class="wrapper">
<div class="box a">A</div>
<div class="box b">B</div>
<div class="box c">C</div>
<div class="box d">D</div>
</div>