首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当柔性盒内容垂直中心时,如何修复滚动?

当柔性盒内容垂直中心时,如何修复滚动?
EN

Stack Overflow用户
提问于 2021-11-24 10:17:45
回答 1查看 511关注 0票数 0

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

当我将柔性盒内容与中间对齐,并将内容设置为滚动时,它不仅会忽略内容边距,而且会在顶部截断(给定的视口比内容短)。有办法解决这个问题吗?

代码语言:javascript
复制
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;
}
代码语言:javascript
复制
<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>

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-11-24 10:30:09

align-items: safe center应该避免孩子们越界。

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

安全

与对齐关键字一起使用。如果选择的关键字意味着项溢出导致数据丢失的对齐容器,则项目将被对齐,就好像对齐模式已经启动一样。

代码语言:javascript
复制
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;
}
代码语言:javascript
复制
<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>

如果浏览器在对齐时不支持安全/不安全的关键字,则可以使用自动页边距:

代码语言:javascript
复制
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;
}
代码语言:javascript
复制
<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>

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70094298

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档