当 Flexbox 内容垂直居中时如何修复滚动?

Paw*_*wel 1 css scroll vertical-alignment

我想要实现的是,当视口比内容高时,内容应该垂直居中。当视口不够高并且内容溢出时,父级应该提供垂直滚动条。

当我将 Flexbox 内容对齐到中间并将内容设置为滚动时,它不仅会忽略内容边距,还会在顶部截断(假设视口比内容短)。有没有办法来解决这个问题?

html, body {
    height: 100%;
}
body {
    display: flex;
    flex-wrap: nowrap;
    flex-direction: row;
    overflow: hidden;
}
.container {
    overflow-y: auto;
    display: flex;
    flex-wrap: nowrap;
    flex-direction: row;
    align-items: center;
}

.content {
    border: 1px solid grey;
    background-color: lightgrey;
    padding: 10px;
    margin: 10px;
    border-radius: 10px;
}
Run Code Online (Sandbox Code Playgroud)
<div class="container">
    <div class="content">
        Start of the content
        <br />
        <br />
        Middle of the content
        <br />
        <br />
        End of the content
    </div>
</div>
<div class="container">
    <div class="content">
        Start of the content :(((
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        Middle of the content
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        End of the content
    </div>
</div>
Run Code Online (Sandbox Code Playgroud)

G-C*_*Cyr 7

align-items: safe center应避免儿童擅自离开箱子。

https://developer.mozilla.org/en-US/docs/Web/CSS/align-items

安全的

与对齐关键字一起使用。如果所选关键字意味着该项目溢出对齐容器导致数据丢失,则该项目将按照对齐模式已启动的方式进行对齐。

html, body {
    height: 100%;
}
body {
    display: flex;
    flex-wrap: nowrap;
    flex-direction: row;
    overflow: hidden;
}
.container {
    overflow-y: auto;
    display: flex;
    flex-wrap: nowrap;
    flex-direction: row;
    align-items: safe center;
}

.content {
    border: 1px solid grey;
    background-color: lightgrey;
    padding: 10px;
    margin: 10px;
    border-radius: 10px;
}
Run Code Online (Sandbox Code Playgroud)
<div class="container">
    <div class="content">
        Start of the content
        <br />
        <br />
        Middle of the content
        <br />
        <br />
        End of the content
    </div>
</div>
<div class="container">
    <div class="content">
        Start of the content :(((
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        Middle of the content
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        End of the content
    </div>
</div>
Run Code Online (Sandbox Code Playgroud)

如果您的浏览器不支持对齐的安全/不安全关键字,那么您可以使用自动边距:

html, body {
    height: 100%;
}
body {
    display: flex;
    flex-wrap: nowrap;
    flex-direction: row;
    overflow: hidden;
}
.container {
    overflow-y: auto;
    display: flex;
    flex-wrap: nowrap;
    flex-direction: row; 
    padding:10px 0;
}

.content {
    border: 1px solid grey;
    background-color: lightgrey;
    padding: 10px;
    margin:auto 10px;
    border-radius: 10px;
}
Run Code Online (Sandbox Code Playgroud)
<div class="container">
    <div class="content">
        Start of the content
        <br />
        <br />
        Middle of the content
        <br />
        <br />
        End of the content
    </div>
</div>
<div class="container">
    <div class="content">
        Start of the content :(((
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        Middle of the content
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        End of the content
    </div>
</div>
Run Code Online (Sandbox Code Playgroud)