css - 当内容较大时,垂直居中 div 而不会溢出
问题描述
当没有很多内容时,它会居中,但是当我有更多内容时,它会裁剪并且模态的开头不可见。
如果我不使用绝对位置一切都可以,但是如果我的内容较少,它就不会居中
.overlay {
position: fixed;
top: 0;
left: 0;
width: 100vw;
height: 100%;
z-index: 1000;
overflow: auto;
background: purple;
}
.modal {
max-width: 528px;
padding: 24px 32px;
border-radius: 16px;
box-shadow: 0 0 8px 0 rgba(193, 192, 196, 0.35);
background-color: #ffffff;
margin: 0 auto;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
<body>
<div class="overlay">
<div class="modal">
i want to be vertically centered, but when i have lot of content i dont want to get cropped on top
i want to be vertically centered, but when i have lot of content i dont want to get cropped on top
i want to be vertically centered, but when i have lot of content i dont want to get cropped on top
i want to be vertically centered, but when i have lot of content i dont want to get cropped on top
i want to be vertically centered, but when i have lot of content i dont want to get cropped on top
i want to be vertically centered, but when i have lot of content i dont want to get cropped on top
i want to be vertically centered, but when i have lot of content i dont want to get cropped on top
i want to be vertically centered, but when i have lot of content i dont want to get cropped on top
i want to be vertically centered, but when i have lot of content i dont want to get cropped on top
</div>
</div>
</body>
解决方案
推荐阅读
- javascript - 存储数据和组件数据不一样(vuex + 持久化状态)
- c++ - 抛出未处理的异常:写访问冲突:打印出数组元素
- python - Selenium 等到用户单击按钮
- c# - 根据c#中的给定字符串确定日期逻辑
- reactjs - UTF8 使用 AWS S3 和 Nextjs getStaticProps
- javascript - 如何在 Modal 上显示选中的单选按钮?
- c# - ASP.NET MVC 应用程序无法验证从 Azure AD 收到的令牌
- php - laravel 嵌套的急切加载和第一个函数
- python - 将标签悬停在图表上时显示标签*具有高 y 轴值*、多条线和多轴
- git - 当前分支没有跟踪信息。请指定您要合并的分支