当我们为了让内容居中使用了 align-items center 属性之后,因为屏幕小的会遮挡一部分内容
我们就会给盒子再加上 overflow-y auto 这个属性
但是当我们缩小屏幕时,会发现,内容的上半部分会显示不出来
<style>
.box {
height: 100vh;
display: flex;
align-items: center;
overflow-y: auto;
}
.content {
height: 600px;
}
</style>
<body>
<div class="box">
<div class="content"></div>
</div>
</body>
参考文档:
align-items - CSS(层叠样式表) | MDN
这里提供两种解决方案:
1. 嵌套一层盒子
<style>
.container {
height: 100vh;
display: flex;
align-items: center;
overflow-y: auto;
}
.box {
height: 100%;
}
.content {
height: 600px;
}
</style>
<body>
<div class="container">
<div class="box">
<div class="content"></div>
</div>
</div>
</body>
2. 将 display flex 改为 display grid
<style>
.box {
height: 100vh;
display: grid;
align-items: center;
overflow-y: auto;
}
.content {
height: 600px;
}
</style>
<body>
<div class="box">
<div class="content"></div>
</div>
</body>
======================